rc.initdiskless revision 121014
1174380Sjhb#!/bin/sh 2174380Sjhb# 3174380Sjhb# Copyright (c) 1999 Matt Dillon 4174380Sjhb# All rights reserved. 5174380Sjhb# 6174380Sjhb# Redistribution and use in source and binary forms, with or without 7174380Sjhb# modification, are permitted provided that the following conditions 8174380Sjhb# are met: 9174380Sjhb# 1. Redistributions of source code must retain the above copyright 10174380Sjhb# notice, this list of conditions and the following disclaimer. 11174380Sjhb# 2. Redistributions in binary form must reproduce the above copyright 12174380Sjhb# notice, this list of conditions and the following disclaimer in the 13174380Sjhb# documentation and/or other materials provided with the distribution. 14174380Sjhb# 15174380Sjhb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16174380Sjhb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17174380Sjhb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18174380Sjhb# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19174380Sjhb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20174380Sjhb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21174380Sjhb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22174380Sjhb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23174380Sjhb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24174380Sjhb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25174380Sjhb# SUCH DAMAGE. 26174380Sjhb# 27174380Sjhb# $FreeBSD: head/etc/rc.initdiskless 121014 2003-10-12 00:19:45Z kris $ 28174380Sjhb# 29174380Sjhb# PROVIDE: initdiskless 30174380Sjhb# KEYWORD: FreeBSD 31174380Sjhb 32174380Sjhb 33194910Sjhb# On entry to this script the entire system consists of a read-only root 34194910Sjhb# mounted via NFS. We use the contents of /conf to create and populate 35194910Sjhb# memory filesystems. The kernel has run BOOTP and configured an interface 36194910Sjhb# (otherwise it would not have been able to mount the NFS root!) 37194910Sjhb# 38174380Sjhb# The following directories are scanned. Each sucessive directory overrides 39174380Sjhb# (is merged into) the previous one. 40174380Sjhb# 41174380Sjhb# /conf/base universal base 42174381Sjhb# /conf/default modified by a secondary universal base 43174381Sjhb# /conf/${ipba} modified based on the assigned broadcast IP 44174381Sjhb# /conf/${ip} modified based on the machine's assigned IP 45174381Sjhb# 46174381Sjhb# Each of these directories may contain any number of subdirectories which 47174381Sjhb# represent directories in / on the diskless machine. The existance of 48174381Sjhb# these subdirectories causes this script to create a MEMORY FILESYSTEM for 49174381Sjhb# /<sub_directory_name>. For example, if /conf/base/etc exists then a 50174381Sjhb# memory filesystem will be created for /etc. 51174381Sjhb# 52174381Sjhb# If a subdirectory contains the file 'diskless_remount' the contents of 53174381Sjhb# the file is used to remount the subdirectory prior to it being copied to 54174381Sjhb# the memory filesystem. For example, if /conf/base/etc/diskless_remount 55174381Sjhb# contains the string 'my.server.com:/etc' then my.server.com:/etc will be 56174381Sjhb# mounted in place of the subdirectory. This allows you to avoid making 57174381Sjhb# duplicates of system directories in /conf. 58174381Sjhb# 59174381Sjhb# If a subdirectory contains the file 'md_size', the contents of the 60174381Sjhb# file is used to determine the size of the memory filesystem, in 512 61174381Sjhb# byte sectors. The default is 10240 (5MB). You only have to specify an 62174381Sjhb# md_size if the default doesn't work for you (i.e. if it is too big or 63174381Sjhb# too small). For example, /conf/base/etc/md_size might contain '16384'. 64174381Sjhb# 65174381Sjhb# If /conf/<special_dir>/SUBDIR.cpio.gz exists, the file is cpio'd into 66174381Sjhb# the specified /SUBDIR (and a memory filesystem is created for /SUBDIR 67174381Sjhb# if necessary). 68174381Sjhb# 69174381Sjhb# If /conf/<special_dir>/SUBDIR.remove exists, the file contains a list 70174380Sjhb# of paths which are rm -rf'd relative to /SUBDIR. 71174380Sjhb# 72174380Sjhb# You will almost universally want to create a /conf/base/etc containing 73194910Sjhb# a diskless_remount and possibly an md_size file. You will then almost 74194910Sjhb# universally want to override rc.conf, rc.local, and fstab by creating 75194910Sjhb# /conf/default/etc/{rc.conf,rc.local,fstab}. Your fstab should be sure 76174380Sjhb# to mount a /usr... typically an NFS readonly /usr. 77174380Sjhb# 78174380Sjhb# NOTE! rc.diskless2 will create /var, /tmp, and /dev. Those filesystems 79174380Sjhb# should not be specified in /conf. At least not yet. 80174380Sjhb 81174380Sjhbdlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` 82174380Sjhb[ ${dlv:=0} -eq 0 ] && exit 0 83174380Sjhb 84174380Sjhb# chkerr: 85174380Sjhb# 86174380Sjhb# Routine to check for error 87174380Sjhb# 88174380Sjhb# checks error code and drops into shell on failure. 89174380Sjhb# if shell exits, terminates script as well as /etc/rc. 90174380Sjhb# 91174380Sjhbchkerr() { 92174380Sjhb case $1 in 93174380Sjhb 0) 94174380Sjhb ;; 95174380Sjhb *) 96174380Sjhb echo "$2 failed: dropping into /bin/sh" 97174380Sjhb /bin/sh 98194910Sjhb # RESUME 99194910Sjhb ;; 100194910Sjhb esac 101194910Sjhb} 102194910Sjhb 103194910Sjhb# Create a generic memory disk 104194910Sjhb# 105194910Sjhbmount_md() { 106194910Sjhb /sbin/mdmfs -i 4096 -s $1 -M md $2 107194910Sjhb} 108194910Sjhb 109194910Sjhb# Create the memory filesystem if it has not already been created 110194910Sjhb# 111194910Sjhbcreate_md() { 112194910Sjhb if [ "x`eval echo \\$md_created_$1`" = "x" ]; then 113194910Sjhb if [ "x`eval echo \\$md_size_$1`" = "x" ]; then 114194910Sjhb md_size=10240 115194910Sjhb else 116194910Sjhb md_size=`eval echo \\$md_size_$1` 117194910Sjhb fi 118194910Sjhb mount_md $md_size /$1 119194910Sjhb /bin/chmod 755 /$1 120194910Sjhb eval md_created_$1=created 121194910Sjhb fi 122194910Sjhb} 123194910Sjhb 124194910Sjhb# DEBUGGING 125194910Sjhb# 126194910Sjhb# set -v 127194910Sjhb 128194910Sjhb# Figure out our interface and IP. 129194910Sjhb# 130194910Sjhbbootp_ifc="" 131194910Sjhbbootp_ipa="" 132194910Sjhbbootp_ipbca="" 133194910Sjhbiflist=`ifconfig -l` 134194910Sjhbfor i in ${iflist} ; do 135194910Sjhb set `ifconfig ${i}` 136194910Sjhb while [ $# -ge 1 ] ; do 137194910Sjhb if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then 138194910Sjhb bootp_ifc=${i} ; bootp_ipa=${2} ; shift 139194910Sjhb fi 140194910Sjhb if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then 141194910Sjhb bootp_ipbca=$2; shift 142194910Sjhb fi 143194910Sjhb shift 144194910Sjhb done 145194910Sjhb if [ "${bootp_ifc}" != "" ] ; then 146194910Sjhb break 147194910Sjhb fi 148194910Sjhbdone 149194910Sjhbecho "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}" 150205322Skib 151205322Skib# Figure out our NFS root path 152205322Skib# 153205322Skibset `mount -t nfs` 154205322Skibwhile [ $# -ge 1 ] ; do 155194910Sjhb if [ "$2" = "on" -a "$3" = "/" ]; then 156194910Sjhb nfsroot="$1" 157205322Skib break 158205322Skib fi 159205322Skib shift 160174380Sjhbdone 161 162# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca}, 163# and /conf/${bootp_ipa}. For each subdirectory found within these 164# directories: 165# 166# - calculate memory filesystem sizes. If the subdirectory (prior to 167# NFS remounting) contains the file 'md_size', the contents specified 168# in 512 byte sectors will be used to size the memory filesystem. Otherwise 169# 8192 sectors (4MB) is used. 170# 171# - handle NFS remounts. If the subdirectory contains the file 172# diskless_remount, the contents of the file is NFS mounted over 173# the directory. For example /conf/base/etc/diskless_remount 174# might contain 'myserver:/etc'. NFS remounts allow you to avoid 175# having to dup your system directories in /conf. Your server must 176# be sure to export those filesystems -alldirs, however. 177# If the diskless_remount file contains a string beginning with a 178# '/' it is assumed that the local nfsroot should be prepended to 179# it before attemping to the remount. This allows the root to be 180# relocated without needing to change the remount files. 181# 182for i in base default ${bootp_ipbca} ${bootp_ipa} ; do 183 for j in /conf/$i/* ; do 184 # memory filesystem size specification 185 # 186 subdir=${j##*/} 187 if [ -d $j -a -f $j/md_size ]; then 188 eval md_size_$subdir=`cat $j/md_size` 189 fi 190 191 # NFS remount 192 # 193 if [ -d $j -a -f $j/diskless_remount ]; then 194 nfspt=`/bin/cat $j/diskless_remount` 195 if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then 196 nfspt="${nfsroot}${nfspt}" 197 fi 198 mount_nfs $nfspt $j 199 chkerr $? "mount_nfs $nfspt $j" 200 fi 201 done 202done 203 204# - Create all required MFS filesystems and populate them from 205# our templates. Support both a direct template and a dir.cpio.gz 206# archive. Support dir.remove files containing a list of relative 207# paths to remove. 208# 209# TODO: 210# + find a way to assign a 'group' identifier to a machine 211# so we can use group-specific configurations; 212 213for i in base default ${bootp_ipbca} ${bootp_ipa} ; do 214 for j in /conf/$i/* ; do 215 subdir=${j##*/} 216 if [ -d $j ]; then 217 create_md $subdir 218 cp -Rp $j/* /$subdir 219 fi 220 done 221 for j in /conf/$i/*.cpio.gz ; do 222 subdir=${j%*.cpio.gz} 223 subdir=${subdir##*/} 224 if [ -f $j ]; then 225 create_md $subdir 226 echo "Loading /$subdir from cpio archive $j" 227 (cd / ; /stand/gzip -d < $j | /stand/cpio --extract -d ) 228 fi 229 done 230 for j in /conf/$i/*.remove ; do 231 subdir=${j%*.remove} 232 subdir=${subdir##*/} 233 if [ -f $j ]; then 234 # doubly sure it is a memory disk before rm -rf'ing 235 create_md $subdir 236 (cd /$subdir; rm -rf `/bin/cat $j`) 237 fi 238 done 239done 240 241