3 # Copyright (c) 1999 Matt Dillon
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 # On entry to this script the entire system consists of a read-only root
30 # mounted via NFS. The kernel has run BOOTP and configured an interface
31 # (otherwise it would not have been able to mount the NFS root!)
33 # We use the contents of /conf to create and populate memory filesystems
34 # that are mounted on top of this root to implement the writable
35 # (and host-specific) parts of the root filesystem, and other volatile
38 # The hierarchy in /conf has the form /conf/T/M/ where M are directories
39 # for which memory filesystems will be created and filled,
40 # and T is one of the "template" directories below:
42 # base universal base, typically a replica of the original root;
43 # default secondary universal base, typically overriding some
44 # of the files in the original root;
45 # ${ipba} where ${ipba} is the assigned broadcast IP address
46 # bcast/${ipba} same as above
47 # ${class} where ${class} is a list of directories supplied by
48 # bootp/dhcp through the T134 option.
49 # ${ipba} and ${class} are typicall used to configure features
50 # for group of diskless clients, or even individual features;
51 # ${ip} where ${ip} is the machine's assigned IP address, typically
52 # used to set host-specific features;
53 # ip/${ip} same as above
55 # Template directories are scanned in the order they are listed above,
56 # with each sucessive directory overriding (merged into) the previous one;
57 # non-existing directories are ignored. The subdirectory forms exist to
58 # help keep the top level /conf managable in large installations.
60 # The existence of a directory /conf/T/M causes this script to create a
61 # memory filesystem mounted as /M on the client.
63 # Some files in /conf have special meaning, namely:
66 # ----------------------------------------------------------------
68 # The contents of the file is a mount command. E.g. if
69 # /conf/1.2.3.4/foo/remount contains "mount -o ro /dev/ad0s3",
70 # then /dev/ad0s3 will be be mounted on /conf/1.2.3.4/foo/
72 # /conf/T/M/diskless_remount
73 # The contents of the file points to an NFS filesystem. E.g. if
74 # /conf/base/etc/diskless_remount contains "foo.com:/etc",
75 # then foo.com:/etc will be be mounted on /conf/base/etc/
76 # If the file contains a pathname starting with "/", then
77 # the root path is prepended to it; this allows relocation of
78 # the root filesystem without changing configuration files.
81 # The contents of the file specifies the size of the memory
82 # filesystem to be created, in 512 byte blocks.
83 # The default size is 10240 blocks (5MB). E.g. if
84 # /conf/base/etc/md_size contains "30000" then a 15MB MFS
85 # will be created. In case of multiple entries for the same
86 # directory M, the last one in the scanning order is used.
87 # NOTE: If you only need to create a memory filesystem but not
88 # initialize it from a template, it is preferrable to specify
89 # it in fstab e.g. as "md /tmp mfs -s=30m,rw 0 0"
91 # /conf/T/SUBDIR.cpio.gz
92 # The file is cpio'd into /SUBDIR (and a memory filesystem is
93 # created for /SUBDIR if necessary). The presence of this file
94 # prevents the copy from /conf/T/SUBDIR/
96 # /conf/T/SUBDIR.remove
97 # The list of paths contained in the file are rm -rf'd
98 # relative to /SUBDIR.
100 # You will almost universally want to create the following files under /conf
103 # ---------------------------- ------------------------------------------
104 # /conf/base/etc/md_size size of /etc filesystem
105 # /conf/base/diskless_remount "/etc"
106 # /conf/default/etc/rc.conf generic diskless config parameters
107 # /conf/default/etc/fstab generic diskless fstab e.g. like this
109 # foo:/root_part / nfs ro 0 0
110 # foo:/usr_part /usr nfs ro 0 0
111 # foo:/home_part /home nfs rw 0 0
112 # md /tmp mfs -s=30m,rw 0 0
113 # md /var mfs -s=30m,rw 0 0
114 # proc /proc procfs rw 0 0
116 # plus, possibly, overrides for password files etc.
118 # NOTE! /var, /tmp, and /dev will be typically created elsewhere, e.g.
119 # as entries in the fstab as above.
120 # Those filesystems should not be specified in /conf.
122 # (end of documentation, now get to the real code)
124 dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
128 # Routine to check for error
130 # checks error code and drops into shell on failure.
131 # if shell exits, terminates script as well as /etc/rc.
138 echo "$2 failed: dropping into /bin/sh"
145 # Create a generic memory disk
148 /sbin/mdmfs -i 4096 -s $1 -M md $2
151 # Create the memory filesystem if it has not already been created
154 if [ "x`eval echo \\$md_created_$1`" = "x" ]; then
155 if [ "x`eval echo \\$md_size_$1`" = "x" ]; then
158 md_size=`eval echo \\$md_size_$1`
160 mount_md $md_size /$1
162 eval md_created_$1=created
170 # Figure out our interface and IP.
176 if [ ${dlv:=0} -ne 0 ] ; then
178 for i in ${iflist} ; do
179 set -- `ifconfig ${i}`
180 while [ $# -ge 1 ] ; do
181 if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
182 bootp_ifc=${i} ; bootp_ipa=${2} ; shift
184 if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
185 bootp_ipbca=$2; shift
189 if [ "${bootp_ifc}" != "" ] ; then
193 # Get the values passed with the T134 bootp cookie.
194 class="`/sbin/sysctl -qn kern.bootp_cookie`"
196 echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca} ${class}"
199 # Figure out our NFS root path
201 set -- `mount -t nfs`
202 while [ $# -ge 1 ] ; do
203 if [ "$2" = "on" -a "$3" = "/" ]; then
210 # The list of directories with template files
211 templates="base default"
212 if [ -n "${bootp_ipbca}" ]; then
213 templates="${templates} ${bootp_ipbca} bcast/${bootp_ipbca}"
215 if [ -n "${class}" ]; then
216 templates="${templates} ${class}"
218 if [ -n "${bootp_ipa}" ]; then
219 templates="${templates} ${bootp_ipa} ip/${bootp_ipa}"
222 # The list of filesystems to umount after the copy
225 # If /conf/diskless_remount exists, remount all of /conf. This allows
226 # multiple roots to share the same conf files.
227 if [ -d /conf -a -f /conf/diskless_remount ]; then
228 nfspt=`/bin/cat /conf/diskless_remount`
229 if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then
230 nfspt="${nfsroot}${nfspt}"
232 mount_nfs $nfspt /conf
233 chkerr $? "mount_nfs $nfspt /conf"
237 # Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca},
238 # and /conf/${bootp_ipa}. For each subdirectory found within these
241 # - calculate memory filesystem sizes. If the subdirectory (prior to
242 # NFS remounting) contains the file 'md_size', the contents specified
243 # in 512 byte sectors will be used to size the memory filesystem. Otherwise
244 # 8192 sectors (4MB) is used.
246 # - handle NFS remounts. If the subdirectory contains the file
247 # diskless_remount, the contents of the file is NFS mounted over
248 # the directory. For example /conf/base/etc/diskless_remount
249 # might contain 'myserver:/etc'. NFS remounts allow you to avoid
250 # having to dup your system directories in /conf. Your server must
251 # be sure to export those filesystems -alldirs, however.
252 # If the diskless_remount file contains a string beginning with a
253 # '/' it is assumed that the local nfsroot should be prepended to
254 # it before attemping to the remount. This allows the root to be
255 # relocated without needing to change the remount files.
257 for i in ${templates} ; do
258 for j in /conf/$i/* ; do
259 # memory filesystem size specification
262 if [ -d $j -a -f $j/md_size ]; then
263 eval md_size_$subdir=`cat $j/md_size`
268 if [ -d $j -a -f $j/remount ]; then
269 nfspt=`/bin/cat $j/remount`
271 chkerr $? "$nfspt $j"
272 to_umount="$j ${to_umount}" # XXX hope it is really a mount!
277 if [ -d $j -a -f $j/diskless_remount ]; then
278 nfspt=`/bin/cat $j/diskless_remount`
279 if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then
280 nfspt="${nfsroot}${nfspt}"
283 chkerr $? "mount_nfs $nfspt $j"
284 to_umount="$j ${to_umount}"
289 # - Create all required MFS filesystems and populate them from
290 # our templates. Support both a direct template and a dir.cpio.gz
291 # archive. Support dir.remove files containing a list of relative
294 # The dir.cpio.gz form is there to make the copy process more efficient,
295 # so if the cpio archive is present, it prevents the files from dir/
298 for i in ${templates} ; do
299 for j in /conf/$i/* ; do
301 if [ -d $j -a ! -f $j.cpio.gz ]; then
306 for j in /conf/$i/*.cpio.gz ; do
307 subdir=${j%*.cpio.gz}
311 echo "Loading /$subdir from cpio archive $j"
312 (cd / ; /rescue/tar -xpf $j)
315 for j in /conf/$i/*.remove ; do
319 # doubly sure it is a memory disk before rm -rf'ing
321 (cd /$subdir; rm -rf `/bin/cat $j`)
326 # umount partitions used to fill the memory filesystems
327 [ -n "${to_umount}" ] && umount $to_umount