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