]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/rc.initdiskless
This commit was generated by cvs2svn to compensate for changes in r135923,
[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 # REQUIRE: preseedrandom
30 # PROVIDE: initdiskless
31 # KEYWORD: FreeBSD nojail
32 # BEFORE: ipfw
33
34
35 # On entry to this script the entire system consists of a read-only root
36 # mounted via NFS. The kernel has run BOOTP and configured an interface
37 # (otherwise it would not have been able to mount the NFS root!)
38 #
39 # We use the contents of /conf to create and populate memory filesystems
40 # that are mounted on top of this root to implement the writable
41 # (and host-specific) parts of the root filesystem, and other volatile
42 # filesystems.
43 #
44 # The hierarchy in /conf has the form /conf/T/M/ where M are directories
45 # for which memory filesystems will be created and filled,
46 # and T is one of the "template" directories below:
47 #
48 #  base         universal base, typically a replica of the original root;
49 #  default      secondary universal base, typically overriding some
50 #               of the files in the original root;
51 #  ${ipba}      where ${ipba} is the assigned broadcast IP address
52 #  ${class}     where ${class} is a list of directories supplied by
53 #               bootp/dhcp through the T134 option.
54 #               ${ipba} and ${class} are typicall used to configure features
55 #               for group of diskless clients, or even individual features;
56 #  ${ip}        where ${ip} is the machine's assigned IP address, typically
57 #               used to set host-specific features;
58 #
59 # Template directories are scanned in the order they are listed above,
60 # with each sucessive directory overriding (merged into) the previous one;
61 # non-existing directories are ignored.
62 #
63 # The existence of a directory /conf/T/M causes this script to create a
64 # memory filesystem mounted as /M on the client.
65 #
66 # Some files in /conf have special meaning, namely:
67 #
68 # Filename      Action
69 # ----------------------------------------------------------------
70 # /conf/T/M/remount
71 #               The contents of the file is a mount command. E.g. if
72 #               /conf/1.2.3.4/foo/remount contains "mount -o ro /dev/ad0s3",
73 #               then /dev/ad0s3 will be be mounted on /conf/1.2.3.4/foo/
74 #
75 # /conf/T/M/diskless_remount
76 #               The contents of the file points to an NFS filesystem. E.g. if
77 #               /conf/base/etc/diskless_remount contains "foo.com:/etc",
78 #               then foo.com:/etc will be be mounted on /conf/base/etc/
79 #               If the file contains a pathname starting with "/", then
80 #               the root path is prepended to it; this allows relocation of
81 #               the root filesystem withouth changing configuration files.
82 #
83 # /conf/T/M/md_size
84 #               The contents of the file specifies the size of the memory
85 #               filesystem to be created, in 512 byte blocks.
86 #               The default size is 10240 blocks (5MB). E.g. if
87 #               /conf/base/etc/md_size contains "30000" then a 15MB MFS
88 #               will be created. In case of multiple entries for the same
89 #               directory M, the last one in the scanning order is used.
90 #               NOTE: If you only need to create a memory filesystem but not
91 #               initialize it from a template, it is preferrable to specify
92 #               it in fstab e.g. as  "md /tmp mfs -s=30m,rw 0 0"
93 #
94 # /conf/T/SUBDIR.cpio.gz
95 #               The file is cpio'd into /SUBDIR (and a memory filesystem is
96 #               created for /SUBDIR if necessary). The presence of this file
97 #               prevents the copy from /conf/T/SUBDIR/
98 #
99 # /conf/T/SUBDIR.remove
100 #               The list of paths contained in the file are rm -rf'd
101 #               relative to /SUBDIR.
102 #
103 # You will almost universally want to create the following files under /conf
104 #
105 # File                          Content
106 # ----------------------------  ------------------------------------------
107 # /conf/base/etc/md_size        size of /etc filesystem
108 # /conf/base/diskless_remount   "/etc"
109 # /conf/default/etc/rc.conf     generic diskless config parameters
110 # /conf/default/etc/fstab       generic diskless fstab e.g. like this
111 #
112 #       foo:/root_part          /       nfs     ro              0 0
113 #       foo:/usr_part           /usr    nfs     ro              0 0
114 #       foo:/home_part          /home   nfs     rw              0 0
115 #       md                      /tmp    mfs     -s=30m,rw       0 0
116 #       md                      /var    mfs     -s=30m,rw       0 0
117 #       proc                    /proc   procfs  rw              0 0
118 #
119 # plus, possibly, overrides for password files etc.
120 #
121 # NOTE!  /var, /tmp, and /dev will be typically created elsewhere, e.g.
122 # as entries in the fstab as above.
123 # Those filesystems should not be specified in /conf.
124 #
125 # (end of documentation, now get to the real code)
126
127 dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
128 [ ${dlv:=0} -eq 0 ] && [ ! -f /etc/diskless ] && exit 0
129
130 # chkerr:
131 #
132 # Routine to check for error
133 #
134 #       checks error code and drops into shell on failure.
135 #       if shell exits, terminates script as well as /etc/rc.
136 #
137 chkerr() {
138     case $1 in
139     0)
140         ;;
141     *)
142         echo "$2 failed: dropping into /bin/sh"
143         /bin/sh
144         # RESUME
145         ;;
146     esac
147 }
148
149 # Create a generic memory disk
150 #
151 mount_md() {
152     /sbin/mdmfs -i 4096 -s $1 -M md $2
153 }
154
155 # Create the memory filesystem if it has not already been created
156 #
157 create_md() {
158     if [ "x`eval echo \\$md_created_$1`" = "x" ]; then
159         if [ "x`eval echo \\$md_size_$1`" = "x" ]; then
160             md_size=10240
161         else
162             md_size=`eval echo \\$md_size_$1`
163         fi
164         mount_md $md_size /$1
165         /bin/chmod 755 /$1
166         eval md_created_$1=created
167     fi
168 }
169
170 # DEBUGGING
171 #
172 # set -v
173
174 # Figure out our interface and IP.
175 #
176 bootp_ifc=""
177 bootp_ipa=""
178 bootp_ipbca=""
179 if [ ${dlv:=0} -ne 0 ] ; then
180         iflist=`ifconfig -l`
181         for i in ${iflist} ; do
182             set -- `ifconfig ${i}`
183             while [ $# -ge 1 ] ; do
184                 if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
185                     bootp_ifc=${i} ; bootp_ipa=${2} ; shift
186                 fi
187                 if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
188                     bootp_ipbca=$2; shift
189                 fi
190                 shift
191             done
192             if [ "${bootp_ifc}" != "" ] ; then
193                 break
194             fi
195         done
196         # Insert the directories passed with the T134 bootp cookie
197         # in the list of paths used for templates.
198         i="`/sbin/sysctl -n kern.bootp_cookie`"
199         [ "${i}" != "" ] && bootp_ipbca="${bootp_ipbca} ${i}"
200
201         echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}"
202 fi
203
204 # Figure out our NFS root path
205 #
206 set -- `mount -t nfs`
207 while [ $# -ge 1 ] ; do
208     if [ "$2" = "on" -a "$3" = "/" ]; then
209         nfsroot="$1"
210         break
211     fi
212     shift
213 done
214
215 # The list of directories with template files
216 templates="base default ${bootp_ipbca} ${bootp_ipa}"
217
218 # The list of filesystems to umount after the copy
219 to_umount=""
220
221 # If /conf/diskless_remount exists, remount all of /conf.  This allows
222 # multiple roots to share the same conf files.
223 if [ -d /conf -a -f /conf/diskless_remount ]; then
224     nfspt=`/bin/cat /conf/diskless_remount`
225     if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then
226         nfspt="${nfsroot}${nfspt}"
227     fi
228     mount_nfs $nfspt /conf
229     chkerr $? "mount_nfs $nfspt /conf"
230     to_umount="/conf"
231 fi
232
233 # Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca},
234 # and /conf/${bootp_ipa}.  For each subdirectory found within these
235 # directories:
236 #
237 # - calculate memory filesystem sizes.  If the subdirectory (prior to
238 #   NFS remounting) contains the file 'md_size', the contents specified
239 #   in 512 byte sectors will be used to size the memory filesystem.  Otherwise
240 #   8192 sectors (4MB) is used.
241 #
242 # - handle NFS remounts.  If the subdirectory contains the file
243 #   diskless_remount, the contents of the file is NFS mounted over
244 #   the directory.  For example /conf/base/etc/diskless_remount
245 #   might contain 'myserver:/etc'.  NFS remounts allow you to avoid
246 #   having to dup your system directories in /conf.  Your server must
247 #   be sure to export those filesystems -alldirs, however.
248 #   If the diskless_remount file contains a string beginning with a
249 #   '/' it is assumed that the local nfsroot should be prepended to
250 #   it before attemping to the remount.  This allows the root to be
251 #   relocated without needing to change the remount files.
252 #
253 for i in ${templates} ; do
254     for j in /conf/$i/* ; do
255         # memory filesystem size specification
256         #
257         subdir=${j##*/}
258         if [ -d $j -a -f $j/md_size ]; then
259             eval md_size_$subdir=`cat $j/md_size`
260         fi
261
262         # remount
263         #
264         if [ -d $j -a -f $j/remount ]; then
265             nfspt=`/bin/cat $j/remount`
266             $nfspt $j
267             chkerr $? "$nfspt $j"
268             to_umount="${to_umount} $j" # XXX hope it is really a mount!
269         fi
270
271         # NFS remount
272         #
273         if [ -d $j -a -f $j/diskless_remount ]; then
274             nfspt=`/bin/cat $j/diskless_remount`
275             if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then
276                 nfspt="${nfsroot}${nfspt}"
277             fi
278             mount_nfs $nfspt $j
279             chkerr $? "mount_nfs $nfspt $j"
280             to_umount="${to_umount} $j"
281         fi
282     done
283 done
284
285 # - Create all required MFS filesystems and populate them from
286 #   our templates.  Support both a direct template and a dir.cpio.gz
287 #   archive.  Support dir.remove files containing a list of relative
288 #   paths to remove.
289 #
290 # The dir.cpio.gz form is there to make the copy process more efficient,
291 # so if the cpio archive is present, it prevents the files from dir/
292 # from being copied.
293
294 for i in ${templates} ; do
295     for j in /conf/$i/* ; do
296         subdir=${j##*/}
297         if [ -d $j -a ! -f $j.cpio.gz  ]; then
298             create_md $subdir
299             cp -Rp $j/* /$subdir
300         fi
301     done
302     for j in /conf/$i/*.cpio.gz ; do
303         subdir=${j%*.cpio.gz}
304         subdir=${subdir##*/}
305         if [ -f $j ]; then
306             create_md $subdir
307             echo "Loading /$subdir from cpio archive $j"
308             (cd / ; /stand/gzip -d < $j | /stand/cpio --extract -d )
309         fi
310     done
311     for j in /conf/$i/*.remove ; do
312         subdir=${j%*.remove}
313         subdir=${subdir##*/}
314         if [ -f $j ]; then
315             # doubly sure it is a memory disk before rm -rf'ing
316             create_md $subdir
317             (cd /$subdir; rm -rf `/bin/cat $j`)
318         fi
319     done
320 done
321
322 # umount partitions used to fill the memory filesystems
323 [ -n "${to_umount}" ] && umount $to_umount