rc.initdiskless revision 161824
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: head/etc/rc.initdiskless 161824 2006-09-01 16:33:15Z brooks $
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.
49127663Sluigi#		${ipba} and ${class} are typicall 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,
56127663Sluigi# with each sucessive directory overriding (merged into) the previous one;
57149170Sbrooks# non-existing directories are ignored.  The subdirectory forms exist to
58149170Sbrooks# help keep the top level /conf managable 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#
72127663Sluigi# /conf/T/M/diskless_remount
73127663Sluigi#		The contents of the file points to an NFS filesystem. E.g. if
74127663Sluigi#		/conf/base/etc/diskless_remount contains "foo.com:/etc",
75127663Sluigi#		then foo.com:/etc will be be mounted on /conf/base/etc/
76127663Sluigi#		If the file contains a pathname starting with "/", then
77127663Sluigi#		the root path is prepended to it; this allows relocation of
78149556Sbmah#		the root filesystem without changing configuration files.
79161824Sbrooks#		Because mount_nfs understands ".." in paths, it is
80161824Sbrooks#		possible to mount from locations above the NFS root with
81161824Sbrooks#		paths such as "/../../etc".
82108191Sdillon#
83127663Sluigi# /conf/T/M/md_size
84127663Sluigi#		The contents of the file specifies the size of the memory
85127663Sluigi#		filesystem to be created, in 512 byte blocks.
86127663Sluigi#		The default size is 10240 blocks (5MB). E.g. if
87127663Sluigi#		/conf/base/etc/md_size contains "30000" then a 15MB MFS
88127663Sluigi#		will be created. In case of multiple entries for the same
89127663Sluigi#		directory M, the last one in the scanning order is used.
90127663Sluigi#		NOTE: If you only need to create a memory filesystem but not
91127663Sluigi#		initialize it from a template, it is preferrable to specify
92127663Sluigi#		it in fstab e.g. as  "md /tmp mfs -s=30m,rw 0 0"
93108191Sdillon#
94127663Sluigi# /conf/T/SUBDIR.cpio.gz
95127663Sluigi#		The file is cpio'd into /SUBDIR (and a memory filesystem is
96127663Sluigi#		created for /SUBDIR if necessary). The presence of this file
97127663Sluigi#		prevents the copy from /conf/T/SUBDIR/
98108191Sdillon#
99127663Sluigi# /conf/T/SUBDIR.remove
100127663Sluigi#		The list of paths contained in the file are rm -rf'd
101127663Sluigi#		relative to /SUBDIR.
102127663Sluigi#
103161824Sbrooks# /conf/diskless_remount
104161824Sbrooks#		Similar to /conf/T/M/diskless_remount above, but allows
105161824Sbrooks#		all of /conf to be remounted.  This can be used to allow
106161824Sbrooks#		multiple roots to share the same /conf.
107161824Sbrooks#		
108161824Sbrooks#
109127663Sluigi# You will almost universally want to create the following files under /conf
110127663Sluigi#
111161533Sru# File					Content
112161533Sru# ----------------------------		----------------------------------
113161533Sru# /conf/base/etc/md_size		size of /etc filesystem
114161533Sru# /conf/base/etc/diskless_remount	"/etc"
115161533Sru# /conf/default/etc/rc.conf		generic diskless config parameters
116161533Sru# /conf/default/etc/fstab		generic diskless fstab e.g. like this
117127663Sluigi#
118161533Sru#	foo:/root_part			/	nfs	ro		0 0
119161533Sru#	foo:/usr_part			/usr	nfs     ro		0 0
120161533Sru#	foo:/home_part			/home   nfs     rw      	0 0
121161533Sru#	md				/tmp	mfs     -s=30m,rw	0 0
122161533Sru#	md				/var	mfs	-s=30m,rw	0 0
123161533Sru#	proc				/proc	procfs	rw		0 0
124127663Sluigi#
125127663Sluigi# plus, possibly, overrides for password files etc.
126127663Sluigi#
127127663Sluigi# NOTE!  /var, /tmp, and /dev will be typically created elsewhere, e.g.
128127663Sluigi# as entries in the fstab as above.
129126868Sbrooks# Those filesystems should not be specified in /conf.
130127663Sluigi#
131127663Sluigi# (end of documentation, now get to the real code)
132108191Sdillon
133100280Sgordondlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
134100280Sgordon
13543803Sdillon# chkerr:
13643803Sdillon#
13743803Sdillon# Routine to check for error
13843803Sdillon#
13943803Sdillon#	checks error code and drops into shell on failure.
14043803Sdillon#	if shell exits, terminates script as well as /etc/rc.
14151231Ssheldonh#
142108191Sdillonchkerr() {
143108191Sdillon    case $1 in
144108191Sdillon    0)
145108191Sdillon	;;
146108191Sdillon    *)
147108191Sdillon	echo "$2 failed: dropping into /bin/sh"
148108191Sdillon	/bin/sh
149108191Sdillon	# RESUME
150108191Sdillon	;;
151108191Sdillon    esac
15243803Sdillon}
15343803Sdillon
154108191Sdillon# Create a generic memory disk
155108191Sdillon#
156108191Sdillonmount_md() {
157154239Sbrooks    /sbin/mdmfs -S -i 4096 -s $1 -M md $2
15875931Simp}
15975931Simp
160108191Sdillon# Create the memory filesystem if it has not already been created
161108191Sdillon#
162108191Sdilloncreate_md() {
163108191Sdillon    if [ "x`eval echo \\$md_created_$1`" = "x" ]; then
164110942Sjhay	if [ "x`eval echo \\$md_size_$1`" = "x" ]; then
165121014Skris	    md_size=10240
166108191Sdillon	else
167108191Sdillon	    md_size=`eval echo \\$md_size_$1`
168108191Sdillon	fi
169108191Sdillon	mount_md $md_size /$1
170108191Sdillon	/bin/chmod 755 /$1
171108191Sdillon	eval md_created_$1=created
172108191Sdillon    fi
173108191Sdillon}
174108191Sdillon
17543803Sdillon# DEBUGGING
17643803Sdillon#
17755520Sluigi# set -v
17843803Sdillon
17951231Ssheldonh# Figure out our interface and IP.
18043803Sdillon#
18155520Sluigibootp_ifc=""
18255520Sluigibootp_ipa=""
18355520Sluigibootp_ipbca=""
184149170Sbrooksclass=""
185126787Sphkif [ ${dlv:=0} -ne 0 ] ; then
186126787Sphk	iflist=`ifconfig -l`
187126787Sphk	for i in ${iflist} ; do
188128706Sru	    set -- `ifconfig ${i}`
189126787Sphk	    while [ $# -ge 1 ] ; do
190126787Sphk		if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
191126787Sphk		    bootp_ifc=${i} ; bootp_ipa=${2} ; shift
192126787Sphk		fi
193126787Sphk		if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
194126787Sphk		    bootp_ipbca=$2; shift
195126787Sphk		fi
196126787Sphk		shift
197126787Sphk	    done
198126787Sphk	    if [ "${bootp_ifc}" != "" ] ; then
199126787Sphk		break
200126787Sphk	    fi
201126787Sphk	done
202149170Sbrooks	# Get the values passed with the T134 bootp cookie.
203150169Srwatson	class="`/sbin/sysctl -qn kern.bootp_cookie`"
204127657Sluigi
205149170Sbrooks	echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca} ${class}"
206126787Sphkfi
20743803Sdillon
208117087Sbrooks# Figure out our NFS root path
209121067Sdougb#
210128706Sruset -- `mount -t nfs`
211117087Sbrookswhile [ $# -ge 1 ] ; do
212117087Sbrooks    if [ "$2" = "on" -a "$3" = "/" ]; then
213117087Sbrooks	nfsroot="$1"
214117087Sbrooks	break
215117087Sbrooks    fi
216117087Sbrooks    shift
217117087Sbrooksdone
218117087Sbrooks
219127657Sluigi# The list of directories with template files
220149170Sbrookstemplates="base default"
221149170Sbrooksif [ -n "${bootp_ipbca}" ]; then
222149170Sbrooks	templates="${templates} ${bootp_ipbca} bcast/${bootp_ipbca}"
223149170Sbrooksfi
224149170Sbrooksif [ -n "${class}" ]; then
225149170Sbrooks	templates="${templates} ${class}"
226149170Sbrooksfi
227149170Sbrooksif [ -n "${bootp_ipa}" ]; then
228149170Sbrooks	templates="${templates} ${bootp_ipa} ip/${bootp_ipa}"
229149170Sbrooksfi
230127657Sluigi
231127657Sluigi# The list of filesystems to umount after the copy
232127657Sluigito_umount=""
233127657Sluigi
234161824Sbrooks# If /conf/diskless_remount exists, remount all of /conf.
235126868Sbrooksif [ -d /conf -a -f /conf/diskless_remount ]; then
236126868Sbrooks    nfspt=`/bin/cat /conf/diskless_remount`
237126868Sbrooks    if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then
238126868Sbrooks	nfspt="${nfsroot}${nfspt}"
239126868Sbrooks    fi
240126868Sbrooks    mount_nfs $nfspt /conf
241126868Sbrooks    chkerr $? "mount_nfs $nfspt /conf"
242127657Sluigi    to_umount="/conf"
243126868Sbrooksfi
244126868Sbrooks
245108191Sdillon# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca},
246121067Sdougb# and /conf/${bootp_ipa}.  For each subdirectory found within these
247108191Sdillon# directories:
24843803Sdillon#
249108191Sdillon# - calculate memory filesystem sizes.  If the subdirectory (prior to
250108191Sdillon#   NFS remounting) contains the file 'md_size', the contents specified
251108191Sdillon#   in 512 byte sectors will be used to size the memory filesystem.  Otherwise
252108191Sdillon#   8192 sectors (4MB) is used.
253108191Sdillon#
254108191Sdillon# - handle NFS remounts.  If the subdirectory contains the file
255108191Sdillon#   diskless_remount, the contents of the file is NFS mounted over
256108191Sdillon#   the directory.  For example /conf/base/etc/diskless_remount
257108191Sdillon#   might contain 'myserver:/etc'.  NFS remounts allow you to avoid
258108191Sdillon#   having to dup your system directories in /conf.  Your server must
259108191Sdillon#   be sure to export those filesystems -alldirs, however.
260117087Sbrooks#   If the diskless_remount file contains a string beginning with a
261117087Sbrooks#   '/' it is assumed that the local nfsroot should be prepended to
262117087Sbrooks#   it before attemping to the remount.  This allows the root to be
263117087Sbrooks#   relocated without needing to change the remount files.
264108191Sdillon#
265127657Sluigifor i in ${templates} ; do
266108191Sdillon    for j in /conf/$i/* ; do
267108191Sdillon	# memory filesystem size specification
268108191Sdillon	#
269108191Sdillon	subdir=${j##*/}
270108191Sdillon	if [ -d $j -a -f $j/md_size ]; then
271108191Sdillon	    eval md_size_$subdir=`cat $j/md_size`
272108191Sdillon	fi
27343803Sdillon
274126787Sphk	# remount
275126787Sphk	#
276126787Sphk	if [ -d $j -a -f $j/remount ]; then
277126787Sphk	    nfspt=`/bin/cat $j/remount`
278126787Sphk	    $nfspt $j
279126787Sphk	    chkerr $? "$nfspt $j"
280151916Sbrooks	    to_umount="$j ${to_umount}" # XXX hope it is really a mount!
281126787Sphk	fi
282126787Sphk
283108191Sdillon	# NFS remount
284108191Sdillon	#
285108191Sdillon	if [ -d $j -a -f $j/diskless_remount ]; then
286108191Sdillon	    nfspt=`/bin/cat $j/diskless_remount`
287117087Sbrooks	    if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then
288117087Sbrooks		nfspt="${nfsroot}${nfspt}"
289117087Sbrooks	    fi
290108191Sdillon	    mount_nfs $nfspt $j
291108191Sdillon	    chkerr $? "mount_nfs $nfspt $j"
292151916Sbrooks	    to_umount="$j ${to_umount}"
29375746Sbsd	fi
294108191Sdillon    done
29575746Sbsddone
29675746Sbsd
297108191Sdillon# - Create all required MFS filesystems and populate them from
298108191Sdillon#   our templates.  Support both a direct template and a dir.cpio.gz
299108191Sdillon#   archive.  Support dir.remove files containing a list of relative
300108191Sdillon#   paths to remove.
30195280Sobrien#
302127657Sluigi# The dir.cpio.gz form is there to make the copy process more efficient,
303127657Sluigi# so if the cpio archive is present, it prevents the files from dir/
304127657Sluigi# from being copied.
30595280Sobrien
306127657Sluigifor i in ${templates} ; do
307108191Sdillon    for j in /conf/$i/* ; do
308108191Sdillon	subdir=${j##*/}
309127657Sluigi	if [ -d $j -a ! -f $j.cpio.gz  ]; then
310108191Sdillon	    create_md $subdir
311161824Sbrooks	    cp -Rp $j/ /$subdir
312108191Sdillon	fi
313108191Sdillon    done
314108191Sdillon    for j in /conf/$i/*.cpio.gz ; do
315108191Sdillon	subdir=${j%*.cpio.gz}
316108191Sdillon	subdir=${subdir##*/}
317108191Sdillon	if [ -f $j ]; then
318108191Sdillon	    create_md $subdir
319108191Sdillon	    echo "Loading /$subdir from cpio archive $j"
320151914Sbrooks	    (cd / ; /rescue/tar -xpf $j)
321108191Sdillon	fi
322108191Sdillon    done
323108191Sdillon    for j in /conf/$i/*.remove ; do
324108191Sdillon	subdir=${j%*.remove}
325108191Sdillon	subdir=${subdir##*/}
326108191Sdillon	if [ -f $j ]; then
327108191Sdillon	    # doubly sure it is a memory disk before rm -rf'ing
328108191Sdillon	    create_md $subdir
329108191Sdillon	    (cd /$subdir; rm -rf `/bin/cat $j`)
330108191Sdillon	fi
331108191Sdillon    done
332108191Sdillondone
333104334Sdd
334127657Sluigi# umount partitions used to fill the memory filesystems
335127657Sluigi[ -n "${to_umount}" ] && umount $to_umount
336