xref: /dragonfly/etc/rc.d/initdiskless (revision 71126e33)
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: src/etc/rc.d/initdiskless,v 1.24 2003/06/30 21:47:06 brooks Exp $
28# $DragonFly: src/etc/rc.d/initdiskless,v 1.6 2004/08/25 01:53:39 dillon Exp $
29#
30# PROVIDE: initdiskless
31# KEYWORD: DragonFly
32
33. /etc/rc.subr
34dummy_rc_command "$1"
35
36# On entry to this script the entire system consists of a read-only root
37# mounted via NFS.  We use the contents of /conf to create and populate
38# memory filesystems.  The kernel has run BOOTP and configured an interface
39# (otherwise it would not have been able to mount the NFS root!)
40#
41# The following directories are scanned.  Each successive directory overrides
42# (is merged into) the previous one.
43#
44#	/conf/base		universal base
45#	/conf/default		modified by a secondary universal base
46#	/conf/${ipba}		modified based on the assigned broadcast IP
47#	/conf/${ip}		modified based on the machine's assigned IP
48#
49# Each of these directories may contain any number of subdirectories which
50# represent directories in / on the diskless machine.  The existance of
51# these subdirectories causes this script to create a MEMORY FILESYSTEM for
52# /<sub_directory_name>.  For example, if /conf/base/etc exists then a
53# memory filesystem will be created for /etc.
54#
55# If a subdirectory contains the file 'diskless_remount' the contents of
56# the file is used to remount the subdirectory prior to it being copied to
57# the memory filesystem.  For example, if /conf/base/etc/diskless_remount
58# contains the string 'my.server.com:/etc' then my.server.com:/etc will be
59# mounted in place of the subdirectory.  This allows you to avoid making
60# duplicates of system directories in /conf.
61#
62# If a subdirectory contains the file 'md_size', the contents of the
63# file is used to determine the size of the memory filesystem, in 512
64# byte sectors.  The default is 8192 (4MB).  You only have to specify an
65# md_size if the default doesn't work for you (i.e. if it is too big or
66# too small).  Note that in -current the default is 4096 (2MB).  For
67# example, /conf/base/etc/md_size might contain '16384'.
68#
69# If /conf/<special_dir>/SUBDIR.cpio.gz exists, the file is cpio'd into
70# the specified /SUBDIR (and a memory filesystem is created for /SUBDIR
71# if necessary).
72#
73# If /conf/<special_dir>/SUBDIR.remove exists, the file contains a list
74# of paths which are rm -rf'd relative to /SUBDIR.
75#
76# You will almost universally want to create a /conf/base/etc containing
77# a diskless_remount and possibly an md_size file.  You will then almost
78# universally want to override rc.conf, rc.local, and fstab by creating
79# /conf/default/etc/{rc.conf,rc.local,fstab}.  Your fstab should be sure
80# to mount a /usr... typically an NFS readonly /usr.
81#
82# NOTE!  rc.diskless2 will create /var, /tmp, and /dev.  Those filesystems
83# should not be specified in /conf.  At least not yet.
84
85dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
86[ ${dlv:=0} -eq 0 ] && exit 0
87
88# chkerr:
89#
90# Routine to check for error
91#
92#	checks error code and drops into shell on failure.
93#	if shell exits, terminates script as well as /etc/rc.
94#
95chkerr() {
96	case $1 in
97	0)
98	;;
99	*)
100	echo "$2 failed: dropping into /bin/sh"
101	/bin/sh
102	# RESUME
103	;;
104	esac
105}
106
107# Create a generic memory disk
108#
109mount_md() {
110	/sbin/mdmfs -i 4096 -s $1 -M md $2
111}
112
113# Create the memory filesystem if it has not already been created
114#
115create_md() {
116	if [ "x`eval echo \\$md_created_$1`" = "x" ]; then
117	if [ "x`eval echo \\$md_size_$1`" = "x" ]; then
118	    md_size=4096
119	else
120	    md_size=`eval echo \\$md_size_$1`
121	fi
122	mount_md $md_size /$1
123	/bin/chmod 755 /$1
124	eval md_created_$1=created
125    fi
126}
127
128# DEBUGGING
129#
130# set -v
131
132# Figure out our interface and IP.
133#
134bootp_ifc=""
135bootp_ipa=""
136bootp_ipbca=""
137iflist=`ifconfig -l`
138for i in ${iflist} ; do
139	set `ifconfig ${i}`
140	while [ $# -ge 1 ] ; do
141		if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
142			bootp_ifc=${i} ; bootp_ipa=${2} ; shift
143		fi
144		if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
145			bootp_ipbca=$2; shift
146		fi
147		shift
148	done
149	if [ "${bootp_ifc}" != "" ] ; then
150		break
151	fi
152done
153echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}"
154
155# Figure out our NFS root path
156#
157set `mount -t nfs`
158while [ $# -ge 1 ] ; do
159	if [ "$2" = "on" -a "$3" = "/" ]; then
160		nfsroot="$1"
161		break
162	fi
163	shift
164done
165
166# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca},
167# and /conf/${bootp_ipa}.  For each subdirectory found within these
168# directories:
169#
170# - calculate memory filesystem sizes.  If the subdirectory (prior to
171#   NFS remounting) contains the file 'md_size', the contents specified
172#   in 512 byte sectors will be used to size the memory filesystem.  Otherwise
173#   8192 sectors (4MB) is used.
174#
175# - handle NFS remounts.  If the subdirectory contains the file
176#   diskless_remount, the contents of the file is NFS mounted over
177#   the directory.  For example /conf/base/etc/diskless_remount
178#   might contain 'myserver:/etc'.  NFS remounts allow you to avoid
179#   having to dup your system directories in /conf.  Your server must
180#   be sure to export those filesystems -alldirs, however.
181#   If the diskless_remount file contains a string beginning with a
182#   '/' it is assumed that the local nfsroot should be prepended to
183#   it before attemping to the remount.  This allows the root to be
184#   relocated without needing to change the remount files.
185#
186for i in base default ${bootp_ipbca} ${bootp_ipa} ; do
187	for j in /conf/$i/* ; do
188	# memory filesystem size specification
189	#
190	subdir=${j##*/}
191	if [ -d $j -a -f $j/md_size ]; then
192	    eval md_size_$subdir=`cat $j/md_size`
193	fi
194
195	# NFS remount
196	#
197	if [ -d $j -a -f $j/diskless_remount ]; then
198		nfspt=`/bin/cat $j/diskless_remount`
199		if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then
200		nfspt="${nfsroot}${nfspt}"
201		fi
202		mount_nfs $nfspt $j
203		chkerr $? "mount_nfs $nfspt $j"
204	fi
205    done
206done
207
208# - Create all required MFS filesystems and populate them from
209#   our templates.  Support both a direct template and a dir.cpio.gz
210#   archive.  Support dir.remove files containing a list of relative
211#   paths to remove.
212#
213# TODO:
214#   + find a way to assign a 'group' identifier to a machine
215#	so we can use group-specific configurations;
216
217for i in base default ${bootp_ipbca} ${bootp_ipa} ; do
218	for j in /conf/$i/* ; do
219	subdir=${j##*/}
220	if [ -d $j ]; then
221		create_md $subdir
222		cp -Rp $j/* /$subdir
223	fi
224	done
225	for j in /conf/$i/*.cpio.gz ; do
226		subdir=${j%*.cpio.gz}
227		subdir=${subdir##*/}
228		if [ -f $j ]; then
229			create_md $subdir
230			echo "Loading /$subdir from cpio archive $j"
231			(cd / ; /stand/gzip -d < $j | /stand/cpio --extract -d )
232		fi
233	done
234	for j in /conf/$i/*.remove ; do
235		subdir=${j%*.remove}
236		subdir=${subdir##*/}
237		if [ -f $j ]; then
238			# doubly sure it is a memory disk before rm -rf'ing
239			create_md $subdir
240			(cd /$subdir; rm -rf `/bin/cat $j`)
241		fi
242	done
243done
244
245