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