]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/rc.initdiskless
Move a lot of diagnostic output behind $rc_quiet in scripts that
[FreeBSD/FreeBSD.git] / etc / rc.initdiskless
1 #!/bin/sh
2 #
3 # Copyright (c) 1999  Matt Dillon
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
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.
14 #
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
25 # SUCH DAMAGE.
26 #
27 # $FreeBSD$
28
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!)
32 #
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
36 # filesystems.
37 #
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:
41 #
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
54 #
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.
59 #
60 # The existence of a directory /conf/T/M causes this script to create a
61 # memory filesystem mounted as /M on the client.
62 #
63 # Some files in /conf have special meaning, namely:
64 #
65 # Filename      Action
66 # ----------------------------------------------------------------
67 # /conf/T/M/remount
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/
71 #
72 # /conf/T/M/remount_optional
73 #               If this file exists, then failure to execute the mount
74 #               command contained in /conf/T/M/remount is non-fatal.
75 #
76 # /conf/T/M/diskless_remount
77 #               The contents of the file points to an NFS filesystem,
78 #               possibly followed by mount_nfs options. If the server name
79 #               is omitted, the script will prepend the root path used when
80 #               booting. E.g. if you booted from foo.com:/path/to/root,
81 #               an entry for /conf/base/etc/diskless_remount could be any of
82 #                       foo.com:/path/to/root/etc
83 #                       /etc -o ro
84 #               Because mount_nfs understands ".." in paths, it is
85 #               possible to mount from locations above the NFS root with
86 #               paths such as "/../../etc".
87 #
88 # /conf/T/M/md_size
89 #               The contents of the file specifies the size of the memory
90 #               filesystem to be created, in 512 byte blocks.
91 #               The default size is 10240 blocks (5MB). E.g. if
92 #               /conf/base/etc/md_size contains "30000" then a 15MB MFS
93 #               will be created. In case of multiple entries for the same
94 #               directory M, the last one in the scanning order is used.
95 #               NOTE: If you only need to create a memory filesystem but not
96 #               initialize it from a template, it is preferrable to specify
97 #               it in fstab e.g. as  "md /tmp mfs -s=30m,rw 0 0"
98 #
99 # /conf/T/SUBDIR.cpio.gz
100 #               The file is cpio'd into /SUBDIR (and a memory filesystem is
101 #               created for /SUBDIR if necessary). The presence of this file
102 #               prevents the copy from /conf/T/SUBDIR/
103 #
104 # /conf/T/SUBDIR.remove
105 #               The list of paths contained in the file are rm -rf'd
106 #               relative to /SUBDIR.
107 #
108 # /conf/diskless_remount
109 #               Similar to /conf/T/M/diskless_remount above, but allows
110 #               all of /conf to be remounted.  This can be used to allow
111 #               multiple roots to share the same /conf.
112 #               
113 #
114 # You will almost universally want to create the following files under /conf
115 #
116 # File                                  Content
117 # ----------------------------          ----------------------------------
118 # /conf/base/etc/md_size                size of /etc filesystem
119 # /conf/base/etc/diskless_remount       "/etc"
120 # /conf/default/etc/rc.conf             generic diskless config parameters
121 # /conf/default/etc/fstab               generic diskless fstab e.g. like this
122 #
123 #       foo:/root_part                  /       nfs     ro              0 0
124 #       foo:/usr_part                   /usr    nfs     ro              0 0
125 #       foo:/home_part                  /home   nfs     rw              0 0
126 #       md                              /tmp    mfs     -s=30m,rw       0 0
127 #       md                              /var    mfs     -s=30m,rw       0 0
128 #       proc                            /proc   procfs  rw              0 0
129 #
130 # plus, possibly, overrides for password files etc.
131 #
132 # NOTE!  /var, /tmp, and /dev will be typically created elsewhere, e.g.
133 # as entries in the fstab as above.
134 # Those filesystems should not be specified in /conf.
135 #
136 # (end of documentation, now get to the real code)
137
138 dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
139
140 # DEBUGGING
141 # log something on stdout if verbose.
142 o_verbose=0     # set to 1 or 2 if you want more debugging
143 log() {
144     [ ${o_verbose} -gt 0 ] && echo "*** $* ***"
145     [ ${o_verbose} -gt 1 ] && read -p "=== Press enter to continue" foo
146 }
147
148 # chkerr:
149 #
150 # Routine to check for error
151 #
152 #       checks error code and drops into shell on failure.
153 #       if shell exits, terminates script as well as /etc/rc.
154 #       if remount_optional exists under the mountpoint, skip this check.
155 #
156 chkerr() {
157     lastitem () ( n=$(($# - 1)) ; shift $n ; echo $1 )
158     mountpoint="$(lastitem $2)"
159     [ -r $mountpoint/remount_optional ] && ( echo "$2 failed: ignoring due to remount_optional" ; return )
160     case $1 in
161     0)
162         ;;
163     *)
164         echo "$2 failed: dropping into /bin/sh"
165         /bin/sh
166         # RESUME
167         ;;
168     esac
169 }
170
171 # The list of filesystems to umount after the copy
172 to_umount=""
173
174 handle_remount() { # $1 = mount point
175     local nfspt mountopts b
176     b=$1
177     log handle_remount $1
178     [ -d $b -a -f $b/diskless_remount ] || return
179     read nfspt mountopts < $b/diskless_remount
180     log "nfspt ${nfspt} mountopts ${mountopts}"
181     # prepend the nfs root if not present
182     [ `expr "$nfspt" : '\(.\)'` = "/" ] && nfspt="${nfsroot}${nfspt}"
183     mount_nfs $mountopts $nfspt $b
184     chkerr $? "mount_nfs $nfspt $b"
185     to_umount="$b ${to_umount}"
186 }
187
188 # Create a generic memory disk
189 #
190 mount_md() {
191     /sbin/mdmfs -S -i 4096 -s $1 -M md $2
192 }
193
194 # Create the memory filesystem if it has not already been created
195 #
196 create_md() {
197         [ "x`eval echo \\$md_created_$1`" = "x" ] || return # only once
198         if [ "x`eval echo \\$md_size_$1`" = "x" ]; then
199             md_size=10240
200         else
201             md_size=`eval echo \\$md_size_$1`
202         fi
203         log create_md $1 with size $md_size
204         mount_md $md_size /$1
205         /bin/chmod 755 /$1
206         eval md_created_$1=created
207 }
208
209 # DEBUGGING
210 #
211 # set -v
212
213 # Figure out our interface and IP.
214 #
215 bootp_ifc=""
216 bootp_ipa=""
217 bootp_ipbca=""
218 class=""
219 if [ ${dlv:=0} -ne 0 ] ; then
220         iflist=`ifconfig -l`
221         for i in ${iflist} ; do
222             set -- `ifconfig ${i}`
223             while [ $# -ge 1 ] ; do
224                 if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
225                     bootp_ifc=${i} ; bootp_ipa=${2} ; shift
226                 fi
227                 if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
228                     bootp_ipbca=$2; shift
229                 fi
230                 shift
231             done
232             if [ "${bootp_ifc}" != "" ] ; then
233                 break
234             fi
235         done
236         # Get the values passed with the T134 bootp cookie.
237         class="`/sbin/sysctl -qn kern.bootp_cookie`"
238
239         echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca} ${class}"
240 fi
241
242 log Figure out our NFS root path
243 #
244 set -- `mount -t nfs`
245 while [ $# -ge 1 ] ; do
246     if [ "$2" = "on" -a "$3" = "/" ]; then
247         nfsroot="$1"
248         break
249     fi
250     shift
251 done
252
253 # The list of directories with template files
254 templates="base default"
255 if [ -n "${bootp_ipbca}" ]; then
256         templates="${templates} ${bootp_ipbca} bcast/${bootp_ipbca}"
257 fi
258 if [ -n "${class}" ]; then
259         templates="${templates} ${class}"
260 fi
261 if [ -n "${bootp_ipa}" ]; then
262         templates="${templates} ${bootp_ipa} ip/${bootp_ipa}"
263 fi
264
265 # If /conf/diskless_remount exists, remount all of /conf.
266 handle_remount /conf
267
268 # Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca},
269 # and /conf/${bootp_ipa}.  For each subdirectory found within these
270 # directories:
271 #
272 # - calculate memory filesystem sizes.  If the subdirectory (prior to
273 #   NFS remounting) contains the file 'md_size', the contents specified
274 #   in 512 byte sectors will be used to size the memory filesystem.  Otherwise
275 #   8192 sectors (4MB) is used.
276 #
277 # - handle NFS remounts.  If the subdirectory contains the file
278 #   diskless_remount, the contents of the file is NFS mounted over
279 #   the directory.  For example /conf/base/etc/diskless_remount
280 #   might contain 'myserver:/etc'.  NFS remounts allow you to avoid
281 #   having to dup your system directories in /conf.  Your server must
282 #   be sure to export those filesystems -alldirs, however.
283 #   If the diskless_remount file contains a string beginning with a
284 #   '/' it is assumed that the local nfsroot should be prepended to
285 #   it before attemping to the remount.  This allows the root to be
286 #   relocated without needing to change the remount files.
287 #
288 log "templates are ${templates}"
289 for i in ${templates} ; do
290     for j in /conf/$i/* ; do
291         [ -d $j ] || continue
292
293         # memory filesystem size specification
294         subdir=${j##*/}
295         [ -f $j/md_size ] && eval md_size_$subdir=`cat $j/md_size`
296
297         # remount. Beware, the command is in the file itself!
298         if [ -f $j/remount ]; then
299             nfspt=`/bin/cat $j/remount`
300             $nfspt $j
301             chkerr $? "$nfspt $j"
302             to_umount="$j ${to_umount}" # XXX hope it is really a mount!
303         fi
304
305         # NFS remount
306         handle_remount $j
307     done
308 done
309
310 # - Create all required MFS filesystems and populate them from
311 #   our templates.  Support both a direct template and a dir.cpio.gz
312 #   archive.  Support dir.remove files containing a list of relative
313 #   paths to remove.
314 #
315 # The dir.cpio.gz form is there to make the copy process more efficient,
316 # so if the cpio archive is present, it prevents the files from dir/
317 # from being copied.
318
319 for i in ${templates} ; do
320     for j in /conf/$i/* ; do
321         subdir=${j##*/}
322         if [ -d $j -a ! -f $j.cpio.gz  ]; then
323             create_md $subdir
324             cp -Rp $j/ /$subdir
325         fi
326     done
327     for j in /conf/$i/*.cpio.gz ; do
328         subdir=${j%*.cpio.gz}
329         subdir=${subdir##*/}
330         if [ -f $j ]; then
331             create_md $subdir
332             echo "Loading /$subdir from cpio archive $j"
333             (cd / ; /rescue/tar -xpf $j)
334         fi
335     done
336     for j in /conf/$i/*.remove ; do
337         subdir=${j%*.remove}
338         subdir=${subdir##*/}
339         if [ -f $j ]; then
340             # doubly sure it is a memory disk before rm -rf'ing
341             create_md $subdir
342             (cd /$subdir; rm -rf `/bin/cat $j`)
343         fi
344     done
345 done
346
347 # umount partitions used to fill the memory filesystems
348 [ -n "${to_umount}" ] && umount $to_umount