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