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$ 28 29# On entry to this script the entire system consists of a read-only root 30# mounted via NFS. The kernel has run BOOTP and configured an interface 31# (otherwise it would not have been able to mount the NFS root!) 32# 33# We use the contents of /conf to create and populate memory filesystems 34# that are mounted on top of this root to implement the writable 35# (and host-specific) parts of the root filesystem, and other volatile 36# filesystems. 37# 38# The hierarchy in /conf has the form /conf/T/M/ where M are directories 39# for which memory filesystems will be created and filled, 40# and T is one of the "template" directories below: 41# 42# base universal base, typically a replica of the original root; 43# default secondary universal base, typically overriding some 44# of the files in the original root; 45# ${ipba} where ${ipba} is the assigned broadcast IP address 46# bcast/${ipba} same as above 47# ${class} where ${class} is a list of directories supplied by 48# bootp/dhcp through the T134 option. 49# ${ipba} and ${class} are typically used to configure features 50# for group of diskless clients, or even individual features; 51# ${ip} where ${ip} is the machine's assigned IP address, typically 52# used to set host-specific features; 53# ip/${ip} same as above 54# 55# Template directories are scanned in the order they are listed above, 56# with each successive directory overriding (merged into) the previous one; 57# non-existing directories are ignored. The subdirectory forms exist to 58# help keep the top level /conf manageable in large installations. 59# 60# The existence of a directory /conf/T/M causes this script to create a 61# memory filesystem mounted as /M on the client. 62# 63# Some files in /conf have special meaning, namely: 64# 65# Filename Action 66# ---------------------------------------------------------------- 67# /conf/T/M/remount 68# The contents of the file is a mount command. E.g. if 69# /conf/1.2.3.4/foo/remount contains "mount -o ro /dev/ad0s3", 70# then /dev/ad0s3 will be mounted on /conf/1.2.3.4/foo/ 71# 72# /conf/T/M/remount_optional 73# If this file exists, then failure to execute the mount 74# command contained in /conf/T/M/remount is non-fatal. 75# 76# /conf/T/M/remount_subdir 77# If this file exists, then the behaviour of /conf/T/M/remount 78# changes as follows: 79# 1. /conf/T/M/remount is invoked to mount the root of the 80# filesystem where the configuration data exists on a 81# temporary mountpoint. 82# 2. /conf/T/M/remount_subdir is then invoked to mount a 83# *subdirectory* of the filesystem mounted by 84# /conf/T/M/remount on /conf/T/M/. 85# 86# /conf/T/M/diskless_remount 87# The contents of the file points to an NFS filesystem, 88# possibly followed by mount_nfs options. If the server name 89# is omitted, the script will prepend the root path used when 90# booting. E.g. if you booted from foo.com:/path/to/root, 91# an entry for /conf/base/etc/diskless_remount could be any of 92# foo.com:/path/to/root/etc 93# /etc -o ro 94# Because mount_nfs understands ".." in paths, it is 95# possible to mount from locations above the NFS root with 96# paths such as "/../../etc". 97# 98# /conf/T/M/md_size 99# The contents of the file specifies the size of the memory 100# filesystem to be created, in 512 byte blocks. 101# The default size is 10240 blocks (5MB). E.g. if 102# /conf/base/etc/md_size contains "30000" then a 15MB MFS 103# will be created. In case of multiple entries for the same 104# directory M, the last one in the scanning order is used. 105# NOTE: If you only need to create a memory filesystem but not 106# initialize it from a template, it is preferable to specify 107# it in fstab e.g. as "md /tmp mfs -s=30m,rw 0 0" 108# 109# /conf/T/SUBDIR.cpio.gz 110# The file is cpio'd into /SUBDIR (and a memory filesystem is 111# created for /SUBDIR if necessary). The presence of this file 112# prevents the copy from /conf/T/SUBDIR/ 113# 114# /conf/T/M/extract 115# This is alternative to SUBDIR.cpio.gz and remount. 116# Similar to remount case, a memory filesystem is created 117# for /M and initialized from a template but no mounting 118# performed. Instead, this file is run passing /M as single 119# argument. It is expected to extract template override to /M 120# using auxiliary storage found in some embedded systems 121# having NVRAM too small to hold mountable file system. 122# 123# /conf/T/SUBDIR.remove 124# The list of paths contained in the file are rm -rf'd 125# relative to /SUBDIR. 126# 127# /conf/diskless_remount 128# Similar to /conf/T/M/diskless_remount above, but allows 129# all of /conf to be remounted. This can be used to allow 130# multiple roots to share the same /conf. 131# 132# 133# You will almost universally want to create the following files under /conf 134# 135# File Content 136# ---------------------------- ---------------------------------- 137# /conf/base/etc/md_size size of /etc filesystem 138# /conf/base/etc/diskless_remount "/etc" 139# /conf/default/etc/rc.conf generic diskless config parameters 140# /conf/default/etc/fstab generic diskless fstab e.g. like this 141# 142# foo:/root_part / nfs ro 0 0 143# foo:/usr_part /usr nfs ro 0 0 144# foo:/home_part /home nfs rw 0 0 145# md /tmp mfs -s=30m,rw 0 0 146# md /var mfs -s=30m,rw 0 0 147# proc /proc procfs rw 0 0 148# 149# plus, possibly, overrides for password files etc. 150# 151# NOTE! /var, /tmp, and /dev will be typically created elsewhere, e.g. 152# as entries in the fstab as above. 153# Those filesystems should not be specified in /conf. 154# 155# (end of documentation, now get to the real code) 156 157dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` 158 159# DEBUGGING 160# log something on stdout if verbose. 161o_verbose=0 # set to 1 or 2 if you want more debugging 162log() { 163 [ ${o_verbose} -gt 0 ] && echo "*** $* ***" 164 [ ${o_verbose} -gt 1 ] && read -p "=== Press enter to continue" foo 165} 166 167# chkerr: 168# 169# Routine to check for error 170# 171# checks error code and drops into shell on failure. 172# if shell exits, terminates script as well as /etc/rc. 173# if remount_optional exists under the mountpoint, skip this check. 174# 175chkerr() { 176 lastitem () ( n=$(($# - 1)) ; shift $n ; echo $1 ) 177 mountpoint="$(lastitem $2)" 178 [ -r $mountpoint/remount_optional ] && ( echo "$2 failed: ignoring due to remount_optional" ; return ) 179 case $1 in 180 0) 181 ;; 182 *) 183 echo "$2 failed: dropping into /bin/sh" 184 /bin/sh 185 # RESUME 186 ;; 187 esac 188} 189 190# The list of filesystems to umount after the copy 191to_umount="" 192 193handle_remount() { # $1 = mount point 194 local nfspt mountopts b 195 b=$1 196 log handle_remount $1 197 [ -d $b -a -f $b/diskless_remount ] || return 198 read nfspt mountopts < $b/diskless_remount 199 log "nfspt ${nfspt} mountopts ${mountopts}" 200 # prepend the nfs root if not present 201 [ `expr "$nfspt" : '\(.\)'` = "/" ] && nfspt="${nfsroot}${nfspt}" 202 mount_nfs $mountopts $nfspt $b 203 chkerr $? "mount_nfs $nfspt $b" 204 to_umount="$b ${to_umount}" 205} 206 207# Create a generic memory disk. 208# The 'auto' parameter will attempt to use tmpfs(5), falls back to md(4). 209# $1 is size in 512-byte sectors, $2 is the mount point. 210mount_md() { 211 /sbin/mdmfs -s $1 auto $2 212} 213 214# Create the memory filesystem if it has not already been created 215# 216create_md() { 217 [ "x`eval echo \\$md_created_$1`" = "x" ] || return # only once 218 if [ "x`eval echo \\$md_size_$1`" = "x" ]; then 219 md_size=10240 220 else 221 md_size=`eval echo \\$md_size_$1` 222 fi 223 log create_md $1 with size $md_size 224 mount_md $md_size /$1 225 /bin/chmod 755 /$1 226 eval md_created_$1=created 227} 228 229# DEBUGGING 230# 231# set -v 232 233# Figure out our interface and IP. 234# 235bootp_ifc="" 236bootp_ipa="" 237bootp_ipbca="" 238class="" 239if [ ${dlv:=0} -ne 0 ] ; then 240 iflist=`ifconfig -l` 241 for i in ${iflist} ; do 242 set -- `ifconfig ${i}` 243 while [ $# -ge 1 ] ; do 244 if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then 245 bootp_ifc=${i} ; bootp_ipa=${2} ; shift 246 fi 247 if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then 248 bootp_ipbca=$2; shift 249 fi 250 shift 251 done 252 if [ "${bootp_ifc}" != "" ] ; then 253 break 254 fi 255 done 256 # Get the values passed with the T134 bootp cookie. 257 class="`/sbin/sysctl -qn kern.bootp_cookie`" 258 259 echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca} ${class}" 260fi 261 262log Figure out our NFS root path 263# 264set -- `mount -t nfs` 265while [ $# -ge 1 ] ; do 266 if [ "$2" = "on" -a "$3" = "/" ]; then 267 nfsroot="$1" 268 break 269 fi 270 shift 271done 272 273# The list of directories with template files 274templates="base default" 275if [ -n "${bootp_ipbca}" ]; then 276 templates="${templates} ${bootp_ipbca} bcast/${bootp_ipbca}" 277fi 278if [ -n "${class}" ]; then 279 templates="${templates} ${class}" 280fi 281if [ -n "${bootp_ipa}" ]; then 282 templates="${templates} ${bootp_ipa} ip/${bootp_ipa}" 283fi 284 285# If /conf/diskless_remount exists, remount all of /conf. 286handle_remount /conf 287 288# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca}, 289# and /conf/${bootp_ipa}. For each subdirectory found within these 290# directories: 291# 292# - calculate memory filesystem sizes. If the subdirectory (prior to 293# NFS remounting) contains the file 'md_size', the contents specified 294# in 512 byte sectors will be used to size the memory filesystem. Otherwise 295# 8192 sectors (4MB) is used. 296# 297# - handle NFS remounts. If the subdirectory contains the file 298# diskless_remount, the contents of the file is NFS mounted over 299# the directory. For example /conf/base/etc/diskless_remount 300# might contain 'myserver:/etc'. NFS remounts allow you to avoid 301# having to dup your system directories in /conf. Your server must 302# be sure to export those filesystems -alldirs, however. 303# If the diskless_remount file contains a string beginning with a 304# '/' it is assumed that the local nfsroot should be prepended to 305# it before attemping to the remount. This allows the root to be 306# relocated without needing to change the remount files. 307# 308log "templates are ${templates}" 309for i in ${templates} ; do 310 for j in /conf/$i/* ; do 311 [ -d $j ] || continue 312 313 # memory filesystem size specification 314 subdir=${j##*/} 315 [ -f $j/md_size ] && eval md_size_$subdir=`cat $j/md_size` 316 317 # remount. Beware, the command is in the file itself! 318 if [ -f $j/remount ]; then 319 if [ -f $j/remount_subdir ]; then 320 k="/conf.tmp/$i/$subdir" 321 [ -d $k ] || continue 322 323 # Mount the filesystem root where the config data is 324 # on the temporary mount point. 325 nfspt=`/bin/cat $j/remount` 326 $nfspt $k 327 chkerr $? "$nfspt $k" 328 329 # Now use a nullfs mount to get the data where we 330 # really want to see it. 331 remount_subdir=`/bin/cat $j/remount_subdir` 332 remount_subdir_cmd="mount -t nullfs $k/$remount_subdir" 333 334 $remount_subdir_cmd $j 335 chkerr $? "$remount_subdir_cmd $j" 336 337 # XXX check order -- we must force $k to be unmounted 338 # after j, as j depends on k. 339 to_umount="$j $k ${to_umount}" 340 else 341 nfspt=`/bin/cat $j/remount` 342 $nfspt $j 343 chkerr $? "$nfspt $j" 344 to_umount="$j ${to_umount}" # XXX hope it is really a mount! 345 fi 346 fi 347 348 # NFS remount 349 handle_remount $j 350 done 351done 352 353# - Create all required MFS filesystems and populate them from 354# our templates. Support both a direct template and a dir.cpio.gz 355# archive. Support for auxiliary NVRAM. Support dir.remove files containing 356# a list of relative paths to remove. 357# 358# The dir.cpio.gz form is there to make the copy process more efficient, 359# so if the cpio archive is present, it prevents the files from dir/ 360# from being copied. 361 362for i in ${templates} ; do 363 for j in /conf/$i/* ; do 364 subdir=${j##*/} 365 if [ -d $j -a ! -f $j.cpio.gz ]; then 366 create_md $subdir 367 cp -Rp $j/ /$subdir 368 fi 369 done 370 for j in /conf/$i/*.cpio.gz ; do 371 subdir=${j%*.cpio.gz} 372 subdir=${subdir##*/} 373 if [ -f $j ]; then 374 create_md $subdir 375 echo "Loading /$subdir from cpio archive $j" 376 (cd / ; /rescue/tar -xpf $j) 377 fi 378 done 379 for j in /conf/$i/*/extract ; do 380 if [ -x $j ]; then 381 subdir=${j%*/extract} 382 subdir=${subdir##*/} 383 create_md $subdir 384 echo "Loading /$subdir using auxiliary command $j" 385 $j /$subdir 386 fi 387 done 388 for j in /conf/$i/*.remove ; do 389 subdir=${j%*.remove} 390 subdir=${subdir##*/} 391 if [ -f $j ]; then 392 # doubly sure it is a memory disk before rm -rf'ing 393 create_md $subdir 394 (cd /$subdir; rm -rf `/bin/cat $j`) 395 fi 396 done 397done 398 399# umount partitions used to fill the memory filesystems 400[ -n "${to_umount}" ] && umount $to_umount 401