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