rc.initdiskless revision 126787
1100280Sgordon#!/bin/sh
2100280Sgordon#
3118638Sfjoe# Copyright (c) 1999  Matt Dillon
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 126787 2004-03-09 23:06:50Z phk $
2851231Ssheldonh#
29100280Sgordon# PROVIDE: initdiskless
30126744Spjd# KEYWORD: FreeBSD nojail
31100280Sgordon
32121067Sdougb
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#
52126787Sphk# If a subdirectory contains the file 'remount' the contents of the file
53126787Sphk# is a mount command used to remount the subdirectory prior to it being
54126787Sphk# copied.  An example contents could be: "mount -o ro /dev/ad0s3".  Note
55126787Sphk# that the directory to be mounted on is supplied by this script.
56126787Sphk#
57108191Sdillon# If a subdirectory contains the file 'diskless_remount' the contents of
58108191Sdillon# the file is used to remount the subdirectory prior to it being copied to
59108191Sdillon# the memory filesystem.  For example, if /conf/base/etc/diskless_remount
60108191Sdillon# contains the string 'my.server.com:/etc' then my.server.com:/etc will be
61108191Sdillon# mounted in place of the subdirectory.  This allows you to avoid making
62126787Sphk# duplicates of system directories in /conf.  Special processing is done
63126787Sphk# to allow specifications relative to the root filesystem.
64108191Sdillon#
65108191Sdillon# If a subdirectory contains the file 'md_size', the contents of the
66108191Sdillon# file is used to determine the size of the memory filesystem, in 512
67121014Skris# byte sectors.  The default is 10240 (5MB).  You only have to specify an
68108191Sdillon# md_size if the default doesn't work for you (i.e. if it is too big or
69121014Skris# too small).  For example, /conf/base/etc/md_size might contain '16384'.
70108191Sdillon#
71108191Sdillon# If /conf/<special_dir>/SUBDIR.cpio.gz exists, the file is cpio'd into
72108191Sdillon# the specified /SUBDIR (and a memory filesystem is created for /SUBDIR
73108191Sdillon# if necessary).
74108191Sdillon#
75108191Sdillon# If /conf/<special_dir>/SUBDIR.remove exists, the file contains a list
76108191Sdillon# of paths which are rm -rf'd relative to /SUBDIR.
77108191Sdillon#
78108191Sdillon# You will almost universally want to create a /conf/base/etc containing
79108191Sdillon# a diskless_remount and possibly an md_size file.  You will then almost
80108191Sdillon# universally want to override rc.conf, rc.local, and fstab by creating
81108191Sdillon# /conf/default/etc/{rc.conf,rc.local,fstab}.  Your fstab should be sure
82108191Sdillon# to mount a /usr... typically an NFS readonly /usr.
83108191Sdillon#
84121066Sdougb# NOTE!  /etc/rc.d/diskless will create /var, /tmp, and /dev.
85121066Sdougb# Those filesystems should not be specified in /conf.  At least not yet.
86108191Sdillon
87100280Sgordondlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
88126787Sphk[ ${dlv:=0} -eq 0 ] && [ ! -f /etc/diskless ] && exit 0
89100280Sgordon
9043803Sdillon# chkerr:
9143803Sdillon#
9243803Sdillon# Routine to check for error
9343803Sdillon#
9443803Sdillon#	checks error code and drops into shell on failure.
9543803Sdillon#	if shell exits, terminates script as well as /etc/rc.
9651231Ssheldonh#
97108191Sdillonchkerr() {
98108191Sdillon    case $1 in
99108191Sdillon    0)
100108191Sdillon	;;
101108191Sdillon    *)
102108191Sdillon	echo "$2 failed: dropping into /bin/sh"
103108191Sdillon	/bin/sh
104108191Sdillon	# RESUME
105108191Sdillon	;;
106108191Sdillon    esac
10743803Sdillon}
10843803Sdillon
109108191Sdillon# Create a generic memory disk
110108191Sdillon#
111108191Sdillonmount_md() {
112108191Sdillon    /sbin/mdmfs -i 4096 -s $1 -M md $2
11375931Simp}
11475931Simp
115108191Sdillon# Create the memory filesystem if it has not already been created
116108191Sdillon#
117108191Sdilloncreate_md() {
118108191Sdillon    if [ "x`eval echo \\$md_created_$1`" = "x" ]; then
119110942Sjhay	if [ "x`eval echo \\$md_size_$1`" = "x" ]; then
120121014Skris	    md_size=10240
121108191Sdillon	else
122108191Sdillon	    md_size=`eval echo \\$md_size_$1`
123108191Sdillon	fi
124108191Sdillon	mount_md $md_size /$1
125108191Sdillon	/bin/chmod 755 /$1
126108191Sdillon	eval md_created_$1=created
127108191Sdillon    fi
128108191Sdillon}
129108191Sdillon
13043803Sdillon# DEBUGGING
13143803Sdillon#
13255520Sluigi# set -v
13343803Sdillon
13451231Ssheldonh# Figure out our interface and IP.
13543803Sdillon#
13655520Sluigibootp_ifc=""
13755520Sluigibootp_ipa=""
13855520Sluigibootp_ipbca=""
139126787Sphkif [ ${dlv:=0} -ne 0 ] ; then
140126787Sphk	iflist=`ifconfig -l`
141126787Sphk	for i in ${iflist} ; do
142126787Sphk	    set `ifconfig ${i}`
143126787Sphk	    while [ $# -ge 1 ] ; do
144126787Sphk		if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
145126787Sphk		    bootp_ifc=${i} ; bootp_ipa=${2} ; shift
146126787Sphk		fi
147126787Sphk		if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
148126787Sphk		    bootp_ipbca=$2; shift
149126787Sphk		fi
150126787Sphk		shift
151126787Sphk	    done
152126787Sphk	    if [ "${bootp_ifc}" != "" ] ; then
153126787Sphk		break
154126787Sphk	    fi
155126787Sphk	done
156126787Sphk	echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}"
157126787Sphkfi
15843803Sdillon
159117087Sbrooks# Figure out our NFS root path
160121067Sdougb#
161117087Sbrooksset `mount -t nfs`
162117087Sbrookswhile [ $# -ge 1 ] ; do
163117087Sbrooks    if [ "$2" = "on" -a "$3" = "/" ]; then
164117087Sbrooks	nfsroot="$1"
165117087Sbrooks	break
166117087Sbrooks    fi
167117087Sbrooks    shift
168117087Sbrooksdone
169117087Sbrooks
170108191Sdillon# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca},
171121067Sdougb# and /conf/${bootp_ipa}.  For each subdirectory found within these
172108191Sdillon# directories:
17343803Sdillon#
174108191Sdillon# - calculate memory filesystem sizes.  If the subdirectory (prior to
175108191Sdillon#   NFS remounting) contains the file 'md_size', the contents specified
176108191Sdillon#   in 512 byte sectors will be used to size the memory filesystem.  Otherwise
177108191Sdillon#   8192 sectors (4MB) is used.
178108191Sdillon#
179108191Sdillon# - handle NFS remounts.  If the subdirectory contains the file
180108191Sdillon#   diskless_remount, the contents of the file is NFS mounted over
181108191Sdillon#   the directory.  For example /conf/base/etc/diskless_remount
182108191Sdillon#   might contain 'myserver:/etc'.  NFS remounts allow you to avoid
183108191Sdillon#   having to dup your system directories in /conf.  Your server must
184108191Sdillon#   be sure to export those filesystems -alldirs, however.
185117087Sbrooks#   If the diskless_remount file contains a string beginning with a
186117087Sbrooks#   '/' it is assumed that the local nfsroot should be prepended to
187117087Sbrooks#   it before attemping to the remount.  This allows the root to be
188117087Sbrooks#   relocated without needing to change the remount files.
189108191Sdillon#
190108191Sdillonfor i in base default ${bootp_ipbca} ${bootp_ipa} ; do
191108191Sdillon    for j in /conf/$i/* ; do
192108191Sdillon	# memory filesystem size specification
193108191Sdillon	#
194108191Sdillon	subdir=${j##*/}
195108191Sdillon	if [ -d $j -a -f $j/md_size ]; then
196108191Sdillon	    eval md_size_$subdir=`cat $j/md_size`
197108191Sdillon	fi
19843803Sdillon
199126787Sphk	# remount
200126787Sphk	#
201126787Sphk	if [ -d $j -a -f $j/remount ]; then
202126787Sphk	    nfspt=`/bin/cat $j/remount`
203126787Sphk	    $nfspt $j
204126787Sphk	    chkerr $? "$nfspt $j"
205126787Sphk	fi
206126787Sphk
207108191Sdillon	# NFS remount
208108191Sdillon	#
209108191Sdillon	if [ -d $j -a -f $j/diskless_remount ]; then
210108191Sdillon	    nfspt=`/bin/cat $j/diskless_remount`
211117087Sbrooks	    if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then
212117087Sbrooks		nfspt="${nfsroot}${nfspt}"
213117087Sbrooks	    fi
214108191Sdillon	    mount_nfs $nfspt $j
215108191Sdillon	    chkerr $? "mount_nfs $nfspt $j"
21675746Sbsd	fi
217108191Sdillon    done
21875746Sbsddone
21975746Sbsd
220108191Sdillon# - Create all required MFS filesystems and populate them from
221108191Sdillon#   our templates.  Support both a direct template and a dir.cpio.gz
222108191Sdillon#   archive.  Support dir.remove files containing a list of relative
223108191Sdillon#   paths to remove.
22495280Sobrien#
225108191Sdillon# TODO:
226108191Sdillon#   + find a way to assign a 'group' identifier to a machine
227108191Sdillon#	so we can use group-specific configurations;
22895280Sobrien
229108191Sdillonfor i in base default ${bootp_ipbca} ${bootp_ipa} ; do
230108191Sdillon    for j in /conf/$i/* ; do
231108191Sdillon	subdir=${j##*/}
232108191Sdillon	if [ -d $j ]; then
233108191Sdillon	    create_md $subdir
234108191Sdillon	    cp -Rp $j/* /$subdir
235108191Sdillon	fi
236108191Sdillon    done
237108191Sdillon    for j in /conf/$i/*.cpio.gz ; do
238108191Sdillon	subdir=${j%*.cpio.gz}
239108191Sdillon	subdir=${subdir##*/}
240108191Sdillon	if [ -f $j ]; then
241108191Sdillon	    create_md $subdir
242108191Sdillon	    echo "Loading /$subdir from cpio archive $j"
243108191Sdillon	    (cd / ; /stand/gzip -d < $j | /stand/cpio --extract -d )
244108191Sdillon	fi
245108191Sdillon    done
246108191Sdillon    for j in /conf/$i/*.remove ; do
247108191Sdillon	subdir=${j%*.remove}
248108191Sdillon	subdir=${subdir##*/}
249108191Sdillon	if [ -f $j ]; then
250108191Sdillon	    # doubly sure it is a memory disk before rm -rf'ing
251108191Sdillon	    create_md $subdir
252108191Sdillon	    (cd /$subdir; rm -rf `/bin/cat $j`)
253108191Sdillon	fi
254108191Sdillon    done
255108191Sdillondone
256104334Sdd
257