rc.initdiskless revision 117087
1100280Sgordon#!/bin/sh
2100280Sgordon#
366830Sobrien# Copyright (c) 1999  Matt Dillion
466830Sobrien# All rights reserved.
566830Sobrien#
666830Sobrien# Redistribution and use in source and binary forms, with or without
766830Sobrien# modification, are permitted provided that the following conditions
866830Sobrien# are met:
966830Sobrien# 1. Redistributions of source code must retain the above copyright
1066830Sobrien#    notice, this list of conditions and the following disclaimer.
1166830Sobrien# 2. Redistributions in binary form must reproduce the above copyright
1266830Sobrien#    notice, this list of conditions and the following disclaimer in the
1366830Sobrien#    documentation and/or other materials provided with the distribution.
1466830Sobrien#
1566830Sobrien# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1666830Sobrien# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1766830Sobrien# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1866830Sobrien# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1966830Sobrien# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2066830Sobrien# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2166830Sobrien# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2266830Sobrien# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2366830Sobrien# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2466830Sobrien# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2566830Sobrien# SUCH DAMAGE.
2666830Sobrien#
2751231Ssheldonh# $FreeBSD: head/etc/rc.initdiskless 117087 2003-06-30 21:47:06Z brooks $
2851231Ssheldonh#
29100280Sgordon# PROVIDE: initdiskless
30100280Sgordon# KEYWORD: FreeBSD
31100280Sgordon
32108191Sdillon 
33108191Sdillon# On entry to this script the entire system consists of a read-only root
34108191Sdillon# mounted via NFS.  We use the contents of /conf to create and populate
35108191Sdillon# memory filesystems.  The kernel has run BOOTP and configured an interface
36108191Sdillon# (otherwise it would not have been able to mount the NFS root!)
37108191Sdillon#
38108191Sdillon# The following directories are scanned.  Each sucessive directory overrides
39108191Sdillon# (is merged into) the previous one.
40108191Sdillon#
41108191Sdillon#	/conf/base		universal base
42108191Sdillon#	/conf/default		modified by a secondary universal base
43108191Sdillon#	/conf/${ipba}		modified based on the assigned broadcast IP
44108191Sdillon#	/conf/${ip}		modified based on the machine's assigned IP
45108191Sdillon#
46108191Sdillon# Each of these directories may contain any number of subdirectories which
47108191Sdillon# represent directories in / on the diskless machine.  The existance of
48108191Sdillon# these subdirectories causes this script to create a MEMORY FILESYSTEM for
49108191Sdillon# /<sub_directory_name>.  For example, if /conf/base/etc exists then a
50108191Sdillon# memory filesystem will be created for /etc.
51108191Sdillon#
52108191Sdillon# If a subdirectory contains the file 'diskless_remount' the contents of
53108191Sdillon# the file is used to remount the subdirectory prior to it being copied to
54108191Sdillon# the memory filesystem.  For example, if /conf/base/etc/diskless_remount
55108191Sdillon# contains the string 'my.server.com:/etc' then my.server.com:/etc will be
56108191Sdillon# mounted in place of the subdirectory.  This allows you to avoid making
57108191Sdillon# duplicates of system directories in /conf.
58108191Sdillon#
59108191Sdillon# If a subdirectory contains the file 'md_size', the contents of the
60108191Sdillon# file is used to determine the size of the memory filesystem, in 512
61108191Sdillon# byte sectors.  The default is 8192 (4MB).  You only have to specify an
62108191Sdillon# md_size if the default doesn't work for you (i.e. if it is too big or
63108191Sdillon# too small).  Note that in -current the default is 4096 (2MB).  For
64108191Sdillon# example, /conf/base/etc/md_size might contain '16384'.
65108191Sdillon#
66108191Sdillon# If /conf/<special_dir>/SUBDIR.cpio.gz exists, the file is cpio'd into
67108191Sdillon# the specified /SUBDIR (and a memory filesystem is created for /SUBDIR
68108191Sdillon# if necessary).
69108191Sdillon#
70108191Sdillon# If /conf/<special_dir>/SUBDIR.remove exists, the file contains a list
71108191Sdillon# of paths which are rm -rf'd relative to /SUBDIR.
72108191Sdillon#
73108191Sdillon# You will almost universally want to create a /conf/base/etc containing
74108191Sdillon# a diskless_remount and possibly an md_size file.  You will then almost
75108191Sdillon# universally want to override rc.conf, rc.local, and fstab by creating
76108191Sdillon# /conf/default/etc/{rc.conf,rc.local,fstab}.  Your fstab should be sure
77108191Sdillon# to mount a /usr... typically an NFS readonly /usr.
78108191Sdillon#
79108191Sdillon# NOTE!  rc.diskless2 will create /var, /tmp, and /dev.  Those filesystems
80108191Sdillon# should not be specified in /conf.  At least not yet.
81108191Sdillon
82100280Sgordondlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
83100280Sgordon[ ${dlv:=0} -eq 0 ] && exit 0
84100280Sgordon
8543803Sdillon# chkerr:
8643803Sdillon#
8743803Sdillon# Routine to check for error
8843803Sdillon#
8943803Sdillon#	checks error code and drops into shell on failure.
9043803Sdillon#	if shell exits, terminates script as well as /etc/rc.
9151231Ssheldonh#
92108191Sdillonchkerr() {
93108191Sdillon    case $1 in
94108191Sdillon    0)
95108191Sdillon	;;
96108191Sdillon    *)
97108191Sdillon	echo "$2 failed: dropping into /bin/sh"
98108191Sdillon	/bin/sh
99108191Sdillon	# RESUME
100108191Sdillon	;;
101108191Sdillon    esac
10243803Sdillon}
10343803Sdillon
104108191Sdillon# Create a generic memory disk
105108191Sdillon#
106108191Sdillonmount_md() {
107108191Sdillon    /sbin/mdmfs -i 4096 -s $1 -M md $2
10875931Simp}
10975931Simp
110108191Sdillon# Create the memory filesystem if it has not already been created
111108191Sdillon#
112108191Sdilloncreate_md() {
113108191Sdillon    if [ "x`eval echo \\$md_created_$1`" = "x" ]; then
114110942Sjhay	if [ "x`eval echo \\$md_size_$1`" = "x" ]; then
115108191Sdillon	    md_size=4096
116108191Sdillon	else
117108191Sdillon	    md_size=`eval echo \\$md_size_$1`
118108191Sdillon	fi
119108191Sdillon	mount_md $md_size /$1
120108191Sdillon	/bin/chmod 755 /$1
121108191Sdillon	eval md_created_$1=created
122108191Sdillon    fi
123108191Sdillon}
124108191Sdillon
12543803Sdillon# DEBUGGING
12643803Sdillon#
12755520Sluigi# set -v
12843803Sdillon
12951231Ssheldonh# Figure out our interface and IP.
13043803Sdillon#
13155520Sluigibootp_ifc=""
13255520Sluigibootp_ipa=""
13355520Sluigibootp_ipbca=""
13455520Sluigiiflist=`ifconfig -l`
13555520Sluigifor i in ${iflist} ; do
136108191Sdillon    set `ifconfig ${i}`
137108191Sdillon    while [ $# -ge 1 ] ; do
138108191Sdillon        if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
139108191Sdillon            bootp_ifc=${i} ; bootp_ipa=${2} ; shift
140108191Sdillon        fi
141108191Sdillon        if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
142108191Sdillon            bootp_ipbca=$2; shift
143108191Sdillon        fi
144108191Sdillon        shift
145108191Sdillon    done
146108191Sdillon    if [ "${bootp_ifc}" != "" ] ; then
147108191Sdillon        break
148108191Sdillon    fi
14955520Sluigidone
15055520Sluigiecho "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}"
15143803Sdillon
152117087Sbrooks# Figure out our NFS root path
153117087Sbrooks# 
154117087Sbrooksset `mount -t nfs`
155117087Sbrookswhile [ $# -ge 1 ] ; do
156117087Sbrooks    if [ "$2" = "on" -a "$3" = "/" ]; then
157117087Sbrooks	nfsroot="$1"
158117087Sbrooks	break
159117087Sbrooks    fi
160117087Sbrooks    shift
161117087Sbrooksdone
162117087Sbrooks
163108191Sdillon# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca},
164108191Sdillon# and /conf/${bootp_ipa}.  For each subdirectory found within these 
165108191Sdillon# directories:
16643803Sdillon#
167108191Sdillon# - calculate memory filesystem sizes.  If the subdirectory (prior to
168108191Sdillon#   NFS remounting) contains the file 'md_size', the contents specified
169108191Sdillon#   in 512 byte sectors will be used to size the memory filesystem.  Otherwise
170108191Sdillon#   8192 sectors (4MB) is used.
171108191Sdillon#
172108191Sdillon# - handle NFS remounts.  If the subdirectory contains the file
173108191Sdillon#   diskless_remount, the contents of the file is NFS mounted over
174108191Sdillon#   the directory.  For example /conf/base/etc/diskless_remount
175108191Sdillon#   might contain 'myserver:/etc'.  NFS remounts allow you to avoid
176108191Sdillon#   having to dup your system directories in /conf.  Your server must
177108191Sdillon#   be sure to export those filesystems -alldirs, however.
178117087Sbrooks#   If the diskless_remount file contains a string beginning with a
179117087Sbrooks#   '/' it is assumed that the local nfsroot should be prepended to
180117087Sbrooks#   it before attemping to the remount.  This allows the root to be
181117087Sbrooks#   relocated without needing to change the remount files.
182108191Sdillon#
183108191Sdillonfor i in base default ${bootp_ipbca} ${bootp_ipa} ; do
184108191Sdillon    for j in /conf/$i/* ; do
185108191Sdillon	# memory filesystem size specification
186108191Sdillon	#
187108191Sdillon	subdir=${j##*/}
188108191Sdillon	if [ -d $j -a -f $j/md_size ]; then
189108191Sdillon	    eval md_size_$subdir=`cat $j/md_size`
190108191Sdillon	fi
19143803Sdillon
192108191Sdillon	# NFS remount
193108191Sdillon	#
194108191Sdillon	if [ -d $j -a -f $j/diskless_remount ]; then
195108191Sdillon	    nfspt=`/bin/cat $j/diskless_remount`
196117087Sbrooks	    if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then
197117087Sbrooks		nfspt="${nfsroot}${nfspt}"
198117087Sbrooks	    fi
199108191Sdillon	    mount_nfs $nfspt $j
200108191Sdillon	    chkerr $? "mount_nfs $nfspt $j"
20175746Sbsd	fi
202108191Sdillon    done
20375746Sbsddone
20475746Sbsd
205108191Sdillon# - Create all required MFS filesystems and populate them from
206108191Sdillon#   our templates.  Support both a direct template and a dir.cpio.gz
207108191Sdillon#   archive.  Support dir.remove files containing a list of relative
208108191Sdillon#   paths to remove.
20995280Sobrien#
210108191Sdillon# TODO:
211108191Sdillon#   + find a way to assign a 'group' identifier to a machine
212108191Sdillon#	so we can use group-specific configurations;
21395280Sobrien
214108191Sdillonfor i in base default ${bootp_ipbca} ${bootp_ipa} ; do
215108191Sdillon    for j in /conf/$i/* ; do
216108191Sdillon	subdir=${j##*/}
217108191Sdillon	if [ -d $j ]; then
218108191Sdillon	    create_md $subdir
219108191Sdillon	    cp -Rp $j/* /$subdir
220108191Sdillon	fi
221108191Sdillon    done
222108191Sdillon    for j in /conf/$i/*.cpio.gz ; do
223108191Sdillon	subdir=${j%*.cpio.gz}
224108191Sdillon	subdir=${subdir##*/}
225108191Sdillon	if [ -f $j ]; then
226108191Sdillon	    create_md $subdir
227108191Sdillon	    echo "Loading /$subdir from cpio archive $j"
228108191Sdillon	    (cd / ; /stand/gzip -d < $j | /stand/cpio --extract -d )
229108191Sdillon	fi
230108191Sdillon    done
231108191Sdillon    for j in /conf/$i/*.remove ; do
232108191Sdillon	subdir=${j%*.remove}
233108191Sdillon	subdir=${subdir##*/}
234108191Sdillon	if [ -f $j ]; then
235108191Sdillon	    # doubly sure it is a memory disk before rm -rf'ing
236108191Sdillon	    create_md $subdir
237108191Sdillon	    (cd /$subdir; rm -rf `/bin/cat $j`)
238108191Sdillon	fi
239108191Sdillon    done
240108191Sdillondone
241104334Sdd
242