xref: /freebsd/libexec/rc/rc.initdiskless (revision 3c61bbebc4166ca209b9becfd1529298bac6ed66)
10696600cSBjoern A. Zeeb#!/bin/sh
20696600cSBjoern A. Zeeb#
30696600cSBjoern A. Zeeb# Copyright (c) 1999  Matt Dillon
40696600cSBjoern A. Zeeb# All rights reserved.
50696600cSBjoern A. Zeeb#
60696600cSBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
70696600cSBjoern A. Zeeb# modification, are permitted provided that the following conditions
80696600cSBjoern A. Zeeb# are met:
90696600cSBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
100696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
110696600cSBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
120696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
130696600cSBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
140696600cSBjoern A. Zeeb#
150696600cSBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
160696600cSBjoern A. Zeeb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
170696600cSBjoern A. Zeeb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
180696600cSBjoern A. Zeeb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
190696600cSBjoern A. Zeeb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
200696600cSBjoern A. Zeeb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
210696600cSBjoern A. Zeeb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
220696600cSBjoern A. Zeeb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
230696600cSBjoern A. Zeeb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
240696600cSBjoern A. Zeeb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
250696600cSBjoern A. Zeeb# SUCH DAMAGE.
260696600cSBjoern A. Zeeb#
270696600cSBjoern A. Zeeb
280696600cSBjoern A. Zeeb# On entry to this script the entire system consists of a read-only root
290696600cSBjoern A. Zeeb# mounted via NFS. The kernel has run BOOTP and configured an interface
300696600cSBjoern A. Zeeb# (otherwise it would not have been able to mount the NFS root!)
310696600cSBjoern A. Zeeb#
320696600cSBjoern A. Zeeb# We use the contents of /conf to create and populate memory filesystems
330696600cSBjoern A. Zeeb# that are mounted on top of this root to implement the writable
340696600cSBjoern A. Zeeb# (and host-specific) parts of the root filesystem, and other volatile
350696600cSBjoern A. Zeeb# filesystems.
360696600cSBjoern A. Zeeb#
370696600cSBjoern A. Zeeb# The hierarchy in /conf has the form /conf/T/M/ where M are directories
380696600cSBjoern A. Zeeb# for which memory filesystems will be created and filled,
390696600cSBjoern A. Zeeb# and T is one of the "template" directories below:
400696600cSBjoern A. Zeeb#
410696600cSBjoern A. Zeeb#  base		universal base, typically a replica of the original root;
420696600cSBjoern A. Zeeb#  default	secondary universal base, typically overriding some
430696600cSBjoern A. Zeeb#		of the files in the original root;
440696600cSBjoern A. Zeeb#  ${ipba}	where ${ipba} is the assigned broadcast IP address
450696600cSBjoern A. Zeeb#  bcast/${ipba} same as above
460696600cSBjoern A. Zeeb#  ${class}	where ${class} is a list of directories supplied by
470696600cSBjoern A. Zeeb#		bootp/dhcp through the T134 option.
480696600cSBjoern A. Zeeb#		${ipba} and ${class} are typically used to configure features
490696600cSBjoern A. Zeeb#		for group of diskless clients, or even individual features;
500696600cSBjoern A. Zeeb#  ${ip}	where ${ip} is the machine's assigned IP address, typically
510696600cSBjoern A. Zeeb#		used to set host-specific features;
520696600cSBjoern A. Zeeb#  ip/${ip}	same as above
530696600cSBjoern A. Zeeb#
540696600cSBjoern A. Zeeb# Template directories are scanned in the order they are listed above,
550696600cSBjoern A. Zeeb# with each successive directory overriding (merged into) the previous one;
560696600cSBjoern A. Zeeb# non-existing directories are ignored.  The subdirectory forms exist to
570696600cSBjoern A. Zeeb# help keep the top level /conf manageable in large installations.
580696600cSBjoern A. Zeeb#
590696600cSBjoern A. Zeeb# The existence of a directory /conf/T/M causes this script to create a
600696600cSBjoern A. Zeeb# memory filesystem mounted as /M on the client.
610696600cSBjoern A. Zeeb#
620696600cSBjoern A. Zeeb# Some files in /conf have special meaning, namely:
630696600cSBjoern A. Zeeb#
640696600cSBjoern A. Zeeb# Filename	Action
650696600cSBjoern A. Zeeb# ----------------------------------------------------------------
660696600cSBjoern A. Zeeb# /conf/T/M/remount
670696600cSBjoern A. Zeeb#		The contents of the file is a mount command. E.g. if
680696600cSBjoern A. Zeeb# 		/conf/1.2.3.4/foo/remount contains "mount -o ro /dev/ad0s3",
690696600cSBjoern A. Zeeb#		then /dev/ad0s3 will be mounted on /conf/1.2.3.4/foo/
700696600cSBjoern A. Zeeb#
710696600cSBjoern A. Zeeb# /conf/T/M/remount_optional
720696600cSBjoern A. Zeeb#		If this file exists, then failure to execute the mount
730696600cSBjoern A. Zeeb#		command contained in /conf/T/M/remount is non-fatal.
740696600cSBjoern A. Zeeb#
750696600cSBjoern A. Zeeb# /conf/T/M/remount_subdir
760696600cSBjoern A. Zeeb#		If this file exists, then the behaviour of /conf/T/M/remount
770696600cSBjoern A. Zeeb#		changes as follows:
780696600cSBjoern A. Zeeb#		 1. /conf/T/M/remount is invoked to mount the root of the
790696600cSBjoern A. Zeeb#		    filesystem where the configuration data exists on a
800696600cSBjoern A. Zeeb#		    temporary mountpoint.
810696600cSBjoern A. Zeeb#		 2. /conf/T/M/remount_subdir is then invoked to mount a
820696600cSBjoern A. Zeeb#		    *subdirectory* of the filesystem mounted by
830696600cSBjoern A. Zeeb#		    /conf/T/M/remount on /conf/T/M/.
840696600cSBjoern A. Zeeb#
850696600cSBjoern A. Zeeb# /conf/T/M/diskless_remount
860696600cSBjoern A. Zeeb#		The contents of the file points to an NFS filesystem,
870696600cSBjoern A. Zeeb#		possibly followed by mount_nfs options. If the server name
880696600cSBjoern A. Zeeb#		is omitted, the script will prepend the root path used when
890696600cSBjoern A. Zeeb#		booting. E.g. if you booted from foo.com:/path/to/root,
900696600cSBjoern A. Zeeb#		an entry for /conf/base/etc/diskless_remount could be any of
910696600cSBjoern A. Zeeb#			foo.com:/path/to/root/etc
920696600cSBjoern A. Zeeb#			/etc -o ro
930696600cSBjoern A. Zeeb#		Because mount_nfs understands ".." in paths, it is
940696600cSBjoern A. Zeeb#		possible to mount from locations above the NFS root with
950696600cSBjoern A. Zeeb#		paths such as "/../../etc".
960696600cSBjoern A. Zeeb#
970696600cSBjoern A. Zeeb# /conf/T/M/md_size
980696600cSBjoern A. Zeeb#		The contents of the file specifies the size of the memory
990696600cSBjoern A. Zeeb#		filesystem to be created, in 512 byte blocks.
1000696600cSBjoern A. Zeeb#		The default size is 10240 blocks (5MB). E.g. if
1010696600cSBjoern A. Zeeb#		/conf/base/etc/md_size contains "30000" then a 15MB MFS
1020696600cSBjoern A. Zeeb#		will be created. In case of multiple entries for the same
1030696600cSBjoern A. Zeeb#		directory M, the last one in the scanning order is used.
1040696600cSBjoern A. Zeeb#		NOTE: If you only need to create a memory filesystem but not
1050696600cSBjoern A. Zeeb#		initialize it from a template, it is preferable to specify
1060696600cSBjoern A. Zeeb#		it in fstab e.g. as  "md /tmp mfs -s=30m,rw 0 0"
1070696600cSBjoern A. Zeeb#
1080696600cSBjoern A. Zeeb# /conf/T/SUBDIR.cpio.gz
1090696600cSBjoern A. Zeeb#		The file is cpio'd into /SUBDIR (and a memory filesystem is
1100696600cSBjoern A. Zeeb#		created for /SUBDIR if necessary). The presence of this file
1110696600cSBjoern A. Zeeb#		prevents the copy from /conf/T/SUBDIR/
1120696600cSBjoern A. Zeeb#
113b4d24263SEugene Grosbein# /conf/T/M/extract
114b4d24263SEugene Grosbein#		This is alternative to SUBDIR.cpio.gz and remount.
115b4d24263SEugene Grosbein#		Similar to remount case, a memory filesystem is created
116b4d24263SEugene Grosbein#		for /M and initialized from a template but no mounting
117707a657cSEugene Grosbein#		performed. Instead, this file is run passing /M as single
118b4d24263SEugene Grosbein#		argument. It is expected to extract template override to /M
119b4d24263SEugene Grosbein#		using auxiliary storage found in some embedded systems
120b4d24263SEugene Grosbein#		having NVRAM too small to hold mountable file system.
121b4d24263SEugene Grosbein#
1220696600cSBjoern A. Zeeb# /conf/T/SUBDIR.remove
1230696600cSBjoern A. Zeeb#		The list of paths contained in the file are rm -rf'd
1240696600cSBjoern A. Zeeb#		relative to /SUBDIR.
1250696600cSBjoern A. Zeeb#
1260696600cSBjoern A. Zeeb# /conf/diskless_remount
1270696600cSBjoern A. Zeeb#		Similar to /conf/T/M/diskless_remount above, but allows
1280696600cSBjoern A. Zeeb#		all of /conf to be remounted.  This can be used to allow
1290696600cSBjoern A. Zeeb#		multiple roots to share the same /conf.
1300696600cSBjoern A. Zeeb#
1310696600cSBjoern A. Zeeb#
1320696600cSBjoern A. Zeeb# You will almost universally want to create the following files under /conf
1330696600cSBjoern A. Zeeb#
1340696600cSBjoern A. Zeeb# File					Content
1350696600cSBjoern A. Zeeb# ----------------------------		----------------------------------
1360696600cSBjoern A. Zeeb# /conf/base/etc/md_size		size of /etc filesystem
1370696600cSBjoern A. Zeeb# /conf/base/etc/diskless_remount	"/etc"
1380696600cSBjoern A. Zeeb# /conf/default/etc/rc.conf		generic diskless config parameters
1390696600cSBjoern A. Zeeb# /conf/default/etc/fstab		generic diskless fstab e.g. like this
1400696600cSBjoern A. Zeeb#
1410696600cSBjoern A. Zeeb#	foo:/root_part			/	nfs	ro		0 0
1420696600cSBjoern A. Zeeb#	foo:/usr_part			/usr	nfs     ro		0 0
1430696600cSBjoern A. Zeeb#	foo:/home_part			/home   nfs     rw      	0 0
1440696600cSBjoern A. Zeeb#	md				/tmp	mfs     -s=30m,rw	0 0
1450696600cSBjoern A. Zeeb#	md				/var	mfs	-s=30m,rw	0 0
1460696600cSBjoern A. Zeeb#	proc				/proc	procfs	rw		0 0
1470696600cSBjoern A. Zeeb#
1480696600cSBjoern A. Zeeb# plus, possibly, overrides for password files etc.
1490696600cSBjoern A. Zeeb#
1500696600cSBjoern A. Zeeb# NOTE!  /var, /tmp, and /dev will be typically created elsewhere, e.g.
1510696600cSBjoern A. Zeeb# as entries in the fstab as above.
1520696600cSBjoern A. Zeeb# Those filesystems should not be specified in /conf.
1530696600cSBjoern A. Zeeb#
1540696600cSBjoern A. Zeeb# (end of documentation, now get to the real code)
1550696600cSBjoern A. Zeeb
1560696600cSBjoern A. Zeebdlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
1570696600cSBjoern A. Zeeb
1580696600cSBjoern A. Zeeb# DEBUGGING
1590696600cSBjoern A. Zeeb# log something on stdout if verbose.
1600696600cSBjoern A. Zeebo_verbose=0     # set to 1 or 2 if you want more debugging
1610696600cSBjoern A. Zeeblog() {
1620696600cSBjoern A. Zeeb    [ ${o_verbose} -gt 0 ] && echo "*** $* ***"
1630696600cSBjoern A. Zeeb    [ ${o_verbose} -gt 1 ] && read -p "=== Press enter to continue" foo
1640696600cSBjoern A. Zeeb}
1650696600cSBjoern A. Zeeb
1660696600cSBjoern A. Zeeb# chkerr:
1670696600cSBjoern A. Zeeb#
1680696600cSBjoern A. Zeeb# Routine to check for error
1690696600cSBjoern A. Zeeb#
1700696600cSBjoern A. Zeeb#	checks error code and drops into shell on failure.
1710696600cSBjoern A. Zeeb#	if shell exits, terminates script as well as /etc/rc.
1720696600cSBjoern A. Zeeb#	if remount_optional exists under the mountpoint, skip this check.
1730696600cSBjoern A. Zeeb#
1740696600cSBjoern A. Zeebchkerr() {
1750696600cSBjoern A. Zeeb    lastitem () ( n=$(($# - 1)) ; shift $n ; echo $1 )
1760696600cSBjoern A. Zeeb    mountpoint="$(lastitem $2)"
177*3c61bbebSKeve Müller    if [ -r $mountpoint/remount_optional ]; then
178*3c61bbebSKeve Müller        echo "$2 failed: ignoring due to remount_optional"
179*3c61bbebSKeve Müller        return
180*3c61bbebSKeve Müller    fi
1810696600cSBjoern A. Zeeb    case $1 in
1820696600cSBjoern A. Zeeb    0)
1830696600cSBjoern A. Zeeb	;;
1840696600cSBjoern A. Zeeb    *)
1850696600cSBjoern A. Zeeb	echo "$2 failed: dropping into /bin/sh"
1860696600cSBjoern A. Zeeb	/bin/sh
1870696600cSBjoern A. Zeeb	# RESUME
1880696600cSBjoern A. Zeeb	;;
1890696600cSBjoern A. Zeeb    esac
1900696600cSBjoern A. Zeeb}
1910696600cSBjoern A. Zeeb
1920696600cSBjoern A. Zeeb# The list of filesystems to umount after the copy
1930696600cSBjoern A. Zeebto_umount=""
1940696600cSBjoern A. Zeeb
1950696600cSBjoern A. Zeebhandle_remount() { # $1 = mount point
1960696600cSBjoern A. Zeeb    local nfspt mountopts b
1970696600cSBjoern A. Zeeb    b=$1
1980696600cSBjoern A. Zeeb    log handle_remount $1
1990696600cSBjoern A. Zeeb    [ -d $b -a -f $b/diskless_remount ] || return
2000696600cSBjoern A. Zeeb    read nfspt mountopts < $b/diskless_remount
2010696600cSBjoern A. Zeeb    log "nfspt ${nfspt} mountopts ${mountopts}"
2020696600cSBjoern A. Zeeb    # prepend the nfs root if not present
2030696600cSBjoern A. Zeeb    [ `expr "$nfspt" : '\(.\)'` = "/" ] && nfspt="${nfsroot}${nfspt}"
2040696600cSBjoern A. Zeeb    mount_nfs $mountopts $nfspt $b
2050696600cSBjoern A. Zeeb    chkerr $? "mount_nfs $nfspt $b"
2060696600cSBjoern A. Zeeb    to_umount="$b ${to_umount}"
2070696600cSBjoern A. Zeeb}
2080696600cSBjoern A. Zeeb
2090696600cSBjoern A. Zeeb# Create a generic memory disk.
210088cc7d2SAlexander Ziaee# The 'auto' parameter will attempt to use tmpfs(4), falls back to md(4).
2110696600cSBjoern A. Zeeb# $1 is size in 512-byte sectors, $2 is the mount point.
2120696600cSBjoern A. Zeebmount_md() {
21326e8ed62SLev A. Serebryakov    if [ ${o_verbose} -gt 0 ] ; then
214bb411657SJose Luis Duran        /sbin/mdmfs -XL -S -s $1 auto $2
21526e8ed62SLev A. Serebryakov    else
216bb411657SJose Luis Duran        /sbin/mdmfs -S -s $1 auto $2
21726e8ed62SLev A. Serebryakov    fi
2180696600cSBjoern A. Zeeb}
2190696600cSBjoern A. Zeeb
2200696600cSBjoern A. Zeeb# Create the memory filesystem if it has not already been created
2210696600cSBjoern A. Zeeb#
2220696600cSBjoern A. Zeebcreate_md() {
2230696600cSBjoern A. Zeeb	[ "x`eval echo \\$md_created_$1`" = "x" ] || return # only once
2240696600cSBjoern A. Zeeb	if [ "x`eval echo \\$md_size_$1`" = "x" ]; then
2250696600cSBjoern A. Zeeb	    md_size=10240
2260696600cSBjoern A. Zeeb	else
2270696600cSBjoern A. Zeeb	    md_size=`eval echo \\$md_size_$1`
2280696600cSBjoern A. Zeeb	fi
2290696600cSBjoern A. Zeeb	log create_md $1 with size $md_size
2300696600cSBjoern A. Zeeb	mount_md $md_size /$1
2310696600cSBjoern A. Zeeb	/bin/chmod 755 /$1
2320696600cSBjoern A. Zeeb	eval md_created_$1=created
2330696600cSBjoern A. Zeeb}
2340696600cSBjoern A. Zeeb
2350696600cSBjoern A. Zeeb# DEBUGGING
2360696600cSBjoern A. Zeeb#
2370696600cSBjoern A. Zeeb# set -v
2380696600cSBjoern A. Zeeb
2390696600cSBjoern A. Zeeb# Figure out our interface and IP.
2400696600cSBjoern A. Zeeb#
2410696600cSBjoern A. Zeebbootp_ifc=""
2420696600cSBjoern A. Zeebbootp_ipa=""
2430696600cSBjoern A. Zeebbootp_ipbca=""
2440696600cSBjoern A. Zeebclass=""
2450696600cSBjoern A. Zeebif [ ${dlv:=0} -ne 0 ] ; then
2460696600cSBjoern A. Zeeb	iflist=`ifconfig -l`
2470696600cSBjoern A. Zeeb	for i in ${iflist} ; do
2480696600cSBjoern A. Zeeb	    set -- `ifconfig ${i}`
2490696600cSBjoern A. Zeeb	    while [ $# -ge 1 ] ; do
2500696600cSBjoern A. Zeeb		if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
2510696600cSBjoern A. Zeeb		    bootp_ifc=${i} ; bootp_ipa=${2} ; shift
2520696600cSBjoern A. Zeeb		fi
2530696600cSBjoern A. Zeeb		if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
2540696600cSBjoern A. Zeeb		    bootp_ipbca=$2; shift
2550696600cSBjoern A. Zeeb		fi
2560696600cSBjoern A. Zeeb		shift
2570696600cSBjoern A. Zeeb	    done
2580696600cSBjoern A. Zeeb	    if [ "${bootp_ifc}" != "" ] ; then
2590696600cSBjoern A. Zeeb		break
2600696600cSBjoern A. Zeeb	    fi
2610696600cSBjoern A. Zeeb	done
2620696600cSBjoern A. Zeeb	# Get the values passed with the T134 bootp cookie.
2630696600cSBjoern A. Zeeb	class="`/sbin/sysctl -qn kern.bootp_cookie`"
2640696600cSBjoern A. Zeeb
2650696600cSBjoern A. Zeeb	echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca} ${class}"
2660696600cSBjoern A. Zeebfi
2670696600cSBjoern A. Zeeb
2680696600cSBjoern A. Zeeblog Figure out our NFS root path
2690696600cSBjoern A. Zeeb#
2700696600cSBjoern A. Zeebset -- `mount -t nfs`
2710696600cSBjoern A. Zeebwhile [ $# -ge 1 ] ; do
2720696600cSBjoern A. Zeeb    if [ "$2" = "on" -a "$3" = "/" ]; then
2730696600cSBjoern A. Zeeb	nfsroot="$1"
2740696600cSBjoern A. Zeeb	break
2750696600cSBjoern A. Zeeb    fi
2760696600cSBjoern A. Zeeb    shift
2770696600cSBjoern A. Zeebdone
2780696600cSBjoern A. Zeeb
2790696600cSBjoern A. Zeeb# The list of directories with template files
2800696600cSBjoern A. Zeebtemplates="base default"
2810696600cSBjoern A. Zeebif [ -n "${bootp_ipbca}" ]; then
2820696600cSBjoern A. Zeeb	templates="${templates} ${bootp_ipbca} bcast/${bootp_ipbca}"
2830696600cSBjoern A. Zeebfi
2840696600cSBjoern A. Zeebif [ -n "${class}" ]; then
2850696600cSBjoern A. Zeeb	templates="${templates} ${class}"
2860696600cSBjoern A. Zeebfi
2870696600cSBjoern A. Zeebif [ -n "${bootp_ipa}" ]; then
2880696600cSBjoern A. Zeeb	templates="${templates} ${bootp_ipa} ip/${bootp_ipa}"
2890696600cSBjoern A. Zeebfi
2900696600cSBjoern A. Zeeb
2910696600cSBjoern A. Zeeb# If /conf/diskless_remount exists, remount all of /conf.
2920696600cSBjoern A. Zeebhandle_remount /conf
2930696600cSBjoern A. Zeeb
2940696600cSBjoern A. Zeeb# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca},
2950696600cSBjoern A. Zeeb# and /conf/${bootp_ipa}.  For each subdirectory found within these
2960696600cSBjoern A. Zeeb# directories:
2970696600cSBjoern A. Zeeb#
2980696600cSBjoern A. Zeeb# - calculate memory filesystem sizes.  If the subdirectory (prior to
2990696600cSBjoern A. Zeeb#   NFS remounting) contains the file 'md_size', the contents specified
3000696600cSBjoern A. Zeeb#   in 512 byte sectors will be used to size the memory filesystem.  Otherwise
3010696600cSBjoern A. Zeeb#   8192 sectors (4MB) is used.
3020696600cSBjoern A. Zeeb#
3030696600cSBjoern A. Zeeb# - handle NFS remounts.  If the subdirectory contains the file
3040696600cSBjoern A. Zeeb#   diskless_remount, the contents of the file is NFS mounted over
3050696600cSBjoern A. Zeeb#   the directory.  For example /conf/base/etc/diskless_remount
3060696600cSBjoern A. Zeeb#   might contain 'myserver:/etc'.  NFS remounts allow you to avoid
3070696600cSBjoern A. Zeeb#   having to dup your system directories in /conf.  Your server must
3080696600cSBjoern A. Zeeb#   be sure to export those filesystems -alldirs, however.
3090696600cSBjoern A. Zeeb#   If the diskless_remount file contains a string beginning with a
3100696600cSBjoern A. Zeeb#   '/' it is assumed that the local nfsroot should be prepended to
31130985d19SGordon Bergling#   it before attempting to the remount.  This allows the root to be
3120696600cSBjoern A. Zeeb#   relocated without needing to change the remount files.
3130696600cSBjoern A. Zeeb#
3140696600cSBjoern A. Zeeblog "templates are ${templates}"
3150696600cSBjoern A. Zeebfor i in ${templates} ; do
3160696600cSBjoern A. Zeeb    for j in /conf/$i/* ; do
3170696600cSBjoern A. Zeeb	[ -d $j ] || continue
3180696600cSBjoern A. Zeeb
3190696600cSBjoern A. Zeeb	# memory filesystem size specification
3200696600cSBjoern A. Zeeb	subdir=${j##*/}
3210696600cSBjoern A. Zeeb	[ -f $j/md_size ] && eval md_size_$subdir=`cat $j/md_size`
3220696600cSBjoern A. Zeeb
3230696600cSBjoern A. Zeeb	# remount. Beware, the command is in the file itself!
3240696600cSBjoern A. Zeeb	if [ -f $j/remount ]; then
3250696600cSBjoern A. Zeeb	    if [ -f $j/remount_subdir ]; then
3260696600cSBjoern A. Zeeb		k="/conf.tmp/$i/$subdir"
3270696600cSBjoern A. Zeeb		[ -d $k ] || continue
3280696600cSBjoern A. Zeeb
3290696600cSBjoern A. Zeeb		# Mount the filesystem root where the config data is
3300696600cSBjoern A. Zeeb		# on the temporary mount point.
3310696600cSBjoern A. Zeeb		nfspt=`/bin/cat $j/remount`
3320696600cSBjoern A. Zeeb		$nfspt $k
3330696600cSBjoern A. Zeeb		chkerr $? "$nfspt $k"
3340696600cSBjoern A. Zeeb
3350696600cSBjoern A. Zeeb		# Now use a nullfs mount to get the data where we
3360696600cSBjoern A. Zeeb		# really want to see it.
3370696600cSBjoern A. Zeeb		remount_subdir=`/bin/cat $j/remount_subdir`
3380696600cSBjoern A. Zeeb		remount_subdir_cmd="mount -t nullfs $k/$remount_subdir"
3390696600cSBjoern A. Zeeb
3400696600cSBjoern A. Zeeb		$remount_subdir_cmd $j
3410696600cSBjoern A. Zeeb		chkerr $? "$remount_subdir_cmd $j"
3420696600cSBjoern A. Zeeb
3430696600cSBjoern A. Zeeb		# XXX check order -- we must force $k to be unmounted
3440696600cSBjoern A. Zeeb		# after j, as j depends on k.
3450696600cSBjoern A. Zeeb		to_umount="$j $k ${to_umount}"
3460696600cSBjoern A. Zeeb	    else
3470696600cSBjoern A. Zeeb		nfspt=`/bin/cat $j/remount`
3480696600cSBjoern A. Zeeb		$nfspt $j
3490696600cSBjoern A. Zeeb		chkerr $? "$nfspt $j"
3500696600cSBjoern A. Zeeb		to_umount="$j ${to_umount}" # XXX hope it is really a mount!
3510696600cSBjoern A. Zeeb	    fi
3520696600cSBjoern A. Zeeb	fi
3530696600cSBjoern A. Zeeb
3540696600cSBjoern A. Zeeb	# NFS remount
3550696600cSBjoern A. Zeeb	handle_remount $j
3560696600cSBjoern A. Zeeb    done
3570696600cSBjoern A. Zeebdone
3580696600cSBjoern A. Zeeb
3590696600cSBjoern A. Zeeb# - Create all required MFS filesystems and populate them from
3600696600cSBjoern A. Zeeb#   our templates.  Support both a direct template and a dir.cpio.gz
361b4d24263SEugene Grosbein#   archive. Support for auxiliary NVRAM. Support dir.remove files containing
362b4d24263SEugene Grosbein#   a list of relative paths to remove.
3630696600cSBjoern A. Zeeb#
3640696600cSBjoern A. Zeeb# The dir.cpio.gz form is there to make the copy process more efficient,
3650696600cSBjoern A. Zeeb# so if the cpio archive is present, it prevents the files from dir/
3660696600cSBjoern A. Zeeb# from being copied.
3670696600cSBjoern A. Zeeb
368a04ca1c2SJose Luis DuranPATH=${PATH}:/rescue
36993b78182SCheng Cui
3700696600cSBjoern A. Zeebfor i in ${templates} ; do
3710696600cSBjoern A. Zeeb    for j in /conf/$i/* ; do
3720696600cSBjoern A. Zeeb	subdir=${j##*/}
3730696600cSBjoern A. Zeeb	if [ -d $j -a ! -f $j.cpio.gz  ]; then
3740696600cSBjoern A. Zeeb	    create_md $subdir
3750696600cSBjoern A. Zeeb	    cp -Rp $j/ /$subdir
3760696600cSBjoern A. Zeeb	fi
3770696600cSBjoern A. Zeeb    done
3780696600cSBjoern A. Zeeb    for j in /conf/$i/*.cpio.gz ; do
3790696600cSBjoern A. Zeeb	subdir=${j%*.cpio.gz}
3800696600cSBjoern A. Zeeb	subdir=${subdir##*/}
3810696600cSBjoern A. Zeeb	if [ -f $j ]; then
3820696600cSBjoern A. Zeeb	    create_md $subdir
3830696600cSBjoern A. Zeeb	    echo "Loading /$subdir from cpio archive $j"
38493b78182SCheng Cui	    (cd / ; tar -xpf $j)
3850696600cSBjoern A. Zeeb	fi
3860696600cSBjoern A. Zeeb    done
387b4d24263SEugene Grosbein    for j in /conf/$i/*/extract ; do
388b4d24263SEugene Grosbein	if [ -x $j ]; then
389b4d24263SEugene Grosbein	    subdir=${j%*/extract}
390b4d24263SEugene Grosbein	    subdir=${subdir##*/}
391b4d24263SEugene Grosbein	    create_md $subdir
392b4d24263SEugene Grosbein	    echo "Loading /$subdir using auxiliary command $j"
393b4d24263SEugene Grosbein	    $j /$subdir
394b4d24263SEugene Grosbein	fi
395b4d24263SEugene Grosbein    done
3960696600cSBjoern A. Zeeb    for j in /conf/$i/*.remove ; do
3970696600cSBjoern A. Zeeb	subdir=${j%*.remove}
3980696600cSBjoern A. Zeeb	subdir=${subdir##*/}
3990696600cSBjoern A. Zeeb	if [ -f $j ]; then
4000696600cSBjoern A. Zeeb	    # doubly sure it is a memory disk before rm -rf'ing
4010696600cSBjoern A. Zeeb	    create_md $subdir
4020696600cSBjoern A. Zeeb	    (cd /$subdir; rm -rf `/bin/cat $j`)
4030696600cSBjoern A. Zeeb	fi
4040696600cSBjoern A. Zeeb    done
4050696600cSBjoern A. Zeebdone
4060696600cSBjoern A. Zeeb
4070696600cSBjoern A. Zeeb# umount partitions used to fill the memory filesystems
4080696600cSBjoern A. Zeeb[ -n "${to_umount}" ] && umount $to_umount
409