rc.initdiskless revision 121066
1127664Sbms#!/bin/sh
2127664Sbms#
3127664Sbms# Copyright (c) 1999  Matt Dillon
4127664Sbms# All rights reserved.
5127664Sbms#
6127664Sbms# Redistribution and use in source and binary forms, with or without
7127664Sbms# modification, are permitted provided that the following conditions
8127664Sbms# are met:
9127664Sbms# 1. Redistributions of source code must retain the above copyright
10127664Sbms#    notice, this list of conditions and the following disclaimer.
11127664Sbms# 2. Redistributions in binary form must reproduce the above copyright
12127664Sbms#    notice, this list of conditions and the following disclaimer in the
13127664Sbms#    documentation and/or other materials provided with the distribution.
14127664Sbms#
15127664Sbms# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16127664Sbms# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17127664Sbms# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18127664Sbms# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19127664Sbms# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20127664Sbms# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21127664Sbms# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22127664Sbms# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23127664Sbms# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24127664Sbms# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25127664Sbms# SUCH DAMAGE.
26127664Sbms#
27127664Sbms# $FreeBSD: head/etc/rc.initdiskless 121066 2003-10-13 08:18:05Z dougb $
28127664Sbms#
29127664Sbms# PROVIDE: initdiskless
30127664Sbms# KEYWORD: FreeBSD
31127664Sbms
32127664Sbms 
33127664Sbms# On entry to this script the entire system consists of a read-only root
34127664Sbms# mounted via NFS.  We use the contents of /conf to create and populate
35127664Sbms# memory filesystems.  The kernel has run BOOTP and configured an interface
36127664Sbms# (otherwise it would not have been able to mount the NFS root!)
37214518Srpaulo#
38127664Sbms# The following directories are scanned.  Each sucessive directory overrides
39127664Sbms# (is merged into) the previous one.
40127664Sbms#
41127664Sbms#	/conf/base		universal base
42127664Sbms#	/conf/default		modified by a secondary universal base
43127664Sbms#	/conf/${ipba}		modified based on the assigned broadcast IP
44127664Sbms#	/conf/${ip}		modified based on the machine's assigned IP
45127664Sbms#
46127664Sbms# Each of these directories may contain any number of subdirectories which
47127664Sbms# represent directories in / on the diskless machine.  The existance of
48127664Sbms# these subdirectories causes this script to create a MEMORY FILESYSTEM for
49127664Sbms# /<sub_directory_name>.  For example, if /conf/base/etc exists then a
50147894Ssam# memory filesystem will be created for /etc.
51127664Sbms#
52127664Sbms# If a subdirectory contains the file 'diskless_remount' the contents of
53127664Sbms# the file is used to remount the subdirectory prior to it being copied to
54147894Ssam# the memory filesystem.  For example, if /conf/base/etc/diskless_remount
55127664Sbms# contains the string 'my.server.com:/etc' then my.server.com:/etc will be
56127664Sbms# mounted in place of the subdirectory.  This allows you to avoid making
57127664Sbms# duplicates of system directories in /conf.
58127664Sbms#
59127664Sbms# If a subdirectory contains the file 'md_size', the contents of the
60127664Sbms# file is used to determine the size of the memory filesystem, in 512
61127664Sbms# byte sectors.  The default is 10240 (5MB).  You only have to specify an
62127664Sbms# md_size if the default doesn't work for you (i.e. if it is too big or
63146768Ssam# too small).  For example, /conf/base/etc/md_size might contain '16384'.
64235426Sdelphij#
65235426Sdelphij# If /conf/<special_dir>/SUBDIR.cpio.gz exists, the file is cpio'd into
66235426Sdelphij# the specified /SUBDIR (and a memory filesystem is created for /SUBDIR
67235426Sdelphij# if necessary).
68235426Sdelphij#
69172677Smlaier# If /conf/<special_dir>/SUBDIR.remove exists, the file contains a list
70214518Srpaulo# of paths which are rm -rf'd relative to /SUBDIR.
71214518Srpaulo#
72235426Sdelphij# You will almost universally want to create a /conf/base/etc containing
73214518Srpaulo# a diskless_remount and possibly an md_size file.  You will then almost
74214518Srpaulo# universally want to override rc.conf, rc.local, and fstab by creating
75214518Srpaulo# /conf/default/etc/{rc.conf,rc.local,fstab}.  Your fstab should be sure
76235426Sdelphij# to mount a /usr... typically an NFS readonly /usr.
77235426Sdelphij#
78235426Sdelphij# NOTE!  /etc/rc.d/diskless will create /var, /tmp, and /dev.
79146768Ssam# Those filesystems should not be specified in /conf.  At least not yet.
80127664Sbms
81127664Sbmsdlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
82127664Sbms[ ${dlv:=0} -eq 0 ] && exit 0
83127664Sbms
84127664Sbms# chkerr:
85127664Sbms#
86127664Sbms# Routine to check for error
87127664Sbms#
88127664Sbms#	checks error code and drops into shell on failure.
89127664Sbms#	if shell exits, terminates script as well as /etc/rc.
90127664Sbms#
91127664Sbmschkerr() {
92127664Sbms    case $1 in
93127664Sbms    0)
94127664Sbms	;;
95127664Sbms    *)
96127664Sbms	echo "$2 failed: dropping into /bin/sh"
97127664Sbms	/bin/sh
98127664Sbms	# RESUME
99127664Sbms	;;
100127664Sbms    esac
101127664Sbms}
102127664Sbms
103127664Sbms# Create a generic memory disk
104127664Sbms#
105127664Sbmsmount_md() {
106127664Sbms    /sbin/mdmfs -i 4096 -s $1 -M md $2
107127664Sbms}
108127664Sbms
109127664Sbms# Create the memory filesystem if it has not already been created
110127664Sbms#
111127664Sbmscreate_md() {
112127664Sbms    if [ "x`eval echo \\$md_created_$1`" = "x" ]; then
113127664Sbms	if [ "x`eval echo \\$md_size_$1`" = "x" ]; then
114127664Sbms	    md_size=10240
115127664Sbms	else
116127664Sbms	    md_size=`eval echo \\$md_size_$1`
117127664Sbms	fi
118127664Sbms	mount_md $md_size /$1
119127664Sbms	/bin/chmod 755 /$1
120127664Sbms	eval md_created_$1=created
121127664Sbms    fi
122127664Sbms}
123146768Ssam
124146768Ssam# DEBUGGING
125146768Ssam#
126146768Ssam# set -v
127146768Ssam
128127664Sbms# Figure out our interface and IP.
129127664Sbms#
130127664Sbmsbootp_ifc=""
131127664Sbmsbootp_ipa=""
132127664Sbmsbootp_ipbca=""
133127664Sbmsiflist=`ifconfig -l`
134127664Sbmsfor i in ${iflist} ; do
135127664Sbms    set `ifconfig ${i}`
136127664Sbms    while [ $# -ge 1 ] ; do
137127664Sbms        if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
138127664Sbms            bootp_ifc=${i} ; bootp_ipa=${2} ; shift
139127664Sbms        fi
140127664Sbms        if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
141127664Sbms            bootp_ipbca=$2; shift
142127664Sbms        fi
143127664Sbms        shift
144127664Sbms    done
145127664Sbms    if [ "${bootp_ifc}" != "" ] ; then
146251129Sdelphij        break
147127664Sbms    fi
148127664Sbmsdone
149127664Sbmsecho "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}"
150127664Sbms
151127664Sbms# Figure out our NFS root path
152127664Sbms# 
153147894Ssamset `mount -t nfs`
154127664Sbmswhile [ $# -ge 1 ] ; do
155127664Sbms    if [ "$2" = "on" -a "$3" = "/" ]; then
156127664Sbms	nfsroot="$1"
157127664Sbms	break
158127664Sbms    fi
159127664Sbms    shift
160127664Sbmsdone
161127664Sbms
162127664Sbms# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca},
163127664Sbms# and /conf/${bootp_ipa}.  For each subdirectory found within these 
164127664Sbms# directories:
165127664Sbms#
166127664Sbms# - calculate memory filesystem sizes.  If the subdirectory (prior to
167127664Sbms#   NFS remounting) contains the file 'md_size', the contents specified
168127664Sbms#   in 512 byte sectors will be used to size the memory filesystem.  Otherwise
169127664Sbms#   8192 sectors (4MB) is used.
170127664Sbms#
171127664Sbms# - handle NFS remounts.  If the subdirectory contains the file
172127664Sbms#   diskless_remount, the contents of the file is NFS mounted over
173127664Sbms#   the directory.  For example /conf/base/etc/diskless_remount
174127664Sbms#   might contain 'myserver:/etc'.  NFS remounts allow you to avoid
175127664Sbms#   having to dup your system directories in /conf.  Your server must
176127664Sbms#   be sure to export those filesystems -alldirs, however.
177127664Sbms#   If the diskless_remount file contains a string beginning with a
178127664Sbms#   '/' it is assumed that the local nfsroot should be prepended to
179127664Sbms#   it before attemping to the remount.  This allows the root to be
180127664Sbms#   relocated without needing to change the remount files.
181127664Sbms#
182127664Sbmsfor i in base default ${bootp_ipbca} ${bootp_ipa} ; do
183127664Sbms    for j in /conf/$i/* ; do
184127664Sbms	# memory filesystem size specification
185127664Sbms	#
186127664Sbms	subdir=${j##*/}
187127664Sbms	if [ -d $j -a -f $j/md_size ]; then
188127664Sbms	    eval md_size_$subdir=`cat $j/md_size`
189127664Sbms	fi
190127664Sbms
191127664Sbms	# NFS remount
192127664Sbms	#
193127664Sbms	if [ -d $j -a -f $j/diskless_remount ]; then
194127664Sbms	    nfspt=`/bin/cat $j/diskless_remount`
195127664Sbms	    if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then
196127664Sbms		nfspt="${nfsroot}${nfspt}"
197127664Sbms	    fi
198127664Sbms	    mount_nfs $nfspt $j
199127664Sbms	    chkerr $? "mount_nfs $nfspt $j"
200127664Sbms	fi
201127664Sbms    done
202127664Sbmsdone
203127664Sbms
204127664Sbms# - Create all required MFS filesystems and populate them from
205127664Sbms#   our templates.  Support both a direct template and a dir.cpio.gz
206127664Sbms#   archive.  Support dir.remove files containing a list of relative
207127664Sbms#   paths to remove.
208127664Sbms#
209127664Sbms# TODO:
210127664Sbms#   + find a way to assign a 'group' identifier to a machine
211127664Sbms#	so we can use group-specific configurations;
212127664Sbms
213127664Sbmsfor i in base default ${bootp_ipbca} ${bootp_ipa} ; do
214127664Sbms    for j in /conf/$i/* ; do
215127664Sbms	subdir=${j##*/}
216127664Sbms	if [ -d $j ]; then
217127664Sbms	    create_md $subdir
218127664Sbms	    cp -Rp $j/* /$subdir
219127664Sbms	fi
220127664Sbms    done
221127664Sbms    for j in /conf/$i/*.cpio.gz ; do
222127664Sbms	subdir=${j%*.cpio.gz}
223127664Sbms	subdir=${subdir##*/}
224127664Sbms	if [ -f $j ]; then
225127664Sbms	    create_md $subdir
226127664Sbms	    echo "Loading /$subdir from cpio archive $j"
227127664Sbms	    (cd / ; /stand/gzip -d < $j | /stand/cpio --extract -d )
228127664Sbms	fi
229127664Sbms    done
230127664Sbms    for j in /conf/$i/*.remove ; do
231127664Sbms	subdir=${j%*.remove}
232147894Ssam	subdir=${subdir##*/}
233147894Ssam	if [ -f $j ]; then
234147894Ssam	    # doubly sure it is a memory disk before rm -rf'ing
235147894Ssam	    create_md $subdir
236147894Ssam	    (cd /$subdir; rm -rf `/bin/cat $j`)
237147894Ssam	fi
238147894Ssam    done
239147894Ssamdone
240147894Ssam
241147894Ssam