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