From eb7bf003063f225217f11b267f044e253343145c Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Sun, 22 Dec 2002 22:18:41 +0000 Subject: [PATCH] Do not unconditionally load the configuration files for the RCNG case. Instead, load them as part of the rc.d system. This allows us to prioritize the initidiskless script so it runs before the configuration files are loaded and allows us to get rid of the exit 2 hack in /etc/rc. The exit 2 never worked anyway since it did not unset the prior configuration, causing the diskless code to not operate properly. Do a major cleanup and revamping of the diskless code for RCNG. This will be backported to the non-RCNG scripts as well as -stable. With suggestions from: Mike Makonnen MFC after: 7 days --- etc/rc | 14 +-- etc/rc.d/devd | 1 + etc/rc.d/diskless | 108 ++++++++++++++------ etc/rc.d/initdiskless | 233 ++++++++++++++++++++++++++++-------------- etc/rc.d/rcconf | 12 +++ etc/rc.d/resolv | 108 ++++++++++++++------ etc/rc.d/tmp | 108 ++++++++++++++------ etc/rc.d/var | 108 ++++++++++++++------ etc/rc.initdiskless | 233 ++++++++++++++++++++++++++++-------------- 9 files changed, 638 insertions(+), 287 deletions(-) create mode 100644 etc/rc.d/rcconf diff --git a/etc/rc b/etc/rc index 636fdbc6af7a..3a660e9e7303 100644 --- a/etc/rc +++ b/etc/rc @@ -69,11 +69,10 @@ case ${rc_ng} in [Yy][Ee][Ss]) . /etc/rc.subr - # Load system configuration files. The 'XXX' is there because - # the function requires an argument that we don't need to use. + # Note: the system configuration files are loaded as part of + # the RCNG system (rc.d/rccond). Do not load them here as it may + # interfere with diskless booting. # - load_rc_config 'XXX' - if [ "$1" = autoboot ]; then autoboot=yes _boot="faststart" @@ -88,13 +87,6 @@ case ${rc_ng} in for _rc_elem in ${files}; do run_rc_script ${_rc_elem} ${_boot} - # Local FreeBSD hack to reload the rc.conf on - # a return status of 2 - if [ $? -eq 2 ]; then - unset _rc_conf_loaded - load_rc_config 'XXX' - echo "Reloading rc.conf." - fi done echo '' diff --git a/etc/rc.d/devd b/etc/rc.d/devd index 9636eaac4cba..659ca711213d 100644 --- a/etc/rc.d/devd +++ b/etc/rc.d/devd @@ -3,6 +3,7 @@ # $FreeBSD$ # +# REQUIRE: rcconf # PROVIDE: devd # BEFORE: disks # KEYWORD: FreeBSD diff --git a/etc/rc.d/diskless b/etc/rc.d/diskless index 647e3b7acb7e..e74d0bdeec68 100644 --- a/etc/rc.d/diskless +++ b/etc/rc.d/diskless @@ -28,7 +28,7 @@ # # PROVIDE: diskless -# REQUIRE: initdiskless mountcritlocal +# REQUIRE: initdiskless rcconf mountcritlocal # BEFORE: addswap random # KEYWORD: FreeBSD @@ -42,14 +42,12 @@ name="diskless2" # as close as possible between 5-current and 4-stable. # $1 = size # $2 = mount point -# $3 = md unit number (ignored in pre 5.0 systems) -# $4 = (optional) bytes-per-inode -mount_md() -{ - if [ -n "$4" ]; then - bpi="-i $4" +# $3 = (optional) bytes-per-inode +mount_md() { + if [ -n "$3" ]; then + bpi="-i $3" fi - /sbin/mdmfs $bpi -s $1 -M md$3 $2 + /sbin/mdmfs $bpi -s $1 -M md $2 } # If there is a global system configuration file, suck it in. @@ -64,16 +62,36 @@ elif [ -r /etc/rc.conf ]; then . /etc/rc.conf fi -echo "+++ mount_md of /var" -mount_md ${varsize:=32m} /var 1 +# If we do not have a writable /var, create a memory +# filesystem for /var. We don't have /usr yet so +# use mkdir instead of touch to test. We want mount +# to record its mounts so we have to make sure /var/db +# exists before doing the mount -a. +# +if (/bin/mkdir /var/.diskless 2> /dev/null); then + rmdir /var/.diskless +else + echo "+++ mount_md of /var" + mount_md ${varsize:=32m} /var +fi +if [ ! -d /var/db ]; then + mkdir /var/db +fi + +# Now we need the rest of our mounts, particularly /usr +# +mount -a # chown and chgrp are in /usr + +# Populate /var +# echo "+++ populate /var using /etc/mtree/BSD.var.dist" -/usr/sbin/mtree -deU -f /etc/mtree/BSD.var.dist -p /var +/usr/sbin/mtree -deU -f /etc/mtree/BSD.var.dist -p /var > /dev/null case ${sendmail_enable} in [Nn][Oo][Nn][Ee]) ;; *) - /usr/sbin/mtree -deU -f /etc/mtree/BSD.sendmail.dist -p / + /usr/sbin/mtree -deU -f /etc/mtree/BSD.sendmail.dist -p / > /dev/null ;; esac @@ -86,34 +104,62 @@ fi echo "+++ create lastlog" /usr/bin/touch /var/log/lastlog -mount -a # chown and chgrp are in /usr - -# Since we are starting with a very fresh /etc on an MFS: -if [ -d /conf/default/etc ]; then - newaliases -fi - +# Make sure our aliases database is uptodate, the aliases may have +# been overriden in /conf. # +/usr/bin/newaliases + # XXX make sure to create one dir for each printer as requested by lpd # - -# If /tmp is a symlink, assume it points to somewhere writable, like -# /var/tmp, otherwise, use a small memory filesystem for /tmp. +# If we do not have a writable /tmp, create a memory +# filesystem for /tmp. If /tmp is a symlink (e.g. to /var/tmp, +# then it should already be writable). # -# XXX: mtree runs too early to create any directories needed in /tmp, -# so if /var/tmp == /tmp, then you don't get a vi.recover. -# -if [ ! -L /tmp ]; then - mount_md ${tmpsize:=64m} /tmp 2 - chmod 01777 /tmp +if (/bin/mkdir /tmp/.diskless 2> /dev/null); then + rmdir /tmp/.diskless +else + if [ -h /tmp ]; then + echo "*** /tmp is a symlink to a non-writable area!" + echo "dropping into shell, ^D to continue anyway." + /bin/sh + else + mount_md ${tmpsize:=20480} /tmp + chmod 01777 /tmp + fi fi -if sysctl vfs.devfs.generation > /dev/null 2>&1; then +if sysctl vfs.devfs.generation > /dev/null 2>&1 ; then # we have DEVFS, no worries... true +elif (/bin/mkdir /dev/.diskless 2> /dev/null); then + # if /dev is writable assume it has already been populated + # via rc.diskless1 + # + rmdir /dev/.diskless else - # extract a list of device entries, then copy them to a writable fs (cd /; find -x dev | cpio -o -H newc) > /tmp/dev.tmp - mount_md 4096 /dev 3 512 + mount_md 4096 /dev 512 (cd /; cpio -i -H newc -d < /tmp/dev.tmp) + rm -f /tmp/dev.tmp fi + +# generate our hostname +# +if [ -z "`hostname -s`" ]; then + hostname=`/usr/bin/kenv dhcp.host-name` + hostname $hostname + echo "Hostname is $hostname" +fi + +# if the info is available via dhcp/kenv +# build the resolv.conf +# +if [ ! -e /etc/resolv.conf ]; then + echo domain `/usr/bin/kenv dhcp.domain-name` > /etc/resolv.conf + + set `/usr/bin/kenv dhcp.domain-name-servers` + for ns in `IFS=','; echo $*`; do + echo nameserver $ns >> /etc/resolv.conf; + done +fi + diff --git a/etc/rc.d/initdiskless b/etc/rc.d/initdiskless index faa2acb6e297..5ddc768a43c8 100644 --- a/etc/rc.d/initdiskless +++ b/etc/rc.d/initdiskless @@ -26,25 +26,62 @@ # # $FreeBSD$ # - # PROVIDE: initdiskless # KEYWORD: FreeBSD + +# On entry to this script the entire system consists of a read-only root +# mounted via NFS. We use the contents of /conf to create and populate +# memory filesystems. The kernel has run BOOTP and configured an interface +# (otherwise it would not have been able to mount the NFS root!) +# +# The following directories are scanned. Each sucessive directory overrides +# (is merged into) the previous one. +# +# /conf/base universal base +# /conf/default modified by a secondary universal base +# /conf/${ipba} modified based on the assigned broadcast IP +# /conf/${ip} modified based on the machine's assigned IP +# +# Each of these directories may contain any number of subdirectories which +# represent directories in / on the diskless machine. The existance of +# these subdirectories causes this script to create a MEMORY FILESYSTEM for +# /. For example, if /conf/base/etc exists then a +# memory filesystem will be created for /etc. +# +# If a subdirectory contains the file 'diskless_remount' the contents of +# the file is used to remount the subdirectory prior to it being copied to +# the memory filesystem. For example, if /conf/base/etc/diskless_remount +# contains the string 'my.server.com:/etc' then my.server.com:/etc will be +# mounted in place of the subdirectory. This allows you to avoid making +# duplicates of system directories in /conf. +# +# If a subdirectory contains the file 'md_size', the contents of the +# file is used to determine the size of the memory filesystem, in 512 +# byte sectors. The default is 8192 (4MB). You only have to specify an +# md_size if the default doesn't work for you (i.e. if it is too big or +# too small). Note that in -current the default is 4096 (2MB). For +# example, /conf/base/etc/md_size might contain '16384'. +# +# If /conf//SUBDIR.cpio.gz exists, the file is cpio'd into +# the specified /SUBDIR (and a memory filesystem is created for /SUBDIR +# if necessary). +# +# If /conf//SUBDIR.remove exists, the file contains a list +# of paths which are rm -rf'd relative to /SUBDIR. +# +# You will almost universally want to create a /conf/base/etc containing +# a diskless_remount and possibly an md_size file. You will then almost +# universally want to override rc.conf, rc.local, and fstab by creating +# /conf/default/etc/{rc.conf,rc.local,fstab}. Your fstab should be sure +# to mount a /usr... typically an NFS readonly /usr. +# +# NOTE! rc.diskless2 will create /var, /tmp, and /dev. Those filesystems +# should not be specified in /conf. At least not yet. + dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` [ ${dlv:=0} -eq 0 ] && exit 0 -# -# BOOTP has mounted / for us. Assume a read-only mount. We must then -# - figure out our IP by querying the interface -# - mount /etc as an MFS -# - populate /etc from /conf/default version -# - override files in /etc with files from /conf/*/etc where -# '*' is default, netmask of client, ip-address of client -# -# The operator is in charge of setting /conf/*/etc/* things as appropriate. -# Typically rc.conf and fstab need to be changed, but possibly also other -# files such as inetd.conf etc. - # chkerr: # # Routine to check for error @@ -52,22 +89,37 @@ dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` # checks error code and drops into shell on failure. # if shell exits, terminates script as well as /etc/rc. # -chkerr() -{ - case $1 in - 0) - ;; - *) - echo "$2 failed: dropping into /bin/sh" - /bin/sh - # RESUME - ;; - esac +chkerr() { + case $1 in + 0) + ;; + *) + echo "$2 failed: dropping into /bin/sh" + /bin/sh + # RESUME + ;; + esac } -mount_md() -{ - /sbin/mdmfs -i 4096 -s $1 -M md$3 $2 +# Create a generic memory disk +# +mount_md() { + /sbin/mdmfs -i 4096 -s $1 -M md $2 +} + +# Create the memory filesystem if it has not already been created +# +create_md() { + if [ "x`eval echo \\$md_created_$1`" = "x" ]; then + if [ "x`eval echo \$md_size_$1`" = "x" ]; then + md_size=4096 + else + md_size=`eval echo \\$md_size_$1` + fi + mount_md $md_size /$1 + /bin/chmod 755 /$1 + eval md_created_$1=created + fi } # DEBUGGING @@ -81,62 +133,91 @@ bootp_ipa="" bootp_ipbca="" iflist=`ifconfig -l` for i in ${iflist} ; do - set `ifconfig ${i}` - while [ $# -ge 1 ] ; do - if [ "${bootp_ifc}" = "" -a "$1" = "inet" ]; then - bootp_ifc=${i} ; bootp_ipa=${2} ; shift - fi - if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ]; then - bootp_ipbca=$2; shift - fi - shift - done - if [ "${bootp_ifc}" != "" ]; then - break - fi + set `ifconfig ${i}` + while [ $# -ge 1 ] ; do + if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then + bootp_ifc=${i} ; bootp_ipa=${2} ; shift + fi + if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then + bootp_ipbca=$2; shift + fi + shift + done + if [ "${bootp_ifc}" != "" ] ; then + break + fi done echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}" -if [ -z "`hostname -s`" ]; then - hostname=`kenv dhcp.host-name` - hostname $hostname - echo "Hostname is $hostname" -fi - -if [ -d /conf/default/etc ]; then - mount_md 4096 /etc 0 - chkerr $? "MFS mount on /etc" - /bin/chmod 755 /etc - - /bin/cp -Rp /conf/default/etc/* /etc - chkerr $? "cp /conf/default/etc to /etc MFS" -fi - -# Allow for override files to replace files in /etc. Use /conf/*/etc to find -# the override files. First choice is default files that # always override, -# then files that from the directory that matches the client's broadcast -# address, finally followed by overrides that match the client's IP address. +# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca}, +# and /conf/${bootp_ipa}. For each subdirectory found within these +# directories: # -# This way we have some flexibility to handle clusters of machines on -# separate subnets. - -for i in ${bootp_ipbca} ${bootp_ipa} ${hostname} ; do - if [ -d /conf/${i}/etc ]; then - cp -Rp /conf/${i}/etc/* /etc +# - calculate memory filesystem sizes. If the subdirectory (prior to +# NFS remounting) contains the file 'md_size', the contents specified +# in 512 byte sectors will be used to size the memory filesystem. Otherwise +# 8192 sectors (4MB) is used. +# +# - handle NFS remounts. If the subdirectory contains the file +# diskless_remount, the contents of the file is NFS mounted over +# the directory. For example /conf/base/etc/diskless_remount +# might contain 'myserver:/etc'. NFS remounts allow you to avoid +# having to dup your system directories in /conf. Your server must +# be sure to export those filesystems -alldirs, however. +# +for i in base default ${bootp_ipbca} ${bootp_ipa} ; do + for j in /conf/$i/* ; do + # memory filesystem size specification + # + subdir=${j##*/} + if [ -d $j -a -f $j/md_size ]; then + eval md_size_$subdir=`cat $j/md_size` fi + + # NFS remount + # + if [ -d $j -a -f $j/diskless_remount ]; then + nfspt=`/bin/cat $j/diskless_remount` + mount_nfs $nfspt $j + chkerr $? "mount_nfs $nfspt $j" + fi + done done +# - Create all required MFS filesystems and populate them from +# our templates. Support both a direct template and a dir.cpio.gz +# archive. Support dir.remove files containing a list of relative +# paths to remove. # -# if the info is available via dhcp/kenv -# build the resolv.conf -# -if [ ! -e /etc/resolv.conf ]; then - echo domain `kenv dhcp.domain-name` > /etc/resolv.conf +# TODO: +# + find a way to assign a 'group' identifier to a machine +# so we can use group-specific configurations; - set `kenv dhcp.domain-name-servers` - for ns in `IFS=','; echo $*`; do - echo nameserver $ns >> /etc/resolv.conf; - done -fi +for i in base default ${bootp_ipbca} ${bootp_ipa} ; do + for j in /conf/$i/* ; do + subdir=${j##*/} + if [ -d $j ]; then + create_md $subdir + cp -Rp $j/* /$subdir + fi + done + for j in /conf/$i/*.cpio.gz ; do + subdir=${j%*.cpio.gz} + subdir=${subdir##*/} + if [ -f $j ]; then + create_md $subdir + echo "Loading /$subdir from cpio archive $j" + (cd / ; /stand/gzip -d < $j | /stand/cpio --extract -d ) + fi + done + for j in /conf/$i/*.remove ; do + subdir=${j%*.remove} + subdir=${subdir##*/} + if [ -f $j ]; then + # doubly sure it is a memory disk before rm -rf'ing + create_md $subdir + (cd /$subdir; rm -rf `/bin/cat $j`) + fi + done +done -exit 2 # Tell /etc/rc to re-source rc.conf diff --git a/etc/rc.d/rcconf b/etc/rc.d/rcconf new file mode 100644 index 000000000000..0fe75aee13b8 --- /dev/null +++ b/etc/rc.d/rcconf @@ -0,0 +1,12 @@ +#!/bin/sh +# +# $FreeBSD$ +# + +# PROVIDE: rcconf +# REQUIRE: initdiskless +# BEFORE: disks initrandom +# KEYWORD: FreeBSD + +echo "Loading configuration files." +load_rc_config 'XXX' diff --git a/etc/rc.d/resolv b/etc/rc.d/resolv index 647e3b7acb7e..e74d0bdeec68 100644 --- a/etc/rc.d/resolv +++ b/etc/rc.d/resolv @@ -28,7 +28,7 @@ # # PROVIDE: diskless -# REQUIRE: initdiskless mountcritlocal +# REQUIRE: initdiskless rcconf mountcritlocal # BEFORE: addswap random # KEYWORD: FreeBSD @@ -42,14 +42,12 @@ name="diskless2" # as close as possible between 5-current and 4-stable. # $1 = size # $2 = mount point -# $3 = md unit number (ignored in pre 5.0 systems) -# $4 = (optional) bytes-per-inode -mount_md() -{ - if [ -n "$4" ]; then - bpi="-i $4" +# $3 = (optional) bytes-per-inode +mount_md() { + if [ -n "$3" ]; then + bpi="-i $3" fi - /sbin/mdmfs $bpi -s $1 -M md$3 $2 + /sbin/mdmfs $bpi -s $1 -M md $2 } # If there is a global system configuration file, suck it in. @@ -64,16 +62,36 @@ elif [ -r /etc/rc.conf ]; then . /etc/rc.conf fi -echo "+++ mount_md of /var" -mount_md ${varsize:=32m} /var 1 +# If we do not have a writable /var, create a memory +# filesystem for /var. We don't have /usr yet so +# use mkdir instead of touch to test. We want mount +# to record its mounts so we have to make sure /var/db +# exists before doing the mount -a. +# +if (/bin/mkdir /var/.diskless 2> /dev/null); then + rmdir /var/.diskless +else + echo "+++ mount_md of /var" + mount_md ${varsize:=32m} /var +fi +if [ ! -d /var/db ]; then + mkdir /var/db +fi + +# Now we need the rest of our mounts, particularly /usr +# +mount -a # chown and chgrp are in /usr + +# Populate /var +# echo "+++ populate /var using /etc/mtree/BSD.var.dist" -/usr/sbin/mtree -deU -f /etc/mtree/BSD.var.dist -p /var +/usr/sbin/mtree -deU -f /etc/mtree/BSD.var.dist -p /var > /dev/null case ${sendmail_enable} in [Nn][Oo][Nn][Ee]) ;; *) - /usr/sbin/mtree -deU -f /etc/mtree/BSD.sendmail.dist -p / + /usr/sbin/mtree -deU -f /etc/mtree/BSD.sendmail.dist -p / > /dev/null ;; esac @@ -86,34 +104,62 @@ fi echo "+++ create lastlog" /usr/bin/touch /var/log/lastlog -mount -a # chown and chgrp are in /usr - -# Since we are starting with a very fresh /etc on an MFS: -if [ -d /conf/default/etc ]; then - newaliases -fi - +# Make sure our aliases database is uptodate, the aliases may have +# been overriden in /conf. # +/usr/bin/newaliases + # XXX make sure to create one dir for each printer as requested by lpd # - -# If /tmp is a symlink, assume it points to somewhere writable, like -# /var/tmp, otherwise, use a small memory filesystem for /tmp. +# If we do not have a writable /tmp, create a memory +# filesystem for /tmp. If /tmp is a symlink (e.g. to /var/tmp, +# then it should already be writable). # -# XXX: mtree runs too early to create any directories needed in /tmp, -# so if /var/tmp == /tmp, then you don't get a vi.recover. -# -if [ ! -L /tmp ]; then - mount_md ${tmpsize:=64m} /tmp 2 - chmod 01777 /tmp +if (/bin/mkdir /tmp/.diskless 2> /dev/null); then + rmdir /tmp/.diskless +else + if [ -h /tmp ]; then + echo "*** /tmp is a symlink to a non-writable area!" + echo "dropping into shell, ^D to continue anyway." + /bin/sh + else + mount_md ${tmpsize:=20480} /tmp + chmod 01777 /tmp + fi fi -if sysctl vfs.devfs.generation > /dev/null 2>&1; then +if sysctl vfs.devfs.generation > /dev/null 2>&1 ; then # we have DEVFS, no worries... true +elif (/bin/mkdir /dev/.diskless 2> /dev/null); then + # if /dev is writable assume it has already been populated + # via rc.diskless1 + # + rmdir /dev/.diskless else - # extract a list of device entries, then copy them to a writable fs (cd /; find -x dev | cpio -o -H newc) > /tmp/dev.tmp - mount_md 4096 /dev 3 512 + mount_md 4096 /dev 512 (cd /; cpio -i -H newc -d < /tmp/dev.tmp) + rm -f /tmp/dev.tmp fi + +# generate our hostname +# +if [ -z "`hostname -s`" ]; then + hostname=`/usr/bin/kenv dhcp.host-name` + hostname $hostname + echo "Hostname is $hostname" +fi + +# if the info is available via dhcp/kenv +# build the resolv.conf +# +if [ ! -e /etc/resolv.conf ]; then + echo domain `/usr/bin/kenv dhcp.domain-name` > /etc/resolv.conf + + set `/usr/bin/kenv dhcp.domain-name-servers` + for ns in `IFS=','; echo $*`; do + echo nameserver $ns >> /etc/resolv.conf; + done +fi + diff --git a/etc/rc.d/tmp b/etc/rc.d/tmp index 647e3b7acb7e..e74d0bdeec68 100644 --- a/etc/rc.d/tmp +++ b/etc/rc.d/tmp @@ -28,7 +28,7 @@ # # PROVIDE: diskless -# REQUIRE: initdiskless mountcritlocal +# REQUIRE: initdiskless rcconf mountcritlocal # BEFORE: addswap random # KEYWORD: FreeBSD @@ -42,14 +42,12 @@ name="diskless2" # as close as possible between 5-current and 4-stable. # $1 = size # $2 = mount point -# $3 = md unit number (ignored in pre 5.0 systems) -# $4 = (optional) bytes-per-inode -mount_md() -{ - if [ -n "$4" ]; then - bpi="-i $4" +# $3 = (optional) bytes-per-inode +mount_md() { + if [ -n "$3" ]; then + bpi="-i $3" fi - /sbin/mdmfs $bpi -s $1 -M md$3 $2 + /sbin/mdmfs $bpi -s $1 -M md $2 } # If there is a global system configuration file, suck it in. @@ -64,16 +62,36 @@ elif [ -r /etc/rc.conf ]; then . /etc/rc.conf fi -echo "+++ mount_md of /var" -mount_md ${varsize:=32m} /var 1 +# If we do not have a writable /var, create a memory +# filesystem for /var. We don't have /usr yet so +# use mkdir instead of touch to test. We want mount +# to record its mounts so we have to make sure /var/db +# exists before doing the mount -a. +# +if (/bin/mkdir /var/.diskless 2> /dev/null); then + rmdir /var/.diskless +else + echo "+++ mount_md of /var" + mount_md ${varsize:=32m} /var +fi +if [ ! -d /var/db ]; then + mkdir /var/db +fi + +# Now we need the rest of our mounts, particularly /usr +# +mount -a # chown and chgrp are in /usr + +# Populate /var +# echo "+++ populate /var using /etc/mtree/BSD.var.dist" -/usr/sbin/mtree -deU -f /etc/mtree/BSD.var.dist -p /var +/usr/sbin/mtree -deU -f /etc/mtree/BSD.var.dist -p /var > /dev/null case ${sendmail_enable} in [Nn][Oo][Nn][Ee]) ;; *) - /usr/sbin/mtree -deU -f /etc/mtree/BSD.sendmail.dist -p / + /usr/sbin/mtree -deU -f /etc/mtree/BSD.sendmail.dist -p / > /dev/null ;; esac @@ -86,34 +104,62 @@ fi echo "+++ create lastlog" /usr/bin/touch /var/log/lastlog -mount -a # chown and chgrp are in /usr - -# Since we are starting with a very fresh /etc on an MFS: -if [ -d /conf/default/etc ]; then - newaliases -fi - +# Make sure our aliases database is uptodate, the aliases may have +# been overriden in /conf. # +/usr/bin/newaliases + # XXX make sure to create one dir for each printer as requested by lpd # - -# If /tmp is a symlink, assume it points to somewhere writable, like -# /var/tmp, otherwise, use a small memory filesystem for /tmp. +# If we do not have a writable /tmp, create a memory +# filesystem for /tmp. If /tmp is a symlink (e.g. to /var/tmp, +# then it should already be writable). # -# XXX: mtree runs too early to create any directories needed in /tmp, -# so if /var/tmp == /tmp, then you don't get a vi.recover. -# -if [ ! -L /tmp ]; then - mount_md ${tmpsize:=64m} /tmp 2 - chmod 01777 /tmp +if (/bin/mkdir /tmp/.diskless 2> /dev/null); then + rmdir /tmp/.diskless +else + if [ -h /tmp ]; then + echo "*** /tmp is a symlink to a non-writable area!" + echo "dropping into shell, ^D to continue anyway." + /bin/sh + else + mount_md ${tmpsize:=20480} /tmp + chmod 01777 /tmp + fi fi -if sysctl vfs.devfs.generation > /dev/null 2>&1; then +if sysctl vfs.devfs.generation > /dev/null 2>&1 ; then # we have DEVFS, no worries... true +elif (/bin/mkdir /dev/.diskless 2> /dev/null); then + # if /dev is writable assume it has already been populated + # via rc.diskless1 + # + rmdir /dev/.diskless else - # extract a list of device entries, then copy them to a writable fs (cd /; find -x dev | cpio -o -H newc) > /tmp/dev.tmp - mount_md 4096 /dev 3 512 + mount_md 4096 /dev 512 (cd /; cpio -i -H newc -d < /tmp/dev.tmp) + rm -f /tmp/dev.tmp fi + +# generate our hostname +# +if [ -z "`hostname -s`" ]; then + hostname=`/usr/bin/kenv dhcp.host-name` + hostname $hostname + echo "Hostname is $hostname" +fi + +# if the info is available via dhcp/kenv +# build the resolv.conf +# +if [ ! -e /etc/resolv.conf ]; then + echo domain `/usr/bin/kenv dhcp.domain-name` > /etc/resolv.conf + + set `/usr/bin/kenv dhcp.domain-name-servers` + for ns in `IFS=','; echo $*`; do + echo nameserver $ns >> /etc/resolv.conf; + done +fi + diff --git a/etc/rc.d/var b/etc/rc.d/var index 647e3b7acb7e..e74d0bdeec68 100644 --- a/etc/rc.d/var +++ b/etc/rc.d/var @@ -28,7 +28,7 @@ # # PROVIDE: diskless -# REQUIRE: initdiskless mountcritlocal +# REQUIRE: initdiskless rcconf mountcritlocal # BEFORE: addswap random # KEYWORD: FreeBSD @@ -42,14 +42,12 @@ name="diskless2" # as close as possible between 5-current and 4-stable. # $1 = size # $2 = mount point -# $3 = md unit number (ignored in pre 5.0 systems) -# $4 = (optional) bytes-per-inode -mount_md() -{ - if [ -n "$4" ]; then - bpi="-i $4" +# $3 = (optional) bytes-per-inode +mount_md() { + if [ -n "$3" ]; then + bpi="-i $3" fi - /sbin/mdmfs $bpi -s $1 -M md$3 $2 + /sbin/mdmfs $bpi -s $1 -M md $2 } # If there is a global system configuration file, suck it in. @@ -64,16 +62,36 @@ elif [ -r /etc/rc.conf ]; then . /etc/rc.conf fi -echo "+++ mount_md of /var" -mount_md ${varsize:=32m} /var 1 +# If we do not have a writable /var, create a memory +# filesystem for /var. We don't have /usr yet so +# use mkdir instead of touch to test. We want mount +# to record its mounts so we have to make sure /var/db +# exists before doing the mount -a. +# +if (/bin/mkdir /var/.diskless 2> /dev/null); then + rmdir /var/.diskless +else + echo "+++ mount_md of /var" + mount_md ${varsize:=32m} /var +fi +if [ ! -d /var/db ]; then + mkdir /var/db +fi + +# Now we need the rest of our mounts, particularly /usr +# +mount -a # chown and chgrp are in /usr + +# Populate /var +# echo "+++ populate /var using /etc/mtree/BSD.var.dist" -/usr/sbin/mtree -deU -f /etc/mtree/BSD.var.dist -p /var +/usr/sbin/mtree -deU -f /etc/mtree/BSD.var.dist -p /var > /dev/null case ${sendmail_enable} in [Nn][Oo][Nn][Ee]) ;; *) - /usr/sbin/mtree -deU -f /etc/mtree/BSD.sendmail.dist -p / + /usr/sbin/mtree -deU -f /etc/mtree/BSD.sendmail.dist -p / > /dev/null ;; esac @@ -86,34 +104,62 @@ fi echo "+++ create lastlog" /usr/bin/touch /var/log/lastlog -mount -a # chown and chgrp are in /usr - -# Since we are starting with a very fresh /etc on an MFS: -if [ -d /conf/default/etc ]; then - newaliases -fi - +# Make sure our aliases database is uptodate, the aliases may have +# been overriden in /conf. # +/usr/bin/newaliases + # XXX make sure to create one dir for each printer as requested by lpd # - -# If /tmp is a symlink, assume it points to somewhere writable, like -# /var/tmp, otherwise, use a small memory filesystem for /tmp. +# If we do not have a writable /tmp, create a memory +# filesystem for /tmp. If /tmp is a symlink (e.g. to /var/tmp, +# then it should already be writable). # -# XXX: mtree runs too early to create any directories needed in /tmp, -# so if /var/tmp == /tmp, then you don't get a vi.recover. -# -if [ ! -L /tmp ]; then - mount_md ${tmpsize:=64m} /tmp 2 - chmod 01777 /tmp +if (/bin/mkdir /tmp/.diskless 2> /dev/null); then + rmdir /tmp/.diskless +else + if [ -h /tmp ]; then + echo "*** /tmp is a symlink to a non-writable area!" + echo "dropping into shell, ^D to continue anyway." + /bin/sh + else + mount_md ${tmpsize:=20480} /tmp + chmod 01777 /tmp + fi fi -if sysctl vfs.devfs.generation > /dev/null 2>&1; then +if sysctl vfs.devfs.generation > /dev/null 2>&1 ; then # we have DEVFS, no worries... true +elif (/bin/mkdir /dev/.diskless 2> /dev/null); then + # if /dev is writable assume it has already been populated + # via rc.diskless1 + # + rmdir /dev/.diskless else - # extract a list of device entries, then copy them to a writable fs (cd /; find -x dev | cpio -o -H newc) > /tmp/dev.tmp - mount_md 4096 /dev 3 512 + mount_md 4096 /dev 512 (cd /; cpio -i -H newc -d < /tmp/dev.tmp) + rm -f /tmp/dev.tmp fi + +# generate our hostname +# +if [ -z "`hostname -s`" ]; then + hostname=`/usr/bin/kenv dhcp.host-name` + hostname $hostname + echo "Hostname is $hostname" +fi + +# if the info is available via dhcp/kenv +# build the resolv.conf +# +if [ ! -e /etc/resolv.conf ]; then + echo domain `/usr/bin/kenv dhcp.domain-name` > /etc/resolv.conf + + set `/usr/bin/kenv dhcp.domain-name-servers` + for ns in `IFS=','; echo $*`; do + echo nameserver $ns >> /etc/resolv.conf; + done +fi + diff --git a/etc/rc.initdiskless b/etc/rc.initdiskless index faa2acb6e297..5ddc768a43c8 100644 --- a/etc/rc.initdiskless +++ b/etc/rc.initdiskless @@ -26,25 +26,62 @@ # # $FreeBSD$ # - # PROVIDE: initdiskless # KEYWORD: FreeBSD + +# On entry to this script the entire system consists of a read-only root +# mounted via NFS. We use the contents of /conf to create and populate +# memory filesystems. The kernel has run BOOTP and configured an interface +# (otherwise it would not have been able to mount the NFS root!) +# +# The following directories are scanned. Each sucessive directory overrides +# (is merged into) the previous one. +# +# /conf/base universal base +# /conf/default modified by a secondary universal base +# /conf/${ipba} modified based on the assigned broadcast IP +# /conf/${ip} modified based on the machine's assigned IP +# +# Each of these directories may contain any number of subdirectories which +# represent directories in / on the diskless machine. The existance of +# these subdirectories causes this script to create a MEMORY FILESYSTEM for +# /. For example, if /conf/base/etc exists then a +# memory filesystem will be created for /etc. +# +# If a subdirectory contains the file 'diskless_remount' the contents of +# the file is used to remount the subdirectory prior to it being copied to +# the memory filesystem. For example, if /conf/base/etc/diskless_remount +# contains the string 'my.server.com:/etc' then my.server.com:/etc will be +# mounted in place of the subdirectory. This allows you to avoid making +# duplicates of system directories in /conf. +# +# If a subdirectory contains the file 'md_size', the contents of the +# file is used to determine the size of the memory filesystem, in 512 +# byte sectors. The default is 8192 (4MB). You only have to specify an +# md_size if the default doesn't work for you (i.e. if it is too big or +# too small). Note that in -current the default is 4096 (2MB). For +# example, /conf/base/etc/md_size might contain '16384'. +# +# If /conf//SUBDIR.cpio.gz exists, the file is cpio'd into +# the specified /SUBDIR (and a memory filesystem is created for /SUBDIR +# if necessary). +# +# If /conf//SUBDIR.remove exists, the file contains a list +# of paths which are rm -rf'd relative to /SUBDIR. +# +# You will almost universally want to create a /conf/base/etc containing +# a diskless_remount and possibly an md_size file. You will then almost +# universally want to override rc.conf, rc.local, and fstab by creating +# /conf/default/etc/{rc.conf,rc.local,fstab}. Your fstab should be sure +# to mount a /usr... typically an NFS readonly /usr. +# +# NOTE! rc.diskless2 will create /var, /tmp, and /dev. Those filesystems +# should not be specified in /conf. At least not yet. + dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` [ ${dlv:=0} -eq 0 ] && exit 0 -# -# BOOTP has mounted / for us. Assume a read-only mount. We must then -# - figure out our IP by querying the interface -# - mount /etc as an MFS -# - populate /etc from /conf/default version -# - override files in /etc with files from /conf/*/etc where -# '*' is default, netmask of client, ip-address of client -# -# The operator is in charge of setting /conf/*/etc/* things as appropriate. -# Typically rc.conf and fstab need to be changed, but possibly also other -# files such as inetd.conf etc. - # chkerr: # # Routine to check for error @@ -52,22 +89,37 @@ dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` # checks error code and drops into shell on failure. # if shell exits, terminates script as well as /etc/rc. # -chkerr() -{ - case $1 in - 0) - ;; - *) - echo "$2 failed: dropping into /bin/sh" - /bin/sh - # RESUME - ;; - esac +chkerr() { + case $1 in + 0) + ;; + *) + echo "$2 failed: dropping into /bin/sh" + /bin/sh + # RESUME + ;; + esac } -mount_md() -{ - /sbin/mdmfs -i 4096 -s $1 -M md$3 $2 +# Create a generic memory disk +# +mount_md() { + /sbin/mdmfs -i 4096 -s $1 -M md $2 +} + +# Create the memory filesystem if it has not already been created +# +create_md() { + if [ "x`eval echo \\$md_created_$1`" = "x" ]; then + if [ "x`eval echo \$md_size_$1`" = "x" ]; then + md_size=4096 + else + md_size=`eval echo \\$md_size_$1` + fi + mount_md $md_size /$1 + /bin/chmod 755 /$1 + eval md_created_$1=created + fi } # DEBUGGING @@ -81,62 +133,91 @@ bootp_ipa="" bootp_ipbca="" iflist=`ifconfig -l` for i in ${iflist} ; do - set `ifconfig ${i}` - while [ $# -ge 1 ] ; do - if [ "${bootp_ifc}" = "" -a "$1" = "inet" ]; then - bootp_ifc=${i} ; bootp_ipa=${2} ; shift - fi - if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ]; then - bootp_ipbca=$2; shift - fi - shift - done - if [ "${bootp_ifc}" != "" ]; then - break - fi + set `ifconfig ${i}` + while [ $# -ge 1 ] ; do + if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then + bootp_ifc=${i} ; bootp_ipa=${2} ; shift + fi + if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then + bootp_ipbca=$2; shift + fi + shift + done + if [ "${bootp_ifc}" != "" ] ; then + break + fi done echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}" -if [ -z "`hostname -s`" ]; then - hostname=`kenv dhcp.host-name` - hostname $hostname - echo "Hostname is $hostname" -fi - -if [ -d /conf/default/etc ]; then - mount_md 4096 /etc 0 - chkerr $? "MFS mount on /etc" - /bin/chmod 755 /etc - - /bin/cp -Rp /conf/default/etc/* /etc - chkerr $? "cp /conf/default/etc to /etc MFS" -fi - -# Allow for override files to replace files in /etc. Use /conf/*/etc to find -# the override files. First choice is default files that # always override, -# then files that from the directory that matches the client's broadcast -# address, finally followed by overrides that match the client's IP address. +# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca}, +# and /conf/${bootp_ipa}. For each subdirectory found within these +# directories: # -# This way we have some flexibility to handle clusters of machines on -# separate subnets. - -for i in ${bootp_ipbca} ${bootp_ipa} ${hostname} ; do - if [ -d /conf/${i}/etc ]; then - cp -Rp /conf/${i}/etc/* /etc +# - calculate memory filesystem sizes. If the subdirectory (prior to +# NFS remounting) contains the file 'md_size', the contents specified +# in 512 byte sectors will be used to size the memory filesystem. Otherwise +# 8192 sectors (4MB) is used. +# +# - handle NFS remounts. If the subdirectory contains the file +# diskless_remount, the contents of the file is NFS mounted over +# the directory. For example /conf/base/etc/diskless_remount +# might contain 'myserver:/etc'. NFS remounts allow you to avoid +# having to dup your system directories in /conf. Your server must +# be sure to export those filesystems -alldirs, however. +# +for i in base default ${bootp_ipbca} ${bootp_ipa} ; do + for j in /conf/$i/* ; do + # memory filesystem size specification + # + subdir=${j##*/} + if [ -d $j -a -f $j/md_size ]; then + eval md_size_$subdir=`cat $j/md_size` fi + + # NFS remount + # + if [ -d $j -a -f $j/diskless_remount ]; then + nfspt=`/bin/cat $j/diskless_remount` + mount_nfs $nfspt $j + chkerr $? "mount_nfs $nfspt $j" + fi + done done +# - Create all required MFS filesystems and populate them from +# our templates. Support both a direct template and a dir.cpio.gz +# archive. Support dir.remove files containing a list of relative +# paths to remove. # -# if the info is available via dhcp/kenv -# build the resolv.conf -# -if [ ! -e /etc/resolv.conf ]; then - echo domain `kenv dhcp.domain-name` > /etc/resolv.conf +# TODO: +# + find a way to assign a 'group' identifier to a machine +# so we can use group-specific configurations; - set `kenv dhcp.domain-name-servers` - for ns in `IFS=','; echo $*`; do - echo nameserver $ns >> /etc/resolv.conf; - done -fi +for i in base default ${bootp_ipbca} ${bootp_ipa} ; do + for j in /conf/$i/* ; do + subdir=${j##*/} + if [ -d $j ]; then + create_md $subdir + cp -Rp $j/* /$subdir + fi + done + for j in /conf/$i/*.cpio.gz ; do + subdir=${j%*.cpio.gz} + subdir=${subdir##*/} + if [ -f $j ]; then + create_md $subdir + echo "Loading /$subdir from cpio archive $j" + (cd / ; /stand/gzip -d < $j | /stand/cpio --extract -d ) + fi + done + for j in /conf/$i/*.remove ; do + subdir=${j%*.remove} + subdir=${subdir##*/} + if [ -f $j ]; then + # doubly sure it is a memory disk before rm -rf'ing + create_md $subdir + (cd /$subdir; rm -rf `/bin/cat $j`) + fi + done +done -exit 2 # Tell /etc/rc to re-source rc.conf