rc.initdiskless revision 127657
12490Sjkh#!/bin/sh 22490Sjkh# 32490Sjkh# Copyright (c) 1999 Matt Dillon 42490Sjkh# All rights reserved. 52490Sjkh# 62490Sjkh# Redistribution and use in source and binary forms, with or without 72490Sjkh# modification, are permitted provided that the following conditions 82490Sjkh# are met: 92490Sjkh# 1. Redistributions of source code must retain the above copyright 102490Sjkh# notice, this list of conditions and the following disclaimer. 112490Sjkh# 2. Redistributions in binary form must reproduce the above copyright 122490Sjkh# notice, this list of conditions and the following disclaimer in the 132490Sjkh# documentation and/or other materials provided with the distribution. 142490Sjkh# 15203932Simp# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 162490Sjkh# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 172490Sjkh# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 182490Sjkh# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 192490Sjkh# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 202490Sjkh# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 212490Sjkh# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 222490Sjkh# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 232490Sjkh# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 242490Sjkh# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 252490Sjkh# SUCH DAMAGE. 262490Sjkh# 272490Sjkh# $FreeBSD: head/etc/rc.initdiskless 127657 2004-03-31 07:24:15Z luigi $ 282490Sjkh# 292490Sjkh# PROVIDE: initdiskless 302490Sjkh# KEYWORD: FreeBSD nojail 312490Sjkh 32114725Sobrien 332490Sjkh# On entry to this script the entire system consists of a read-only root 3415949Sache# mounted via NFS. We use the contents of /conf to create and populate 352490Sjkh# memory filesystems. The kernel has run BOOTP and configured an interface 362490Sjkh# (otherwise it would not have been able to mount the NFS root!) 372490Sjkh# 382490Sjkh# The following directories are scanned. Each sucessive directory overrides 392490Sjkh# (is merged into) the previous one. 4015949Sache# 41114725Sobrien# /conf/base universal base 4253920Sbillf# /conf/default modified by a secondary universal base 43114725Sobrien# /conf/${ipba} modified based on the assigned broadcast IP 44114725Sobrien# /conf/${ip} modified based on the machine's assigned IP 452490Sjkh# 462490Sjkh# Each of these directories may contain any number of subdirectories which 472490Sjkh# represent directories in / on the diskless machine. The existance of 482490Sjkh# these subdirectories causes this script to create a MEMORY FILESYSTEM for 492490Sjkh# /<sub_directory_name>. For example, if /conf/base/etc exists then a 502490Sjkh# memory filesystem will be created for /etc. 512490Sjkh# 522490Sjkh# If a subdirectory contains the file 'remount' the contents of the file 532490Sjkh# is a mount command used to remount the subdirectory prior to it being 542490Sjkh# copied. An example contents could be: "mount -o ro /dev/ad0s3". Note 552490Sjkh# that the directory to be mounted on is supplied by this script. 562490Sjkh# 57201613Sedwin# If a subdirectory contains the file 'diskless_remount' the contents of 582490Sjkh# the file is used to remount the subdirectory prior to it being copied to 59201613Sedwin# the memory filesystem. For example, if /conf/base/etc/diskless_remount 60201613Sedwin# contains the string 'my.server.com:/etc' then my.server.com:/etc will be 61201613Sedwin# mounted in place of the subdirectory. This allows you to avoid making 62201613Sedwin# duplicates of system directories in /conf. Special processing is done 632490Sjkh# to allow specifications relative to the root filesystem. 6445801Ssteve# 6545801Ssteve# If a subdirectory contains the file 'md_size', the contents of the 6645801Ssteve# file is used to determine the size of the memory filesystem, in 512 672490Sjkh# byte sectors. The default is 10240 (5MB). You only have to specify an 682490Sjkh# md_size if the default doesn't work for you (i.e. if it is too big or 692490Sjkh# too small). For example, /conf/base/etc/md_size might contain '16384'. 702490Sjkh# 712490Sjkh# If /conf/<special_dir>/SUBDIR.cpio.gz exists, the file is cpio'd into 722490Sjkh# the specified /SUBDIR (and a memory filesystem is created for /SUBDIR 732490Sjkh# if necessary). 7415949Sache# 752490Sjkh# If /conf/<special_dir>/SUBDIR.remove exists, the file contains a list 7690828Simp# of paths which are rm -rf'd relative to /SUBDIR. 7790828Simp# 7890828Simp# You will almost universally want to create a /conf/base/etc containing 79201613Sedwin# a diskless_remount and possibly an md_size file. You will then almost 802490Sjkh# universally want to override rc.conf, rc.local, and fstab by creating 8117201Sjoerg# /conf/default/etc/{rc.conf,rc.local,fstab}. Your fstab should be sure 82201613Sedwin# to mount a /usr... typically an NFS readonly /usr. 832490Sjkh# 8415949Sache# NOTE! /var, /tmp, and /dev will be created elsewhere. 85201613Sedwin# Those filesystems should not be specified in /conf. 862490Sjkh 87210089Semastedlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` 88201627Sdelphij[ ${dlv:=0} -eq 0 ] && [ ! -f /etc/diskless ] && exit 0 89230644Smaxim 902490Sjkh# chkerr: 91210089Semaste# 92201613Sedwin# Routine to check for error 93201613Sedwin# 94201613Sedwin# checks error code and drops into shell on failure. 95201613Sedwin# if shell exits, terminates script as well as /etc/rc. 96210089Semaste# 97210089Semastechkerr() { 98210089Semaste case $1 in 99201613Sedwin 0) 100201613Sedwin ;; 101201613Sedwin *) 102201613Sedwin echo "$2 failed: dropping into /bin/sh" 103230644Smaxim /bin/sh 104201613Sedwin # RESUME 105201613Sedwin ;; 106201613Sedwin esac 107201613Sedwin} 108201613Sedwin 109201613Sedwin# Create a generic memory disk 110230644Smaxim# 111201613Sedwinmount_md() { 112201613Sedwin /sbin/mdmfs -i 4096 -s $1 -M md $2 113201613Sedwin} 114201613Sedwin 115201613Sedwin# Create the memory filesystem if it has not already been created 116201613Sedwin# 117201613Sedwincreate_md() { 118201613Sedwin if [ "x`eval echo \\$md_created_$1`" = "x" ]; then 119201613Sedwin if [ "x`eval echo \\$md_size_$1`" = "x" ]; then 120201613Sedwin md_size=10240 121201613Sedwin else 122201613Sedwin md_size=`eval echo \\$md_size_$1` 123201613Sedwin fi 124201613Sedwin mount_md $md_size /$1 125201613Sedwin /bin/chmod 755 /$1 126201613Sedwin eval md_created_$1=created 127201613Sedwin fi 128201613Sedwin} 129201613Sedwin 130201613Sedwin# DEBUGGING 131201613Sedwin# 132201613Sedwin# set -v 133201613Sedwin 134201613Sedwin# Figure out our interface and IP. 135201613Sedwin# 136201613Sedwinbootp_ifc="" 137201613Sedwinbootp_ipa="" 138201613Sedwinbootp_ipbca="" 13937868Simpif [ ${dlv:=0} -ne 0 ] ; then 1402490Sjkh iflist=`ifconfig -l` 141210089Semaste for i in ${iflist} ; do 142210089Semaste set `ifconfig ${i}` 143210089Semaste while [ $# -ge 1 ] ; do 144210089Semaste if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then 1452490Sjkh bootp_ifc=${i} ; bootp_ipa=${2} ; shift 1462490Sjkh fi 1472490Sjkh if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then 1482490Sjkh bootp_ipbca=$2; shift 1492490Sjkh fi 1502490Sjkh shift 1512490Sjkh done 1522490Sjkh if [ "${bootp_ifc}" != "" ] ; then 1532490Sjkh break 1542490Sjkh fi 1552490Sjkh done 1562490Sjkh # Insert the directories passed with the T134 bootp cookie 1572490Sjkh # in the list of paths used for templates. 1582490Sjkh i="`/sbin/sysctl -n kern.bootp_cookie`" 1592490Sjkh [ "${i}" != "" ] && bootp_ipbca="${bootp_ipbca} ${i}" 1602490Sjkh 1612490Sjkh echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}" 1622490Sjkhfi 1632490Sjkh 1642490Sjkh# Figure out our NFS root path 1652490Sjkh# 16617201Sjoergset `mount -t nfs` 16717201Sjoergwhile [ $# -ge 1 ] ; do 1682490Sjkh if [ "$2" = "on" -a "$3" = "/" ]; then 1692490Sjkh nfsroot="$1" 1702490Sjkh break 1712490Sjkh fi 1722490Sjkh shift 1732490Sjkhdone 17417201Sjoerg 175145782Sstefanf# The list of directories with template files 1762490Sjkhtemplates="base default ${bootp_ipbca} ${bootp_ipa}" 1772490Sjkh 1782490Sjkh# The list of filesystems to umount after the copy 1792490Sjkhto_umount="" 1802490Sjkh 1812490Sjkh# If /conf/diskless_remount exists, remount all of /conf. This allows 1822490Sjkh# multiple roots to share the same conf files. 1832490Sjkhif [ -d /conf -a -f /conf/diskless_remount ]; then 1842490Sjkh nfspt=`/bin/cat /conf/diskless_remount` 1852490Sjkh if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then 1862490Sjkh nfspt="${nfsroot}${nfspt}" 1872490Sjkh fi 1882490Sjkh mount_nfs $nfspt /conf 1892490Sjkh chkerr $? "mount_nfs $nfspt /conf" 1902490Sjkh to_umount="/conf" 1912490Sjkhfi 1922490Sjkh 1932490Sjkh# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca}, 1942490Sjkh# and /conf/${bootp_ipa}. For each subdirectory found within these 1952490Sjkh# directories: 1962490Sjkh# 1972490Sjkh# - calculate memory filesystem sizes. If the subdirectory (prior to 1982490Sjkh# NFS remounting) contains the file 'md_size', the contents specified 1992490Sjkh# in 512 byte sectors will be used to size the memory filesystem. Otherwise 2002490Sjkh# 8192 sectors (4MB) is used. 2012490Sjkh# 2022490Sjkh# - handle NFS remounts. If the subdirectory contains the file 2032490Sjkh# diskless_remount, the contents of the file is NFS mounted over 2042490Sjkh# the directory. For example /conf/base/etc/diskless_remount 2052490Sjkh# might contain 'myserver:/etc'. NFS remounts allow you to avoid 2062490Sjkh# having to dup your system directories in /conf. Your server must 2072490Sjkh# be sure to export those filesystems -alldirs, however. 2082490Sjkh# If the diskless_remount file contains a string beginning with a 2092490Sjkh# '/' it is assumed that the local nfsroot should be prepended to 21017201Sjoerg# it before attemping to the remount. This allows the root to be 211145782Sstefanf# relocated without needing to change the remount files. 2122490Sjkh# 213201613Sedwinfor i in ${templates} ; do 2142490Sjkh for j in /conf/$i/* ; do 2152490Sjkh # memory filesystem size specification 2162490Sjkh # 2172490Sjkh subdir=${j##*/} 2182490Sjkh if [ -d $j -a -f $j/md_size ]; then 2192490Sjkh eval md_size_$subdir=`cat $j/md_size` 2202490Sjkh fi 22117201Sjoerg 222145782Sstefanf # remount 2232490Sjkh # 224201613Sedwin if [ -d $j -a -f $j/remount ]; then 2252490Sjkh nfspt=`/bin/cat $j/remount` 2262490Sjkh $nfspt $j 2272490Sjkh chkerr $? "$nfspt $j" 2282490Sjkh to_umount="${to_umount} $j" # XXX hope it is really a mount! 2292490Sjkh fi 2302490Sjkh 2312490Sjkh # NFS remount 2322490Sjkh # 233201613Sedwin if [ -d $j -a -f $j/diskless_remount ]; then 234201613Sedwin nfspt=`/bin/cat $j/diskless_remount` 235201613Sedwin if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then 236201613Sedwin nfspt="${nfsroot}${nfspt}" 237201613Sedwin fi 238210089Semaste mount_nfs $nfspt $j 239210089Semaste chkerr $? "mount_nfs $nfspt $j" 240201613Sedwin to_umount="${to_umount} $j" 241201613Sedwin fi 242 done 243done 244 245# - Create all required MFS filesystems and populate them from 246# our templates. Support both a direct template and a dir.cpio.gz 247# archive. Support dir.remove files containing a list of relative 248# paths to remove. 249# 250# The dir.cpio.gz form is there to make the copy process more efficient, 251# so if the cpio archive is present, it prevents the files from dir/ 252# from being copied. 253 254for i in ${templates} ; do 255 for j in /conf/$i/* ; do 256 subdir=${j##*/} 257 if [ -d $j -a ! -f $j.cpio.gz ]; then 258 create_md $subdir 259 cp -Rp $j/* /$subdir 260 fi 261 done 262 for j in /conf/$i/*.cpio.gz ; do 263 subdir=${j%*.cpio.gz} 264 subdir=${subdir##*/} 265 if [ -f $j ]; then 266 create_md $subdir 267 echo "Loading /$subdir from cpio archive $j" 268 (cd / ; /stand/gzip -d < $j | /stand/cpio --extract -d ) 269 fi 270 done 271 for j in /conf/$i/*.remove ; do 272 subdir=${j%*.remove} 273 subdir=${subdir##*/} 274 if [ -f $j ]; then 275 # doubly sure it is a memory disk before rm -rf'ing 276 create_md $subdir 277 (cd /$subdir; rm -rf `/bin/cat $j`) 278 fi 279 done 280done 281 282# umount partitions used to fill the memory filesystems 283[ -n "${to_umount}" ] && umount $to_umount 284