rc.initdiskless revision 128706
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 128706 2004-04-28 09:09:27Z ru $
2851231Ssheldonh#
29128286Sbrooks# REQUIRE: preseedrandom
30100280Sgordon# PROVIDE: initdiskless
31126744Spjd# KEYWORD: FreeBSD nojail
32100280Sgordon
33121067Sdougb
34108191Sdillon# On entry to this script the entire system consists of a read-only root
35127663Sluigi# mounted via NFS. The kernel has run BOOTP and configured an interface
36108191Sdillon# (otherwise it would not have been able to mount the NFS root!)
37108191Sdillon#
38127663Sluigi# We use the contents of /conf to create and populate memory filesystems
39127663Sluigi# that are mounted on top of this root to implement the writable
40127663Sluigi# (and host-specific) parts of the root filesystem, and other volatile
41127663Sluigi# filesystems.
42108191Sdillon#
43127663Sluigi# The hierarchy in /conf has the form /conf/T/M/ where M are directories
44127663Sluigi# for which memory filesystems will be created and filled,
45127663Sluigi# and T is one of the "template" directories below:
46108191Sdillon#
47127663Sluigi#  base		universal base, typically a replica of the original root;
48127663Sluigi#  default	secondary universal base, typically overriding some
49127663Sluigi#		of the files in the original root;
50127663Sluigi#  ${ipba}	where ${ipba} is the assigned broadcast IP address
51127663Sluigi#  ${class}	where ${class} is a list of directories supplied by
52127663Sluigi#		bootp/dhcp through the T134 option.
53127663Sluigi#		${ipba} and ${class} are typicall used to configure features
54127663Sluigi#		for group of diskless clients, or even individual features;
55127663Sluigi#  ${ip}	where ${ip} is the machine's assigned IP address, typically
56127663Sluigi#		used to set host-specific features;
57127663Sluigi#		
58127663Sluigi# Template directories are scanned in the order they are listed above,
59127663Sluigi# with each sucessive directory overriding (merged into) the previous one;
60127663Sluigi# non-existing directories are ignored.
61108191Sdillon#
62127663Sluigi# The existence of a directory /conf/T/M causes this script to create a
63127663Sluigi# memory filesystem mounted as /M on the client.
64126787Sphk#
65127663Sluigi# Some files in /conf have special meaning, namely:
66108191Sdillon#
67127663Sluigi# Filename	Action
68127663Sluigi# ----------------------------------------------------------------
69127663Sluigi# /conf/T/M/remount
70127663Sluigi#		The contents of the file is a mount command. E.g. if
71127663Sluigi# 		/conf/1.2.3.4/foo/remount contains "mount -o ro /dev/ad0s3",
72127663Sluigi#		then /dev/ad0s3 will be be mounted on /conf/1.2.3.4/foo/
73108191Sdillon#
74127663Sluigi# /conf/T/M/diskless_remount
75127663Sluigi#		The contents of the file points to an NFS filesystem. E.g. if
76127663Sluigi#		/conf/base/etc/diskless_remount contains "foo.com:/etc",
77127663Sluigi#		then foo.com:/etc will be be mounted on /conf/base/etc/
78127663Sluigi#		If the file contains a pathname starting with "/", then
79127663Sluigi#		the root path is prepended to it; this allows relocation of
80127663Sluigi#		the root filesystem withouth changing configuration files.
81108191Sdillon#
82127663Sluigi# /conf/T/M/md_size
83127663Sluigi#		The contents of the file specifies the size of the memory
84127663Sluigi#		filesystem to be created, in 512 byte blocks.
85127663Sluigi#		The default size is 10240 blocks (5MB). E.g. if
86127663Sluigi#		/conf/base/etc/md_size contains "30000" then a 15MB MFS
87127663Sluigi#		will be created. In case of multiple entries for the same
88127663Sluigi#		directory M, the last one in the scanning order is used.
89127663Sluigi#		NOTE: If you only need to create a memory filesystem but not
90127663Sluigi#		initialize it from a template, it is preferrable to specify
91127663Sluigi#		it in fstab e.g. as  "md /tmp mfs -s=30m,rw 0 0"
92108191Sdillon#
93127663Sluigi# /conf/T/SUBDIR.cpio.gz
94127663Sluigi#		The file is cpio'd into /SUBDIR (and a memory filesystem is
95127663Sluigi#		created for /SUBDIR if necessary). The presence of this file
96127663Sluigi#		prevents the copy from /conf/T/SUBDIR/
97108191Sdillon#
98127663Sluigi# /conf/T/SUBDIR.remove
99127663Sluigi#		The list of paths contained in the file are rm -rf'd
100127663Sluigi#		relative to /SUBDIR.
101127663Sluigi#
102127663Sluigi# You will almost universally want to create the following files under /conf
103127663Sluigi#
104127663Sluigi# File				Content
105127663Sluigi# ----------------------------	------------------------------------------
106127663Sluigi# /conf/base/etc/md_size	size of /etc filesystem
107127663Sluigi# /conf/base/diskless_remount	"/etc"
108127663Sluigi# /conf/default/etc/rc.conf	generic diskless config parameters
109127663Sluigi# /conf/default/etc/fstab	generic diskless fstab e.g. like this
110127663Sluigi#
111127663Sluigi#	foo:/root_part		/	nfs	ro		0 0
112127663Sluigi#	foo:/usr_part		/usr	nfs     ro		0 0
113127663Sluigi#	foo:/home_part		/home   nfs     rw      	0 0
114127663Sluigi#	md			/tmp	mfs     -s=30m,rw	0 0
115127663Sluigi#	md			/var	mfs	-s=30m,rw	0 0
116127663Sluigi#	proc			/proc	procfs	rw		0 0
117127663Sluigi#
118127663Sluigi# plus, possibly, overrides for password files etc.
119127663Sluigi#
120127663Sluigi# NOTE!  /var, /tmp, and /dev will be typically created elsewhere, e.g.
121127663Sluigi# as entries in the fstab as above.
122126868Sbrooks# Those filesystems should not be specified in /conf.
123127663Sluigi#
124127663Sluigi# (end of documentation, now get to the real code)
125108191Sdillon
126100280Sgordondlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
127126787Sphk[ ${dlv:=0} -eq 0 ] && [ ! -f /etc/diskless ] && exit 0
128100280Sgordon
12943803Sdillon# chkerr:
13043803Sdillon#
13143803Sdillon# Routine to check for error
13243803Sdillon#
13343803Sdillon#	checks error code and drops into shell on failure.
13443803Sdillon#	if shell exits, terminates script as well as /etc/rc.
13551231Ssheldonh#
136108191Sdillonchkerr() {
137108191Sdillon    case $1 in
138108191Sdillon    0)
139108191Sdillon	;;
140108191Sdillon    *)
141108191Sdillon	echo "$2 failed: dropping into /bin/sh"
142108191Sdillon	/bin/sh
143108191Sdillon	# RESUME
144108191Sdillon	;;
145108191Sdillon    esac
14643803Sdillon}
14743803Sdillon
148108191Sdillon# Create a generic memory disk
149108191Sdillon#
150108191Sdillonmount_md() {
151108191Sdillon    /sbin/mdmfs -i 4096 -s $1 -M md $2
15275931Simp}
15375931Simp
154108191Sdillon# Create the memory filesystem if it has not already been created
155108191Sdillon#
156108191Sdilloncreate_md() {
157108191Sdillon    if [ "x`eval echo \\$md_created_$1`" = "x" ]; then
158110942Sjhay	if [ "x`eval echo \\$md_size_$1`" = "x" ]; then
159121014Skris	    md_size=10240
160108191Sdillon	else
161108191Sdillon	    md_size=`eval echo \\$md_size_$1`
162108191Sdillon	fi
163108191Sdillon	mount_md $md_size /$1
164108191Sdillon	/bin/chmod 755 /$1
165108191Sdillon	eval md_created_$1=created
166108191Sdillon    fi
167108191Sdillon}
168108191Sdillon
16943803Sdillon# DEBUGGING
17043803Sdillon#
17155520Sluigi# set -v
17243803Sdillon
17351231Ssheldonh# Figure out our interface and IP.
17443803Sdillon#
17555520Sluigibootp_ifc=""
17655520Sluigibootp_ipa=""
17755520Sluigibootp_ipbca=""
178126787Sphkif [ ${dlv:=0} -ne 0 ] ; then
179126787Sphk	iflist=`ifconfig -l`
180126787Sphk	for i in ${iflist} ; do
181128706Sru	    set -- `ifconfig ${i}`
182126787Sphk	    while [ $# -ge 1 ] ; do
183126787Sphk		if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
184126787Sphk		    bootp_ifc=${i} ; bootp_ipa=${2} ; shift
185126787Sphk		fi
186126787Sphk		if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
187126787Sphk		    bootp_ipbca=$2; shift
188126787Sphk		fi
189126787Sphk		shift
190126787Sphk	    done
191126787Sphk	    if [ "${bootp_ifc}" != "" ] ; then
192126787Sphk		break
193126787Sphk	    fi
194126787Sphk	done
195127657Sluigi	# Insert the directories passed with the T134 bootp cookie
196127657Sluigi	# in the list of paths used for templates.
197127657Sluigi	i="`/sbin/sysctl -n kern.bootp_cookie`"
198127657Sluigi	[ "${i}" != "" ] && bootp_ipbca="${bootp_ipbca} ${i}"
199127657Sluigi
200126787Sphk	echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}"
201126787Sphkfi
20243803Sdillon
203117087Sbrooks# Figure out our NFS root path
204121067Sdougb#
205128706Sruset -- `mount -t nfs`
206117087Sbrookswhile [ $# -ge 1 ] ; do
207117087Sbrooks    if [ "$2" = "on" -a "$3" = "/" ]; then
208117087Sbrooks	nfsroot="$1"
209117087Sbrooks	break
210117087Sbrooks    fi
211117087Sbrooks    shift
212117087Sbrooksdone
213117087Sbrooks
214127657Sluigi# The list of directories with template files
215127657Sluigitemplates="base default ${bootp_ipbca} ${bootp_ipa}"
216127657Sluigi
217127657Sluigi# The list of filesystems to umount after the copy
218127657Sluigito_umount=""
219127657Sluigi
220126868Sbrooks# If /conf/diskless_remount exists, remount all of /conf.  This allows
221126868Sbrooks# multiple roots to share the same conf files.
222126868Sbrooksif [ -d /conf -a -f /conf/diskless_remount ]; then
223126868Sbrooks    nfspt=`/bin/cat /conf/diskless_remount`
224126868Sbrooks    if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then
225126868Sbrooks	nfspt="${nfsroot}${nfspt}"
226126868Sbrooks    fi
227126868Sbrooks    mount_nfs $nfspt /conf
228126868Sbrooks    chkerr $? "mount_nfs $nfspt /conf"
229127657Sluigi    to_umount="/conf"
230126868Sbrooksfi
231126868Sbrooks
232108191Sdillon# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca},
233121067Sdougb# and /conf/${bootp_ipa}.  For each subdirectory found within these
234108191Sdillon# directories:
23543803Sdillon#
236108191Sdillon# - calculate memory filesystem sizes.  If the subdirectory (prior to
237108191Sdillon#   NFS remounting) contains the file 'md_size', the contents specified
238108191Sdillon#   in 512 byte sectors will be used to size the memory filesystem.  Otherwise
239108191Sdillon#   8192 sectors (4MB) is used.
240108191Sdillon#
241108191Sdillon# - handle NFS remounts.  If the subdirectory contains the file
242108191Sdillon#   diskless_remount, the contents of the file is NFS mounted over
243108191Sdillon#   the directory.  For example /conf/base/etc/diskless_remount
244108191Sdillon#   might contain 'myserver:/etc'.  NFS remounts allow you to avoid
245108191Sdillon#   having to dup your system directories in /conf.  Your server must
246108191Sdillon#   be sure to export those filesystems -alldirs, however.
247117087Sbrooks#   If the diskless_remount file contains a string beginning with a
248117087Sbrooks#   '/' it is assumed that the local nfsroot should be prepended to
249117087Sbrooks#   it before attemping to the remount.  This allows the root to be
250117087Sbrooks#   relocated without needing to change the remount files.
251108191Sdillon#
252127657Sluigifor i in ${templates} ; do
253108191Sdillon    for j in /conf/$i/* ; do
254108191Sdillon	# memory filesystem size specification
255108191Sdillon	#
256108191Sdillon	subdir=${j##*/}
257108191Sdillon	if [ -d $j -a -f $j/md_size ]; then
258108191Sdillon	    eval md_size_$subdir=`cat $j/md_size`
259108191Sdillon	fi
26043803Sdillon
261126787Sphk	# remount
262126787Sphk	#
263126787Sphk	if [ -d $j -a -f $j/remount ]; then
264126787Sphk	    nfspt=`/bin/cat $j/remount`
265126787Sphk	    $nfspt $j
266126787Sphk	    chkerr $? "$nfspt $j"
267127657Sluigi	    to_umount="${to_umount} $j" # XXX hope it is really a mount!
268126787Sphk	fi
269126787Sphk
270108191Sdillon	# NFS remount
271108191Sdillon	#
272108191Sdillon	if [ -d $j -a -f $j/diskless_remount ]; then
273108191Sdillon	    nfspt=`/bin/cat $j/diskless_remount`
274117087Sbrooks	    if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then
275117087Sbrooks		nfspt="${nfsroot}${nfspt}"
276117087Sbrooks	    fi
277108191Sdillon	    mount_nfs $nfspt $j
278108191Sdillon	    chkerr $? "mount_nfs $nfspt $j"
279127657Sluigi	    to_umount="${to_umount} $j"
28075746Sbsd	fi
281108191Sdillon    done
28275746Sbsddone
28375746Sbsd
284108191Sdillon# - Create all required MFS filesystems and populate them from
285108191Sdillon#   our templates.  Support both a direct template and a dir.cpio.gz
286108191Sdillon#   archive.  Support dir.remove files containing a list of relative
287108191Sdillon#   paths to remove.
28895280Sobrien#
289127657Sluigi# The dir.cpio.gz form is there to make the copy process more efficient,
290127657Sluigi# so if the cpio archive is present, it prevents the files from dir/
291127657Sluigi# from being copied.
29295280Sobrien
293127657Sluigifor i in ${templates} ; do
294108191Sdillon    for j in /conf/$i/* ; do
295108191Sdillon	subdir=${j##*/}
296127657Sluigi	if [ -d $j -a ! -f $j.cpio.gz  ]; then
297108191Sdillon	    create_md $subdir
298108191Sdillon	    cp -Rp $j/* /$subdir
299108191Sdillon	fi
300108191Sdillon    done
301108191Sdillon    for j in /conf/$i/*.cpio.gz ; do
302108191Sdillon	subdir=${j%*.cpio.gz}
303108191Sdillon	subdir=${subdir##*/}
304108191Sdillon	if [ -f $j ]; then
305108191Sdillon	    create_md $subdir
306108191Sdillon	    echo "Loading /$subdir from cpio archive $j"
307108191Sdillon	    (cd / ; /stand/gzip -d < $j | /stand/cpio --extract -d )
308108191Sdillon	fi
309108191Sdillon    done
310108191Sdillon    for j in /conf/$i/*.remove ; do
311108191Sdillon	subdir=${j%*.remove}
312108191Sdillon	subdir=${subdir##*/}
313108191Sdillon	if [ -f $j ]; then
314108191Sdillon	    # doubly sure it is a memory disk before rm -rf'ing
315108191Sdillon	    create_md $subdir
316108191Sdillon	    (cd /$subdir; rm -rf `/bin/cat $j`)
317108191Sdillon	fi
318108191Sdillon    done
319108191Sdillondone
320104334Sdd
321127657Sluigi# umount partitions used to fill the memory filesystems
322127657Sluigi[ -n "${to_umount}" ] && umount $to_umount
323