rc.initdiskless revision 127657
1#!/bin/sh 2# 3# Copyright (c) 1999 Matt Dillon 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 127657 2004-03-31 07:24:15Z luigi $ 28# 29# PROVIDE: initdiskless 30# KEYWORD: FreeBSD nojail 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 'remount' the contents of the file 53# is a mount command used to remount the subdirectory prior to it being 54# copied. An example contents could be: "mount -o ro /dev/ad0s3". Note 55# that the directory to be mounted on is supplied by this script. 56# 57# If a subdirectory contains the file 'diskless_remount' the contents of 58# the file is used to remount the subdirectory prior to it being copied to 59# the memory filesystem. For example, if /conf/base/etc/diskless_remount 60# contains the string 'my.server.com:/etc' then my.server.com:/etc will be 61# mounted in place of the subdirectory. This allows you to avoid making 62# duplicates of system directories in /conf. Special processing is done 63# to allow specifications relative to the root filesystem. 64# 65# If a subdirectory contains the file 'md_size', the contents of the 66# file is used to determine the size of the memory filesystem, in 512 67# byte sectors. The default is 10240 (5MB). You only have to specify an 68# md_size if the default doesn't work for you (i.e. if it is too big or 69# too small). For example, /conf/base/etc/md_size might contain '16384'. 70# 71# If /conf/<special_dir>/SUBDIR.cpio.gz exists, the file is cpio'd into 72# the specified /SUBDIR (and a memory filesystem is created for /SUBDIR 73# if necessary). 74# 75# If /conf/<special_dir>/SUBDIR.remove exists, the file contains a list 76# of paths which are rm -rf'd relative to /SUBDIR. 77# 78# You will almost universally want to create a /conf/base/etc containing 79# a diskless_remount and possibly an md_size file. You will then almost 80# universally want to override rc.conf, rc.local, and fstab by creating 81# /conf/default/etc/{rc.conf,rc.local,fstab}. Your fstab should be sure 82# to mount a /usr... typically an NFS readonly /usr. 83# 84# NOTE! /var, /tmp, and /dev will be created elsewhere. 85# Those filesystems should not be specified in /conf. 86 87dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` 88[ ${dlv:=0} -eq 0 ] && [ ! -f /etc/diskless ] && exit 0 89 90# chkerr: 91# 92# Routine to check for error 93# 94# checks error code and drops into shell on failure. 95# if shell exits, terminates script as well as /etc/rc. 96# 97chkerr() { 98 case $1 in 99 0) 100 ;; 101 *) 102 echo "$2 failed: dropping into /bin/sh" 103 /bin/sh 104 # RESUME 105 ;; 106 esac 107} 108 109# Create a generic memory disk 110# 111mount_md() { 112 /sbin/mdmfs -i 4096 -s $1 -M md $2 113} 114 115# Create the memory filesystem if it has not already been created 116# 117create_md() { 118 if [ "x`eval echo \\$md_created_$1`" = "x" ]; then 119 if [ "x`eval echo \\$md_size_$1`" = "x" ]; then 120 md_size=10240 121 else 122 md_size=`eval echo \\$md_size_$1` 123 fi 124 mount_md $md_size /$1 125 /bin/chmod 755 /$1 126 eval md_created_$1=created 127 fi 128} 129 130# DEBUGGING 131# 132# set -v 133 134# Figure out our interface and IP. 135# 136bootp_ifc="" 137bootp_ipa="" 138bootp_ipbca="" 139if [ ${dlv:=0} -ne 0 ] ; then 140 iflist=`ifconfig -l` 141 for i in ${iflist} ; do 142 set `ifconfig ${i}` 143 while [ $# -ge 1 ] ; do 144 if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then 145 bootp_ifc=${i} ; bootp_ipa=${2} ; shift 146 fi 147 if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then 148 bootp_ipbca=$2; shift 149 fi 150 shift 151 done 152 if [ "${bootp_ifc}" != "" ] ; then 153 break 154 fi 155 done 156 # Insert the directories passed with the T134 bootp cookie 157 # in the list of paths used for templates. 158 i="`/sbin/sysctl -n kern.bootp_cookie`" 159 [ "${i}" != "" ] && bootp_ipbca="${bootp_ipbca} ${i}" 160 161 echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}" 162fi 163 164# Figure out our NFS root path 165# 166set `mount -t nfs` 167while [ $# -ge 1 ] ; do 168 if [ "$2" = "on" -a "$3" = "/" ]; then 169 nfsroot="$1" 170 break 171 fi 172 shift 173done 174 175# The list of directories with template files 176templates="base default ${bootp_ipbca} ${bootp_ipa}" 177 178# The list of filesystems to umount after the copy 179to_umount="" 180 181# If /conf/diskless_remount exists, remount all of /conf. This allows 182# multiple roots to share the same conf files. 183if [ -d /conf -a -f /conf/diskless_remount ]; then 184 nfspt=`/bin/cat /conf/diskless_remount` 185 if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then 186 nfspt="${nfsroot}${nfspt}" 187 fi 188 mount_nfs $nfspt /conf 189 chkerr $? "mount_nfs $nfspt /conf" 190 to_umount="/conf" 191fi 192 193# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca}, 194# and /conf/${bootp_ipa}. For each subdirectory found within these 195# directories: 196# 197# - calculate memory filesystem sizes. If the subdirectory (prior to 198# NFS remounting) contains the file 'md_size', the contents specified 199# in 512 byte sectors will be used to size the memory filesystem. Otherwise 200# 8192 sectors (4MB) is used. 201# 202# - handle NFS remounts. If the subdirectory contains the file 203# diskless_remount, the contents of the file is NFS mounted over 204# the directory. For example /conf/base/etc/diskless_remount 205# might contain 'myserver:/etc'. NFS remounts allow you to avoid 206# having to dup your system directories in /conf. Your server must 207# be sure to export those filesystems -alldirs, however. 208# If the diskless_remount file contains a string beginning with a 209# '/' it is assumed that the local nfsroot should be prepended to 210# it before attemping to the remount. This allows the root to be 211# relocated without needing to change the remount files. 212# 213for i in ${templates} ; do 214 for j in /conf/$i/* ; do 215 # memory filesystem size specification 216 # 217 subdir=${j##*/} 218 if [ -d $j -a -f $j/md_size ]; then 219 eval md_size_$subdir=`cat $j/md_size` 220 fi 221 222 # remount 223 # 224 if [ -d $j -a -f $j/remount ]; then 225 nfspt=`/bin/cat $j/remount` 226 $nfspt $j 227 chkerr $? "$nfspt $j" 228 to_umount="${to_umount} $j" # XXX hope it is really a mount! 229 fi 230 231 # NFS remount 232 # 233 if [ -d $j -a -f $j/diskless_remount ]; then 234 nfspt=`/bin/cat $j/diskless_remount` 235 if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then 236 nfspt="${nfsroot}${nfspt}" 237 fi 238 mount_nfs $nfspt $j 239 chkerr $? "mount_nfs $nfspt $j" 240 to_umount="${to_umount} $j" 241 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