rc.initdiskless revision 340612
1281SN/A#!/bin/sh 2798Sjoehw# 3281SN/A# Copyright (c) 1999 Matt Dillon 4281SN/A# All rights reserved. 5281SN/A# 6281SN/A# Redistribution and use in source and binary forms, with or without 7281SN/A# modification, are permitted provided that the following conditions 8281SN/A# are met: 9281SN/A# 1. Redistributions of source code must retain the above copyright 10281SN/A# notice, this list of conditions and the following disclaimer. 11281SN/A# 2. Redistributions in binary form must reproduce the above copyright 12281SN/A# notice, this list of conditions and the following disclaimer in the 13281SN/A# documentation and/or other materials provided with the distribution. 14281SN/A# 15281SN/A# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16281SN/A# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17281SN/A# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18281SN/A# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19281SN/A# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20281SN/A# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21281SN/A# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22281SN/A# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23281SN/A# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24281SN/A# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25281SN/A# SUCH DAMAGE. 26281SN/A# 27281SN/A# $FreeBSD: stable/10/etc/rc.initdiskless 340612 2018-11-19 06:39:00Z eugen $ 28281SN/A 29281SN/A# On entry to this script the entire system consists of a read-only root 30281SN/A# mounted via NFS. The kernel has run BOOTP and configured an interface 31281SN/A# (otherwise it would not have been able to mount the NFS root!) 32281SN/A# 33281SN/A# We use the contents of /conf to create and populate memory filesystems 34281SN/A# that are mounted on top of this root to implement the writable 35281SN/A# (and host-specific) parts of the root filesystem, and other volatile 36281SN/A# filesystems. 37281SN/A# 38281SN/A# The hierarchy in /conf has the form /conf/T/M/ where M are directories 39281SN/A# for which memory filesystems will be created and filled, 40281SN/A# and T is one of the "template" directories below: 41281SN/A# 42281SN/A# base universal base, typically a replica of the original root; 43281SN/A# default secondary universal base, typically overriding some 44281SN/A# of the files in the original root; 45281SN/A# ${ipba} where ${ipba} is the assigned broadcast IP address 46281SN/A# bcast/${ipba} same as above 47281SN/A# ${class} where ${class} is a list of directories supplied by 48281SN/A# bootp/dhcp through the T134 option. 49281SN/A# ${ipba} and ${class} are typically used to configure features 50281SN/A# for group of diskless clients, or even individual features; 51281SN/A# ${ip} where ${ip} is the machine's assigned IP address, typically 52281SN/A# used to set host-specific features; 53281SN/A# ip/${ip} same as above 54281SN/A# 55281SN/A# Template directories are scanned in the order they are listed above, 56281SN/A# with each successive directory overriding (merged into) the previous one; 57281SN/A# non-existing directories are ignored. The subdirectory forms exist to 58281SN/A# help keep the top level /conf manageable in large installations. 59281SN/A# 60281SN/A# The existence of a directory /conf/T/M causes this script to create a 61281SN/A# memory filesystem mounted as /M on the client. 62281SN/A# 63281SN/A# Some files in /conf have special meaning, namely: 64281SN/A# 65281SN/A# Filename Action 66281SN/A# ---------------------------------------------------------------- 67281SN/A# /conf/T/M/remount 68281SN/A# The contents of the file is a mount command. E.g. if 69281SN/A# /conf/1.2.3.4/foo/remount contains "mount -o ro /dev/ad0s3", 70281SN/A# then /dev/ad0s3 will be be mounted on /conf/1.2.3.4/foo/ 71281SN/A# 72281SN/A# /conf/T/M/remount_optional 73281SN/A# If this file exists, then failure to execute the mount 74281SN/A# command contained in /conf/T/M/remount is non-fatal. 75281SN/A# 76281SN/A# /conf/T/M/remount_subdir 77281SN/A# If this file exists, then the behaviour of /conf/T/M/remount 78281SN/A# changes as follows: 79281SN/A# 1. /conf/T/M/remount is invoked to mount the root of the 80281SN/A# filesystem where the configuration data exists on a 81281SN/A# temporary mountpoint. 82281SN/A# 2. /conf/T/M/remount_subdir is then invoked to mount a 83281SN/A# *subdirectory* of the filesystem mounted by 84281SN/A# /conf/T/M/remount on /conf/T/M/. 85281SN/A# 86281SN/A# /conf/T/M/diskless_remount 87281SN/A# The contents of the file points to an NFS filesystem, 88281SN/A# possibly followed by mount_nfs options. If the server name 89281SN/A# is omitted, the script will prepend the root path used when 90281SN/A# booting. E.g. if you booted from foo.com:/path/to/root, 91281SN/A# an entry for /conf/base/etc/diskless_remount could be any of 92281SN/A# foo.com:/path/to/root/etc 93281SN/A# /etc -o ro 94281SN/A# Because mount_nfs understands ".." in paths, it is 95281SN/A# possible to mount from locations above the NFS root with 96281SN/A# paths such as "/../../etc". 97281SN/A# 98281SN/A# /conf/T/M/md_size 99281SN/A# The contents of the file specifies the size of the memory 100281SN/A# filesystem to be created, in 512 byte blocks. 101281SN/A# The default size is 10240 blocks (5MB). E.g. if 102281SN/A# /conf/base/etc/md_size contains "30000" then a 15MB MFS 103281SN/A# will be created. In case of multiple entries for the same 104281SN/A# directory M, the last one in the scanning order is used. 105281SN/A# NOTE: If you only need to create a memory filesystem but not 106281SN/A# initialize it from a template, it is preferable to specify 107281SN/A# it in fstab e.g. as "md /tmp mfs -s=30m,rw 0 0" 108281SN/A# 109281SN/A# /conf/T/SUBDIR.cpio.gz 110281SN/A# The file is cpio'd into /SUBDIR (and a memory filesystem is 111281SN/A# created for /SUBDIR if necessary). The presence of this file 112281SN/A# prevents the copy from /conf/T/SUBDIR/ 113281SN/A# 114281SN/A# /conf/T/M/extract 115281SN/A# This is alternative to SUBDIR.cpio.gz and remount. 116281SN/A# Similar to remount case, a memory filesystem is created 117281SN/A# for /M and initialized from a template but no mounting 118281SN/A# performed. Instead, this file is run passing /M as singe 119281SN/A# argument. It is expected to extract template override to /M 120281SN/A# using auxiliary storage found in some embedded systems 121281SN/A# having NVRAM too small to hold mountable file system. 122281SN/A# 123281SN/A# /conf/T/SUBDIR.remove 124281SN/A# The list of paths contained in the file are rm -rf'd 125281SN/A# relative to /SUBDIR. 126281SN/A# 127281SN/A# /conf/diskless_remount 128281SN/A# Similar to /conf/T/M/diskless_remount above, but allows 129281SN/A# all of /conf to be remounted. This can be used to allow 130281SN/A# multiple roots to share the same /conf. 131281SN/A# 132281SN/A# 133281SN/A# You will almost universally want to create the following files under /conf 134281SN/A# 135281SN/A# File Content 136281SN/A# ---------------------------- ---------------------------------- 137281SN/A# /conf/base/etc/md_size size of /etc filesystem 138281SN/A# /conf/base/etc/diskless_remount "/etc" 139281SN/A# /conf/default/etc/rc.conf generic diskless config parameters 140281SN/A# /conf/default/etc/fstab generic diskless fstab e.g. like this 141281SN/A# 142281SN/A# foo:/root_part / nfs ro 0 0 143281SN/A# foo:/usr_part /usr nfs ro 0 0 144281SN/A# foo:/home_part /home nfs rw 0 0 145281SN/A# md /tmp mfs -s=30m,rw 0 0 146281SN/A# md /var mfs -s=30m,rw 0 0 147281SN/A# proc /proc procfs rw 0 0 148281SN/A# 149281SN/A# plus, possibly, overrides for password files etc. 150281SN/A# 151281SN/A# NOTE! /var, /tmp, and /dev will be typically created elsewhere, e.g. 152281SN/A# as entries in the fstab as above. 153281SN/A# Those filesystems should not be specified in /conf. 154281SN/A# 155281SN/A# (end of documentation, now get to the real code) 156281SN/A 157281SN/Adlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` 158281SN/A 159281SN/A# DEBUGGING 160281SN/A# log something on stdout if verbose. 161281SN/Ao_verbose=0 # set to 1 or 2 if you want more debugging 162281SN/Alog() { 163281SN/A [ ${o_verbose} -gt 0 ] && echo "*** $* ***" 164281SN/A [ ${o_verbose} -gt 1 ] && read -p "=== Press enter to continue" foo 165281SN/A} 166281SN/A 167281SN/A# chkerr: 168281SN/A# 169281SN/A# Routine to check for error 170281SN/A# 171281SN/A# checks error code and drops into shell on failure. 172281SN/A# if shell exits, terminates script as well as /etc/rc. 173281SN/A# if remount_optional exists under the mountpoint, skip this check. 174281SN/A# 175281SN/Achkerr() { 176281SN/A lastitem () ( n=$(($# - 1)) ; shift $n ; echo $1 ) 177281SN/A mountpoint="$(lastitem $2)" 178281SN/A [ -r $mountpoint/remount_optional ] && ( echo "$2 failed: ignoring due to remount_optional" ; return ) 179281SN/A case $1 in 180281SN/A 0) 181281SN/A ;; 182281SN/A *) 183281SN/A echo "$2 failed: dropping into /bin/sh" 184281SN/A /bin/sh 185281SN/A # RESUME 186281SN/A ;; 187281SN/A esac 188281SN/A} 189281SN/A 190281SN/A# The list of filesystems to umount after the copy 191281SN/Ato_umount="" 192281SN/A 193281SN/Ahandle_remount() { # $1 = mount point 194281SN/A local nfspt mountopts b 195281SN/A b=$1 196281SN/A log handle_remount $1 197281SN/A [ -d $b -a -f $b/diskless_remount ] || return 198281SN/A read nfspt mountopts < $b/diskless_remount 199281SN/A log "nfspt ${nfspt} mountopts ${mountopts}" 200281SN/A # prepend the nfs root if not present 201281SN/A [ `expr "$nfspt" : '\(.\)'` = "/" ] && nfspt="${nfsroot}${nfspt}" 202281SN/A mount_nfs $mountopts $nfspt $b 203281SN/A chkerr $? "mount_nfs $nfspt $b" 204281SN/A to_umount="$b ${to_umount}" 205281SN/A} 206281SN/A 207281SN/A# Create a generic memory disk 208281SN/A# 209281SN/Amount_md() { 210281SN/A /sbin/mdmfs -S -i 4096 -s $1 -M md $2 211281SN/A} 212281SN/A 213281SN/A# Create the memory filesystem if it has not already been created 214281SN/A# 215281SN/Acreate_md() { 216281SN/A [ "x`eval echo \\$md_created_$1`" = "x" ] || return # only once 217281SN/A if [ "x`eval echo \\$md_size_$1`" = "x" ]; then 218281SN/A md_size=10240 219281SN/A else 220281SN/A md_size=`eval echo \\$md_size_$1` 221281SN/A fi 222281SN/A log create_md $1 with size $md_size 223281SN/A mount_md $md_size /$1 224281SN/A /bin/chmod 755 /$1 225281SN/A eval md_created_$1=created 226281SN/A} 227281SN/A 228281SN/A# DEBUGGING 229281SN/A# 230281SN/A# set -v 231281SN/A 232281SN/A# Figure out our interface and IP. 233281SN/A# 234281SN/Abootp_ifc="" 235281SN/Abootp_ipa="" 236281SN/Abootp_ipbca="" 237281SN/Aclass="" 238281SN/Aif [ ${dlv:=0} -ne 0 ] ; then 239281SN/A iflist=`ifconfig -l` 240281SN/A for i in ${iflist} ; do 241281SN/A set -- `ifconfig ${i}` 242281SN/A while [ $# -ge 1 ] ; do 243281SN/A if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then 244281SN/A bootp_ifc=${i} ; bootp_ipa=${2} ; shift 245281SN/A fi 246281SN/A if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then 247281SN/A bootp_ipbca=$2; shift 248281SN/A fi 249281SN/A shift 250281SN/A done 251281SN/A if [ "${bootp_ifc}" != "" ] ; then 252281SN/A break 253281SN/A fi 254281SN/A done 255281SN/A # Get the values passed with the T134 bootp cookie. 256281SN/A class="`/sbin/sysctl -qn kern.bootp_cookie`" 257281SN/A 258281SN/A echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca} ${class}" 259281SN/Afi 260281SN/A 261281SN/Alog Figure out our NFS root path 262281SN/A# 263281SN/Aset -- `mount -t nfs` 264281SN/Awhile [ $# -ge 1 ] ; do 265281SN/A if [ "$2" = "on" -a "$3" = "/" ]; then 266281SN/A nfsroot="$1" 267281SN/A break 268281SN/A fi 269281SN/A shift 270281SN/Adone 271281SN/A 272281SN/A# The list of directories with template files 273281SN/Atemplates="base default" 274281SN/Aif [ -n "${bootp_ipbca}" ]; then 275281SN/A templates="${templates} ${bootp_ipbca} bcast/${bootp_ipbca}" 276281SN/Afi 277281SN/Aif [ -n "${class}" ]; then 278281SN/A templates="${templates} ${class}" 279281SN/Afi 280281SN/Aif [ -n "${bootp_ipa}" ]; then 281281SN/A templates="${templates} ${bootp_ipa} ip/${bootp_ipa}" 282281SN/Afi 283281SN/A 284281SN/A# If /conf/diskless_remount exists, remount all of /conf. 285281SN/Ahandle_remount /conf 286281SN/A 287281SN/A# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca}, 288281SN/A# and /conf/${bootp_ipa}. For each subdirectory found within these 289281SN/A# directories: 290281SN/A# 291281SN/A# - calculate memory filesystem sizes. If the subdirectory (prior to 292281SN/A# NFS remounting) contains the file 'md_size', the contents specified 293281SN/A# in 512 byte sectors will be used to size the memory filesystem. Otherwise 294281SN/A# 8192 sectors (4MB) is used. 295281SN/A# 296281SN/A# - handle NFS remounts. If the subdirectory contains the file 297281SN/A# diskless_remount, the contents of the file is NFS mounted over 298281SN/A# the directory. For example /conf/base/etc/diskless_remount 299281SN/A# might contain 'myserver:/etc'. NFS remounts allow you to avoid 300281SN/A# having to dup your system directories in /conf. Your server must 301281SN/A# be sure to export those filesystems -alldirs, however. 302281SN/A# If the diskless_remount file contains a string beginning with a 303281SN/A# '/' it is assumed that the local nfsroot should be prepended to 304281SN/A# it before attemping to the remount. This allows the root to be 305281SN/A# relocated without needing to change the remount files. 306281SN/A# 307281SN/Alog "templates are ${templates}" 308281SN/Afor i in ${templates} ; do 309281SN/A for j in /conf/$i/* ; do 310281SN/A [ -d $j ] || continue 311281SN/A 312281SN/A # memory filesystem size specification 313281SN/A subdir=${j##*/} 314281SN/A [ -f $j/md_size ] && eval md_size_$subdir=`cat $j/md_size` 315281SN/A 316281SN/A # remount. Beware, the command is in the file itself! 317281SN/A if [ -f $j/remount ]; then 318281SN/A if [ -f $j/remount_subdir ]; then 319281SN/A k="/conf.tmp/$i/$subdir" 320281SN/A [ -d $k ] || continue 321281SN/A 322281SN/A # Mount the filesystem root where the config data is 323281SN/A # on the temporary mount point. 324281SN/A nfspt=`/bin/cat $j/remount` 325281SN/A $nfspt $k 326281SN/A chkerr $? "$nfspt $k" 327281SN/A 328281SN/A # Now use a nullfs mount to get the data where we 329281SN/A # really want to see it. 330281SN/A remount_subdir=`/bin/cat $j/remount_subdir` 331281SN/A remount_subdir_cmd="mount -t nullfs $k/$remount_subdir" 332281SN/A 333281SN/A $remount_subdir_cmd $j 334281SN/A chkerr $? "$remount_subdir_cmd $j" 335281SN/A 336281SN/A # XXX check order -- we must force $k to be unmounted 337281SN/A # after j, as j depends on k. 338281SN/A to_umount="$j $k ${to_umount}" 339281SN/A else 340281SN/A nfspt=`/bin/cat $j/remount` 341281SN/A $nfspt $j 342281SN/A chkerr $? "$nfspt $j" 343281SN/A to_umount="$j ${to_umount}" # XXX hope it is really a mount! 344281SN/A fi 345281SN/A fi 346281SN/A 347281SN/A # NFS remount 348281SN/A handle_remount $j 349281SN/A done 350281SN/Adone 351281SN/A 352281SN/A# - Create all required MFS filesystems and populate them from 353281SN/A# our templates. Support both a direct template and a dir.cpio.gz 354281SN/A# archive. Support for auxiliary NVRAM. Support dir.remove files containing 355281SN/A# a list of relative paths to remove. 356281SN/A# 357281SN/A# The dir.cpio.gz form is there to make the copy process more efficient, 358281SN/A# so if the cpio archive is present, it prevents the files from dir/ 359281SN/A# from being copied. 360281SN/A 361281SN/Afor i in ${templates} ; do 362281SN/A for j in /conf/$i/* ; do 363281SN/A subdir=${j##*/} 364281SN/A if [ -d $j -a ! -f $j.cpio.gz ]; then 365281SN/A create_md $subdir 366281SN/A cp -Rp $j/ /$subdir 367281SN/A fi 368281SN/A done 369281SN/A for j in /conf/$i/*.cpio.gz ; do 370281SN/A subdir=${j%*.cpio.gz} 371281SN/A subdir=${subdir##*/} 372281SN/A if [ -f $j ]; then 373281SN/A create_md $subdir 374281SN/A echo "Loading /$subdir from cpio archive $j" 375281SN/A (cd / ; /rescue/tar -xpf $j) 376281SN/A fi 377281SN/A done 378281SN/A for j in /conf/$i/*/extract ; do 379281SN/A if [ -x $j ]; then 380281SN/A subdir=${j%*/extract} 381281SN/A subdir=${subdir##*/} 382281SN/A create_md $subdir 383281SN/A echo "Loading /$subdir using auxiliary command $j" 384281SN/A $j /$subdir 385281SN/A fi 386281SN/A done 387281SN/A for j in /conf/$i/*.remove ; do 388281SN/A subdir=${j%*.remove} 389281SN/A subdir=${subdir##*/} 390281SN/A if [ -f $j ]; then 391281SN/A # doubly sure it is a memory disk before rm -rf'ing 392281SN/A create_md $subdir 393281SN/A (cd /$subdir; rm -rf `/bin/cat $j`) 394281SN/A fi 395281SN/A done 396281SN/Adone 397281SN/A 398281SN/A# umount partitions used to fill the memory filesystems 399281SN/A[ -n "${to_umount}" ] && umount $to_umount 400281SN/A