rc.initdiskless revision 110942
1100280Sgordon#!/bin/sh 2100280Sgordon# 366830Sobrien# Copyright (c) 1999 Matt Dillion 466830Sobrien# All rights reserved. 566830Sobrien# 666830Sobrien# Redistribution and use in source and binary forms, with or without 766830Sobrien# modification, are permitted provided that the following conditions 866830Sobrien# are met: 966830Sobrien# 1. Redistributions of source code must retain the above copyright 1066830Sobrien# notice, this list of conditions and the following disclaimer. 1166830Sobrien# 2. Redistributions in binary form must reproduce the above copyright 1266830Sobrien# notice, this list of conditions and the following disclaimer in the 1366830Sobrien# documentation and/or other materials provided with the distribution. 1466830Sobrien# 1566830Sobrien# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1666830Sobrien# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1766830Sobrien# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1866830Sobrien# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1966830Sobrien# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2066830Sobrien# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2166830Sobrien# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2266830Sobrien# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2366830Sobrien# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2466830Sobrien# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2566830Sobrien# SUCH DAMAGE. 2666830Sobrien# 2751231Ssheldonh# $FreeBSD: head/etc/rc.initdiskless 110942 2003-02-15 16:29:20Z jhay $ 2851231Ssheldonh# 29100280Sgordon# PROVIDE: initdiskless 30100280Sgordon# KEYWORD: FreeBSD 31100280Sgordon 32108191Sdillon 33108191Sdillon# On entry to this script the entire system consists of a read-only root 34108191Sdillon# mounted via NFS. We use the contents of /conf to create and populate 35108191Sdillon# memory filesystems. The kernel has run BOOTP and configured an interface 36108191Sdillon# (otherwise it would not have been able to mount the NFS root!) 37108191Sdillon# 38108191Sdillon# The following directories are scanned. Each sucessive directory overrides 39108191Sdillon# (is merged into) the previous one. 40108191Sdillon# 41108191Sdillon# /conf/base universal base 42108191Sdillon# /conf/default modified by a secondary universal base 43108191Sdillon# /conf/${ipba} modified based on the assigned broadcast IP 44108191Sdillon# /conf/${ip} modified based on the machine's assigned IP 45108191Sdillon# 46108191Sdillon# Each of these directories may contain any number of subdirectories which 47108191Sdillon# represent directories in / on the diskless machine. The existance of 48108191Sdillon# these subdirectories causes this script to create a MEMORY FILESYSTEM for 49108191Sdillon# /<sub_directory_name>. For example, if /conf/base/etc exists then a 50108191Sdillon# memory filesystem will be created for /etc. 51108191Sdillon# 52108191Sdillon# If a subdirectory contains the file 'diskless_remount' the contents of 53108191Sdillon# the file is used to remount the subdirectory prior to it being copied to 54108191Sdillon# the memory filesystem. For example, if /conf/base/etc/diskless_remount 55108191Sdillon# contains the string 'my.server.com:/etc' then my.server.com:/etc will be 56108191Sdillon# mounted in place of the subdirectory. This allows you to avoid making 57108191Sdillon# duplicates of system directories in /conf. 58108191Sdillon# 59108191Sdillon# If a subdirectory contains the file 'md_size', the contents of the 60108191Sdillon# file is used to determine the size of the memory filesystem, in 512 61108191Sdillon# byte sectors. The default is 8192 (4MB). You only have to specify an 62108191Sdillon# md_size if the default doesn't work for you (i.e. if it is too big or 63108191Sdillon# too small). Note that in -current the default is 4096 (2MB). For 64108191Sdillon# example, /conf/base/etc/md_size might contain '16384'. 65108191Sdillon# 66108191Sdillon# If /conf/<special_dir>/SUBDIR.cpio.gz exists, the file is cpio'd into 67108191Sdillon# the specified /SUBDIR (and a memory filesystem is created for /SUBDIR 68108191Sdillon# if necessary). 69108191Sdillon# 70108191Sdillon# If /conf/<special_dir>/SUBDIR.remove exists, the file contains a list 71108191Sdillon# of paths which are rm -rf'd relative to /SUBDIR. 72108191Sdillon# 73108191Sdillon# You will almost universally want to create a /conf/base/etc containing 74108191Sdillon# a diskless_remount and possibly an md_size file. You will then almost 75108191Sdillon# universally want to override rc.conf, rc.local, and fstab by creating 76108191Sdillon# /conf/default/etc/{rc.conf,rc.local,fstab}. Your fstab should be sure 77108191Sdillon# to mount a /usr... typically an NFS readonly /usr. 78108191Sdillon# 79108191Sdillon# NOTE! rc.diskless2 will create /var, /tmp, and /dev. Those filesystems 80108191Sdillon# should not be specified in /conf. At least not yet. 81108191Sdillon 82100280Sgordondlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` 83100280Sgordon[ ${dlv:=0} -eq 0 ] && exit 0 84100280Sgordon 8543803Sdillon# chkerr: 8643803Sdillon# 8743803Sdillon# Routine to check for error 8843803Sdillon# 8943803Sdillon# checks error code and drops into shell on failure. 9043803Sdillon# if shell exits, terminates script as well as /etc/rc. 9151231Ssheldonh# 92108191Sdillonchkerr() { 93108191Sdillon case $1 in 94108191Sdillon 0) 95108191Sdillon ;; 96108191Sdillon *) 97108191Sdillon echo "$2 failed: dropping into /bin/sh" 98108191Sdillon /bin/sh 99108191Sdillon # RESUME 100108191Sdillon ;; 101108191Sdillon esac 10243803Sdillon} 10343803Sdillon 104108191Sdillon# Create a generic memory disk 105108191Sdillon# 106108191Sdillonmount_md() { 107108191Sdillon /sbin/mdmfs -i 4096 -s $1 -M md $2 10875931Simp} 10975931Simp 110108191Sdillon# Create the memory filesystem if it has not already been created 111108191Sdillon# 112108191Sdilloncreate_md() { 113108191Sdillon if [ "x`eval echo \\$md_created_$1`" = "x" ]; then 114110942Sjhay if [ "x`eval echo \\$md_size_$1`" = "x" ]; then 115108191Sdillon md_size=4096 116108191Sdillon else 117108191Sdillon md_size=`eval echo \\$md_size_$1` 118108191Sdillon fi 119108191Sdillon mount_md $md_size /$1 120108191Sdillon /bin/chmod 755 /$1 121108191Sdillon eval md_created_$1=created 122108191Sdillon fi 123108191Sdillon} 124108191Sdillon 12543803Sdillon# DEBUGGING 12643803Sdillon# 12755520Sluigi# set -v 12843803Sdillon 12951231Ssheldonh# Figure out our interface and IP. 13043803Sdillon# 13155520Sluigibootp_ifc="" 13255520Sluigibootp_ipa="" 13355520Sluigibootp_ipbca="" 13455520Sluigiiflist=`ifconfig -l` 13555520Sluigifor i in ${iflist} ; do 136108191Sdillon set `ifconfig ${i}` 137108191Sdillon while [ $# -ge 1 ] ; do 138108191Sdillon if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then 139108191Sdillon bootp_ifc=${i} ; bootp_ipa=${2} ; shift 140108191Sdillon fi 141108191Sdillon if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then 142108191Sdillon bootp_ipbca=$2; shift 143108191Sdillon fi 144108191Sdillon shift 145108191Sdillon done 146108191Sdillon if [ "${bootp_ifc}" != "" ] ; then 147108191Sdillon break 148108191Sdillon fi 14955520Sluigidone 15055520Sluigiecho "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}" 15143803Sdillon 152108191Sdillon# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca}, 153108191Sdillon# and /conf/${bootp_ipa}. For each subdirectory found within these 154108191Sdillon# directories: 15543803Sdillon# 156108191Sdillon# - calculate memory filesystem sizes. If the subdirectory (prior to 157108191Sdillon# NFS remounting) contains the file 'md_size', the contents specified 158108191Sdillon# in 512 byte sectors will be used to size the memory filesystem. Otherwise 159108191Sdillon# 8192 sectors (4MB) is used. 160108191Sdillon# 161108191Sdillon# - handle NFS remounts. If the subdirectory contains the file 162108191Sdillon# diskless_remount, the contents of the file is NFS mounted over 163108191Sdillon# the directory. For example /conf/base/etc/diskless_remount 164108191Sdillon# might contain 'myserver:/etc'. NFS remounts allow you to avoid 165108191Sdillon# having to dup your system directories in /conf. Your server must 166108191Sdillon# be sure to export those filesystems -alldirs, however. 167108191Sdillon# 168108191Sdillonfor i in base default ${bootp_ipbca} ${bootp_ipa} ; do 169108191Sdillon for j in /conf/$i/* ; do 170108191Sdillon # memory filesystem size specification 171108191Sdillon # 172108191Sdillon subdir=${j##*/} 173108191Sdillon if [ -d $j -a -f $j/md_size ]; then 174108191Sdillon eval md_size_$subdir=`cat $j/md_size` 175108191Sdillon fi 17643803Sdillon 177108191Sdillon # NFS remount 178108191Sdillon # 179108191Sdillon if [ -d $j -a -f $j/diskless_remount ]; then 180108191Sdillon nfspt=`/bin/cat $j/diskless_remount` 181108191Sdillon mount_nfs $nfspt $j 182108191Sdillon chkerr $? "mount_nfs $nfspt $j" 18375746Sbsd fi 184108191Sdillon done 18575746Sbsddone 18675746Sbsd 187108191Sdillon# - Create all required MFS filesystems and populate them from 188108191Sdillon# our templates. Support both a direct template and a dir.cpio.gz 189108191Sdillon# archive. Support dir.remove files containing a list of relative 190108191Sdillon# paths to remove. 19195280Sobrien# 192108191Sdillon# TODO: 193108191Sdillon# + find a way to assign a 'group' identifier to a machine 194108191Sdillon# so we can use group-specific configurations; 19595280Sobrien 196108191Sdillonfor i in base default ${bootp_ipbca} ${bootp_ipa} ; do 197108191Sdillon for j in /conf/$i/* ; do 198108191Sdillon subdir=${j##*/} 199108191Sdillon if [ -d $j ]; then 200108191Sdillon create_md $subdir 201108191Sdillon cp -Rp $j/* /$subdir 202108191Sdillon fi 203108191Sdillon done 204108191Sdillon for j in /conf/$i/*.cpio.gz ; do 205108191Sdillon subdir=${j%*.cpio.gz} 206108191Sdillon subdir=${subdir##*/} 207108191Sdillon if [ -f $j ]; then 208108191Sdillon create_md $subdir 209108191Sdillon echo "Loading /$subdir from cpio archive $j" 210108191Sdillon (cd / ; /stand/gzip -d < $j | /stand/cpio --extract -d ) 211108191Sdillon fi 212108191Sdillon done 213108191Sdillon for j in /conf/$i/*.remove ; do 214108191Sdillon subdir=${j%*.remove} 215108191Sdillon subdir=${subdir##*/} 216108191Sdillon if [ -f $j ]; then 217108191Sdillon # doubly sure it is a memory disk before rm -rf'ing 218108191Sdillon create_md $subdir 219108191Sdillon (cd /$subdir; rm -rf `/bin/cat $j`) 220108191Sdillon fi 221108191Sdillon done 222108191Sdillondone 223104334Sdd 224