rc.initdiskless revision 340616
1100280Sgordon#!/bin/sh 2100280Sgordon# 3118638Sfjoe# Copyright (c) 1999 Matt Dillon 466830Sobrien# All rights reserved. 566830Sobrien# 666830Sobrien# Redistribution and use in source and binary forms, with or without 766830Sobrien# modification, are permitted provided that the following conditions 866830Sobrien# are met: 966830Sobrien# 1. Redistributions of source code must retain the above copyright 1066830Sobrien# notice, this list of conditions and the following disclaimer. 1166830Sobrien# 2. Redistributions in binary form must reproduce the above copyright 1266830Sobrien# notice, this list of conditions and the following disclaimer in the 1366830Sobrien# documentation and/or other materials provided with the distribution. 1466830Sobrien# 1566830Sobrien# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1666830Sobrien# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1766830Sobrien# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1866830Sobrien# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1966830Sobrien# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2066830Sobrien# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2166830Sobrien# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2266830Sobrien# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2366830Sobrien# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2466830Sobrien# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2566830Sobrien# SUCH DAMAGE. 2666830Sobrien# 2751231Ssheldonh# $FreeBSD: stable/10/etc/rc.initdiskless 340616 2018-11-19 06:52:20Z eugen $ 28100280Sgordon 29108191Sdillon# On entry to this script the entire system consists of a read-only root 30127663Sluigi# mounted via NFS. The kernel has run BOOTP and configured an interface 31108191Sdillon# (otherwise it would not have been able to mount the NFS root!) 32108191Sdillon# 33127663Sluigi# We use the contents of /conf to create and populate memory filesystems 34127663Sluigi# that are mounted on top of this root to implement the writable 35127663Sluigi# (and host-specific) parts of the root filesystem, and other volatile 36127663Sluigi# filesystems. 37108191Sdillon# 38127663Sluigi# The hierarchy in /conf has the form /conf/T/M/ where M are directories 39127663Sluigi# for which memory filesystems will be created and filled, 40127663Sluigi# and T is one of the "template" directories below: 41108191Sdillon# 42127663Sluigi# base universal base, typically a replica of the original root; 43127663Sluigi# default secondary universal base, typically overriding some 44127663Sluigi# of the files in the original root; 45127663Sluigi# ${ipba} where ${ipba} is the assigned broadcast IP address 46149170Sbrooks# bcast/${ipba} same as above 47127663Sluigi# ${class} where ${class} is a list of directories supplied by 48127663Sluigi# bootp/dhcp through the T134 option. 49229783Suqs# ${ipba} and ${class} are typically used to configure features 50127663Sluigi# for group of diskless clients, or even individual features; 51127663Sluigi# ${ip} where ${ip} is the machine's assigned IP address, typically 52127663Sluigi# used to set host-specific features; 53149170Sbrooks# ip/${ip} same as above 54130151Sschweikh# 55127663Sluigi# Template directories are scanned in the order they are listed above, 56229783Suqs# with each successive directory overriding (merged into) the previous one; 57149170Sbrooks# non-existing directories are ignored. The subdirectory forms exist to 58229783Suqs# help keep the top level /conf manageable in large installations. 59108191Sdillon# 60127663Sluigi# The existence of a directory /conf/T/M causes this script to create a 61127663Sluigi# memory filesystem mounted as /M on the client. 62126787Sphk# 63127663Sluigi# Some files in /conf have special meaning, namely: 64108191Sdillon# 65127663Sluigi# Filename Action 66127663Sluigi# ---------------------------------------------------------------- 67127663Sluigi# /conf/T/M/remount 68127663Sluigi# The contents of the file is a mount command. E.g. if 69127663Sluigi# /conf/1.2.3.4/foo/remount contains "mount -o ro /dev/ad0s3", 70127663Sluigi# then /dev/ad0s3 will be be mounted on /conf/1.2.3.4/foo/ 71108191Sdillon# 72179014Sbms# /conf/T/M/remount_optional 73179014Sbms# If this file exists, then failure to execute the mount 74179014Sbms# command contained in /conf/T/M/remount is non-fatal. 75179014Sbms# 76182895Sbms# /conf/T/M/remount_subdir 77182895Sbms# If this file exists, then the behaviour of /conf/T/M/remount 78182895Sbms# changes as follows: 79182895Sbms# 1. /conf/T/M/remount is invoked to mount the root of the 80182895Sbms# filesystem where the configuration data exists on a 81182895Sbms# temporary mountpoint. 82182895Sbms# 2. /conf/T/M/remount_subdir is then invoked to mount a 83182895Sbms# *subdirectory* of the filesystem mounted by 84182895Sbms# /conf/T/M/remount on /conf/T/M/. 85182895Sbms# 86127663Sluigi# /conf/T/M/diskless_remount 87164862Sluigi# The contents of the file points to an NFS filesystem, 88164862Sluigi# possibly followed by mount_nfs options. If the server name 89164862Sluigi# is omitted, the script will prepend the root path used when 90164862Sluigi# booting. E.g. if you booted from foo.com:/path/to/root, 91164862Sluigi# an entry for /conf/base/etc/diskless_remount could be any of 92164862Sluigi# foo.com:/path/to/root/etc 93164862Sluigi# /etc -o ro 94161824Sbrooks# Because mount_nfs understands ".." in paths, it is 95161824Sbrooks# possible to mount from locations above the NFS root with 96161824Sbrooks# paths such as "/../../etc". 97108191Sdillon# 98127663Sluigi# /conf/T/M/md_size 99127663Sluigi# The contents of the file specifies the size of the memory 100127663Sluigi# filesystem to be created, in 512 byte blocks. 101127663Sluigi# The default size is 10240 blocks (5MB). E.g. if 102127663Sluigi# /conf/base/etc/md_size contains "30000" then a 15MB MFS 103127663Sluigi# will be created. In case of multiple entries for the same 104127663Sluigi# directory M, the last one in the scanning order is used. 105127663Sluigi# NOTE: If you only need to create a memory filesystem but not 106299826Spfg# initialize it from a template, it is preferable to specify 107127663Sluigi# it in fstab e.g. as "md /tmp mfs -s=30m,rw 0 0" 108108191Sdillon# 109127663Sluigi# /conf/T/SUBDIR.cpio.gz 110127663Sluigi# The file is cpio'd into /SUBDIR (and a memory filesystem is 111127663Sluigi# created for /SUBDIR if necessary). The presence of this file 112127663Sluigi# prevents the copy from /conf/T/SUBDIR/ 113108191Sdillon# 114340612Seugen# /conf/T/M/extract 115340612Seugen# This is alternative to SUBDIR.cpio.gz and remount. 116340612Seugen# Similar to remount case, a memory filesystem is created 117340612Seugen# for /M and initialized from a template but no mounting 118340616Seugen# performed. Instead, this file is run passing /M as single 119340612Seugen# argument. It is expected to extract template override to /M 120340612Seugen# using auxiliary storage found in some embedded systems 121340612Seugen# having NVRAM too small to hold mountable file system. 122340612Seugen# 123127663Sluigi# /conf/T/SUBDIR.remove 124127663Sluigi# The list of paths contained in the file are rm -rf'd 125127663Sluigi# relative to /SUBDIR. 126127663Sluigi# 127161824Sbrooks# /conf/diskless_remount 128161824Sbrooks# Similar to /conf/T/M/diskless_remount above, but allows 129161824Sbrooks# all of /conf to be remounted. This can be used to allow 130161824Sbrooks# multiple roots to share the same /conf. 131161824Sbrooks# 132208060Sdougb# 133127663Sluigi# You will almost universally want to create the following files under /conf 134127663Sluigi# 135161533Sru# File Content 136161533Sru# ---------------------------- ---------------------------------- 137161533Sru# /conf/base/etc/md_size size of /etc filesystem 138161533Sru# /conf/base/etc/diskless_remount "/etc" 139161533Sru# /conf/default/etc/rc.conf generic diskless config parameters 140161533Sru# /conf/default/etc/fstab generic diskless fstab e.g. like this 141127663Sluigi# 142161533Sru# foo:/root_part / nfs ro 0 0 143161533Sru# foo:/usr_part /usr nfs ro 0 0 144161533Sru# foo:/home_part /home nfs rw 0 0 145161533Sru# md /tmp mfs -s=30m,rw 0 0 146161533Sru# md /var mfs -s=30m,rw 0 0 147161533Sru# proc /proc procfs rw 0 0 148127663Sluigi# 149127663Sluigi# plus, possibly, overrides for password files etc. 150127663Sluigi# 151127663Sluigi# NOTE! /var, /tmp, and /dev will be typically created elsewhere, e.g. 152127663Sluigi# as entries in the fstab as above. 153126868Sbrooks# Those filesystems should not be specified in /conf. 154127663Sluigi# 155127663Sluigi# (end of documentation, now get to the real code) 156108191Sdillon 157100280Sgordondlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` 158100280Sgordon 159164862Sluigi# DEBUGGING 160164862Sluigi# log something on stdout if verbose. 161164862Sluigio_verbose=0 # set to 1 or 2 if you want more debugging 162164862Sluigilog() { 163164862Sluigi [ ${o_verbose} -gt 0 ] && echo "*** $* ***" 164164862Sluigi [ ${o_verbose} -gt 1 ] && read -p "=== Press enter to continue" foo 165164862Sluigi} 166164862Sluigi 16743803Sdillon# chkerr: 16843803Sdillon# 16943803Sdillon# Routine to check for error 17043803Sdillon# 17143803Sdillon# checks error code and drops into shell on failure. 17243803Sdillon# if shell exits, terminates script as well as /etc/rc. 173179014Sbms# if remount_optional exists under the mountpoint, skip this check. 17451231Ssheldonh# 175108191Sdillonchkerr() { 176179014Sbms lastitem () ( n=$(($# - 1)) ; shift $n ; echo $1 ) 177179014Sbms mountpoint="$(lastitem $2)" 178179014Sbms [ -r $mountpoint/remount_optional ] && ( echo "$2 failed: ignoring due to remount_optional" ; return ) 179108191Sdillon case $1 in 180108191Sdillon 0) 181108191Sdillon ;; 182108191Sdillon *) 183108191Sdillon echo "$2 failed: dropping into /bin/sh" 184108191Sdillon /bin/sh 185108191Sdillon # RESUME 186108191Sdillon ;; 187108191Sdillon esac 18843803Sdillon} 18943803Sdillon 190164862Sluigi# The list of filesystems to umount after the copy 191164862Sluigito_umount="" 192164862Sluigi 193164862Sluigihandle_remount() { # $1 = mount point 194164862Sluigi local nfspt mountopts b 195164862Sluigi b=$1 196164862Sluigi log handle_remount $1 197164862Sluigi [ -d $b -a -f $b/diskless_remount ] || return 198164862Sluigi read nfspt mountopts < $b/diskless_remount 199164862Sluigi log "nfspt ${nfspt} mountopts ${mountopts}" 200164862Sluigi # prepend the nfs root if not present 201164862Sluigi [ `expr "$nfspt" : '\(.\)'` = "/" ] && nfspt="${nfsroot}${nfspt}" 202164862Sluigi mount_nfs $mountopts $nfspt $b 203164862Sluigi chkerr $? "mount_nfs $nfspt $b" 204164862Sluigi to_umount="$b ${to_umount}" 205164862Sluigi} 206164862Sluigi 207108191Sdillon# Create a generic memory disk 208108191Sdillon# 209108191Sdillonmount_md() { 210154239Sbrooks /sbin/mdmfs -S -i 4096 -s $1 -M md $2 21175931Simp} 21275931Simp 213108191Sdillon# Create the memory filesystem if it has not already been created 214108191Sdillon# 215108191Sdilloncreate_md() { 216164862Sluigi [ "x`eval echo \\$md_created_$1`" = "x" ] || return # only once 217110942Sjhay if [ "x`eval echo \\$md_size_$1`" = "x" ]; then 218121014Skris md_size=10240 219108191Sdillon else 220108191Sdillon md_size=`eval echo \\$md_size_$1` 221108191Sdillon fi 222164862Sluigi log create_md $1 with size $md_size 223108191Sdillon mount_md $md_size /$1 224108191Sdillon /bin/chmod 755 /$1 225108191Sdillon eval md_created_$1=created 226108191Sdillon} 227108191Sdillon 22843803Sdillon# DEBUGGING 22943803Sdillon# 23055520Sluigi# set -v 23143803Sdillon 23251231Ssheldonh# Figure out our interface and IP. 23343803Sdillon# 23455520Sluigibootp_ifc="" 23555520Sluigibootp_ipa="" 23655520Sluigibootp_ipbca="" 237149170Sbrooksclass="" 238126787Sphkif [ ${dlv:=0} -ne 0 ] ; then 239126787Sphk iflist=`ifconfig -l` 240126787Sphk for i in ${iflist} ; do 241128706Sru set -- `ifconfig ${i}` 242126787Sphk while [ $# -ge 1 ] ; do 243126787Sphk if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then 244126787Sphk bootp_ifc=${i} ; bootp_ipa=${2} ; shift 245126787Sphk fi 246126787Sphk if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then 247126787Sphk bootp_ipbca=$2; shift 248126787Sphk fi 249126787Sphk shift 250126787Sphk done 251126787Sphk if [ "${bootp_ifc}" != "" ] ; then 252126787Sphk break 253126787Sphk fi 254126787Sphk done 255149170Sbrooks # Get the values passed with the T134 bootp cookie. 256150169Srwatson class="`/sbin/sysctl -qn kern.bootp_cookie`" 257127657Sluigi 258149170Sbrooks echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca} ${class}" 259126787Sphkfi 26043803Sdillon 261164862Sluigilog Figure out our NFS root path 262121067Sdougb# 263128706Sruset -- `mount -t nfs` 264117087Sbrookswhile [ $# -ge 1 ] ; do 265117087Sbrooks if [ "$2" = "on" -a "$3" = "/" ]; then 266117087Sbrooks nfsroot="$1" 267117087Sbrooks break 268117087Sbrooks fi 269117087Sbrooks shift 270117087Sbrooksdone 271117087Sbrooks 272127657Sluigi# The list of directories with template files 273149170Sbrookstemplates="base default" 274149170Sbrooksif [ -n "${bootp_ipbca}" ]; then 275149170Sbrooks templates="${templates} ${bootp_ipbca} bcast/${bootp_ipbca}" 276149170Sbrooksfi 277149170Sbrooksif [ -n "${class}" ]; then 278149170Sbrooks templates="${templates} ${class}" 279149170Sbrooksfi 280149170Sbrooksif [ -n "${bootp_ipa}" ]; then 281149170Sbrooks templates="${templates} ${bootp_ipa} ip/${bootp_ipa}" 282149170Sbrooksfi 283127657Sluigi 284161824Sbrooks# If /conf/diskless_remount exists, remount all of /conf. 285164862Sluigihandle_remount /conf 286126868Sbrooks 287108191Sdillon# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca}, 288121067Sdougb# and /conf/${bootp_ipa}. For each subdirectory found within these 289108191Sdillon# directories: 29043803Sdillon# 291108191Sdillon# - calculate memory filesystem sizes. If the subdirectory (prior to 292108191Sdillon# NFS remounting) contains the file 'md_size', the contents specified 293108191Sdillon# in 512 byte sectors will be used to size the memory filesystem. Otherwise 294108191Sdillon# 8192 sectors (4MB) is used. 295108191Sdillon# 296108191Sdillon# - handle NFS remounts. If the subdirectory contains the file 297108191Sdillon# diskless_remount, the contents of the file is NFS mounted over 298108191Sdillon# the directory. For example /conf/base/etc/diskless_remount 299108191Sdillon# might contain 'myserver:/etc'. NFS remounts allow you to avoid 300108191Sdillon# having to dup your system directories in /conf. Your server must 301108191Sdillon# be sure to export those filesystems -alldirs, however. 302117087Sbrooks# If the diskless_remount file contains a string beginning with a 303117087Sbrooks# '/' it is assumed that the local nfsroot should be prepended to 304117087Sbrooks# it before attemping to the remount. This allows the root to be 305117087Sbrooks# relocated without needing to change the remount files. 306108191Sdillon# 307164862Sluigilog "templates are ${templates}" 308127657Sluigifor i in ${templates} ; do 309108191Sdillon for j in /conf/$i/* ; do 310164862Sluigi [ -d $j ] || continue 311164862Sluigi 312108191Sdillon # memory filesystem size specification 313108191Sdillon subdir=${j##*/} 314164862Sluigi [ -f $j/md_size ] && eval md_size_$subdir=`cat $j/md_size` 31543803Sdillon 316164862Sluigi # remount. Beware, the command is in the file itself! 317164862Sluigi if [ -f $j/remount ]; then 318182895Sbms if [ -f $j/remount_subdir ]; then 319182895Sbms k="/conf.tmp/$i/$subdir" 320182895Sbms [ -d $k ] || continue 321182895Sbms 322182895Sbms # Mount the filesystem root where the config data is 323182895Sbms # on the temporary mount point. 324182895Sbms nfspt=`/bin/cat $j/remount` 325182895Sbms $nfspt $k 326182895Sbms chkerr $? "$nfspt $k" 327182895Sbms 328182895Sbms # Now use a nullfs mount to get the data where we 329182895Sbms # really want to see it. 330182895Sbms remount_subdir=`/bin/cat $j/remount_subdir` 331182895Sbms remount_subdir_cmd="mount -t nullfs $k/$remount_subdir" 332182895Sbms 333182895Sbms $remount_subdir_cmd $j 334182895Sbms chkerr $? "$remount_subdir_cmd $j" 335182895Sbms 336182895Sbms # XXX check order -- we must force $k to be unmounted 337182895Sbms # after j, as j depends on k. 338182895Sbms to_umount="$j $k ${to_umount}" 339182895Sbms else 340182895Sbms nfspt=`/bin/cat $j/remount` 341182895Sbms $nfspt $j 342182895Sbms chkerr $? "$nfspt $j" 343182895Sbms to_umount="$j ${to_umount}" # XXX hope it is really a mount! 344182895Sbms fi 345126787Sphk fi 346126787Sphk 347108191Sdillon # NFS remount 348164862Sluigi handle_remount $j 349108191Sdillon done 35075746Sbsddone 35175746Sbsd 352108191Sdillon# - Create all required MFS filesystems and populate them from 353108191Sdillon# our templates. Support both a direct template and a dir.cpio.gz 354340612Seugen# archive. Support for auxiliary NVRAM. Support dir.remove files containing 355340612Seugen# a list of relative paths to remove. 35695280Sobrien# 357127657Sluigi# The dir.cpio.gz form is there to make the copy process more efficient, 358127657Sluigi# so if the cpio archive is present, it prevents the files from dir/ 359127657Sluigi# from being copied. 36095280Sobrien 361127657Sluigifor i in ${templates} ; do 362108191Sdillon for j in /conf/$i/* ; do 363108191Sdillon subdir=${j##*/} 364127657Sluigi if [ -d $j -a ! -f $j.cpio.gz ]; then 365108191Sdillon create_md $subdir 366243374Screes cp -Rp $j/ /$subdir 367108191Sdillon fi 368108191Sdillon done 369108191Sdillon for j in /conf/$i/*.cpio.gz ; do 370108191Sdillon subdir=${j%*.cpio.gz} 371108191Sdillon subdir=${subdir##*/} 372108191Sdillon if [ -f $j ]; then 373108191Sdillon create_md $subdir 374108191Sdillon echo "Loading /$subdir from cpio archive $j" 375151914Sbrooks (cd / ; /rescue/tar -xpf $j) 376108191Sdillon fi 377108191Sdillon done 378340612Seugen for j in /conf/$i/*/extract ; do 379340612Seugen if [ -x $j ]; then 380340612Seugen subdir=${j%*/extract} 381340612Seugen subdir=${subdir##*/} 382340612Seugen create_md $subdir 383340612Seugen echo "Loading /$subdir using auxiliary command $j" 384340612Seugen $j /$subdir 385340612Seugen fi 386340612Seugen done 387108191Sdillon for j in /conf/$i/*.remove ; do 388108191Sdillon subdir=${j%*.remove} 389108191Sdillon subdir=${subdir##*/} 390108191Sdillon if [ -f $j ]; then 391108191Sdillon # doubly sure it is a memory disk before rm -rf'ing 392108191Sdillon create_md $subdir 393108191Sdillon (cd /$subdir; rm -rf `/bin/cat $j`) 394108191Sdillon fi 395108191Sdillon done 396108191Sdillondone 397104334Sdd 398127657Sluigi# umount partitions used to fill the memory filesystems 399127657Sluigi[ -n "${to_umount}" ] && umount $to_umount 400