rc.initdiskless revision 121067
1100280Sgordon#!/bin/sh 2100280Sgordon# 3118638Sfjoe# Copyright (c) 1999 Matt Dillon 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 121067 2003-10-13 08:20:55Z dougb $ 2851231Ssheldonh# 29100280Sgordon# PROVIDE: initdiskless 30100280Sgordon# KEYWORD: FreeBSD 31100280Sgordon 32121067Sdougb 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 61121014Skris# byte sectors. The default is 10240 (5MB). 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 63121014Skris# too small). For example, /conf/base/etc/md_size might contain '16384'. 64108191Sdillon# 65108191Sdillon# If /conf/<special_dir>/SUBDIR.cpio.gz exists, the file is cpio'd into 66108191Sdillon# the specified /SUBDIR (and a memory filesystem is created for /SUBDIR 67108191Sdillon# if necessary). 68108191Sdillon# 69108191Sdillon# If /conf/<special_dir>/SUBDIR.remove exists, the file contains a list 70108191Sdillon# of paths which are rm -rf'd relative to /SUBDIR. 71108191Sdillon# 72108191Sdillon# You will almost universally want to create a /conf/base/etc containing 73108191Sdillon# a diskless_remount and possibly an md_size file. You will then almost 74108191Sdillon# universally want to override rc.conf, rc.local, and fstab by creating 75108191Sdillon# /conf/default/etc/{rc.conf,rc.local,fstab}. Your fstab should be sure 76108191Sdillon# to mount a /usr... typically an NFS readonly /usr. 77108191Sdillon# 78121066Sdougb# NOTE! /etc/rc.d/diskless will create /var, /tmp, and /dev. 79121066Sdougb# Those filesystems should not be specified in /conf. At least not yet. 80108191Sdillon 81100280Sgordondlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` 82100280Sgordon[ ${dlv:=0} -eq 0 ] && exit 0 83100280Sgordon 8443803Sdillon# chkerr: 8543803Sdillon# 8643803Sdillon# Routine to check for error 8743803Sdillon# 8843803Sdillon# checks error code and drops into shell on failure. 8943803Sdillon# if shell exits, terminates script as well as /etc/rc. 9051231Ssheldonh# 91108191Sdillonchkerr() { 92108191Sdillon case $1 in 93108191Sdillon 0) 94108191Sdillon ;; 95108191Sdillon *) 96108191Sdillon echo "$2 failed: dropping into /bin/sh" 97108191Sdillon /bin/sh 98108191Sdillon # RESUME 99108191Sdillon ;; 100108191Sdillon esac 10143803Sdillon} 10243803Sdillon 103108191Sdillon# Create a generic memory disk 104108191Sdillon# 105108191Sdillonmount_md() { 106108191Sdillon /sbin/mdmfs -i 4096 -s $1 -M md $2 10775931Simp} 10875931Simp 109108191Sdillon# Create the memory filesystem if it has not already been created 110108191Sdillon# 111108191Sdilloncreate_md() { 112108191Sdillon if [ "x`eval echo \\$md_created_$1`" = "x" ]; then 113110942Sjhay if [ "x`eval echo \\$md_size_$1`" = "x" ]; then 114121014Skris md_size=10240 115108191Sdillon else 116108191Sdillon md_size=`eval echo \\$md_size_$1` 117108191Sdillon fi 118108191Sdillon mount_md $md_size /$1 119108191Sdillon /bin/chmod 755 /$1 120108191Sdillon eval md_created_$1=created 121108191Sdillon fi 122108191Sdillon} 123108191Sdillon 12443803Sdillon# DEBUGGING 12543803Sdillon# 12655520Sluigi# set -v 12743803Sdillon 12851231Ssheldonh# Figure out our interface and IP. 12943803Sdillon# 13055520Sluigibootp_ifc="" 13155520Sluigibootp_ipa="" 13255520Sluigibootp_ipbca="" 13355520Sluigiiflist=`ifconfig -l` 13455520Sluigifor i in ${iflist} ; do 135108191Sdillon set `ifconfig ${i}` 136108191Sdillon while [ $# -ge 1 ] ; do 137108191Sdillon if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then 138108191Sdillon bootp_ifc=${i} ; bootp_ipa=${2} ; shift 139108191Sdillon fi 140108191Sdillon if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then 141108191Sdillon bootp_ipbca=$2; shift 142108191Sdillon fi 143108191Sdillon shift 144108191Sdillon done 145108191Sdillon if [ "${bootp_ifc}" != "" ] ; then 146108191Sdillon break 147108191Sdillon fi 14855520Sluigidone 14955520Sluigiecho "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}" 15043803Sdillon 151117087Sbrooks# Figure out our NFS root path 152121067Sdougb# 153117087Sbrooksset `mount -t nfs` 154117087Sbrookswhile [ $# -ge 1 ] ; do 155117087Sbrooks if [ "$2" = "on" -a "$3" = "/" ]; then 156117087Sbrooks nfsroot="$1" 157117087Sbrooks break 158117087Sbrooks fi 159117087Sbrooks shift 160117087Sbrooksdone 161117087Sbrooks 162108191Sdillon# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca}, 163121067Sdougb# and /conf/${bootp_ipa}. For each subdirectory found within these 164108191Sdillon# directories: 16543803Sdillon# 166108191Sdillon# - calculate memory filesystem sizes. If the subdirectory (prior to 167108191Sdillon# NFS remounting) contains the file 'md_size', the contents specified 168108191Sdillon# in 512 byte sectors will be used to size the memory filesystem. Otherwise 169108191Sdillon# 8192 sectors (4MB) is used. 170108191Sdillon# 171108191Sdillon# - handle NFS remounts. If the subdirectory contains the file 172108191Sdillon# diskless_remount, the contents of the file is NFS mounted over 173108191Sdillon# the directory. For example /conf/base/etc/diskless_remount 174108191Sdillon# might contain 'myserver:/etc'. NFS remounts allow you to avoid 175108191Sdillon# having to dup your system directories in /conf. Your server must 176108191Sdillon# be sure to export those filesystems -alldirs, however. 177117087Sbrooks# If the diskless_remount file contains a string beginning with a 178117087Sbrooks# '/' it is assumed that the local nfsroot should be prepended to 179117087Sbrooks# it before attemping to the remount. This allows the root to be 180117087Sbrooks# relocated without needing to change the remount files. 181108191Sdillon# 182108191Sdillonfor i in base default ${bootp_ipbca} ${bootp_ipa} ; do 183108191Sdillon for j in /conf/$i/* ; do 184108191Sdillon # memory filesystem size specification 185108191Sdillon # 186108191Sdillon subdir=${j##*/} 187108191Sdillon if [ -d $j -a -f $j/md_size ]; then 188108191Sdillon eval md_size_$subdir=`cat $j/md_size` 189108191Sdillon fi 19043803Sdillon 191108191Sdillon # NFS remount 192108191Sdillon # 193108191Sdillon if [ -d $j -a -f $j/diskless_remount ]; then 194108191Sdillon nfspt=`/bin/cat $j/diskless_remount` 195117087Sbrooks if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then 196117087Sbrooks nfspt="${nfsroot}${nfspt}" 197117087Sbrooks fi 198108191Sdillon mount_nfs $nfspt $j 199108191Sdillon chkerr $? "mount_nfs $nfspt $j" 20075746Sbsd fi 201108191Sdillon done 20275746Sbsddone 20375746Sbsd 204108191Sdillon# - Create all required MFS filesystems and populate them from 205108191Sdillon# our templates. Support both a direct template and a dir.cpio.gz 206108191Sdillon# archive. Support dir.remove files containing a list of relative 207108191Sdillon# paths to remove. 20895280Sobrien# 209108191Sdillon# TODO: 210108191Sdillon# + find a way to assign a 'group' identifier to a machine 211108191Sdillon# so we can use group-specific configurations; 21295280Sobrien 213108191Sdillonfor i in base default ${bootp_ipbca} ${bootp_ipa} ; do 214108191Sdillon for j in /conf/$i/* ; do 215108191Sdillon subdir=${j##*/} 216108191Sdillon if [ -d $j ]; then 217108191Sdillon create_md $subdir 218108191Sdillon cp -Rp $j/* /$subdir 219108191Sdillon fi 220108191Sdillon done 221108191Sdillon for j in /conf/$i/*.cpio.gz ; do 222108191Sdillon subdir=${j%*.cpio.gz} 223108191Sdillon subdir=${subdir##*/} 224108191Sdillon if [ -f $j ]; then 225108191Sdillon create_md $subdir 226108191Sdillon echo "Loading /$subdir from cpio archive $j" 227108191Sdillon (cd / ; /stand/gzip -d < $j | /stand/cpio --extract -d ) 228108191Sdillon fi 229108191Sdillon done 230108191Sdillon for j in /conf/$i/*.remove ; do 231108191Sdillon subdir=${j%*.remove} 232108191Sdillon subdir=${subdir##*/} 233108191Sdillon if [ -f $j ]; then 234108191Sdillon # doubly sure it is a memory disk before rm -rf'ing 235108191Sdillon create_md $subdir 236108191Sdillon (cd /$subdir; rm -rf `/bin/cat $j`) 237108191Sdillon fi 238108191Sdillon done 239108191Sdillondone 240104334Sdd 241