rc.initdiskless revision 161533
1100280Sgordon#!/bin/sh 2100280Sgordon# 366830Sobrien# Copyright (c) 1999 Matt Dillon 466830Sobrien# All rights reserved. 566830Sobrien# 666830Sobrien# Redistribution and use in source and binary forms, with or without 766830Sobrien# modification, are permitted provided that the following conditions 866830Sobrien# are met: 966830Sobrien# 1. Redistributions of source code must retain the above copyright 1066830Sobrien# notice, this list of conditions and the following disclaimer. 1166830Sobrien# 2. Redistributions in binary form must reproduce the above copyright 1266830Sobrien# notice, this list of conditions and the following disclaimer in the 1366830Sobrien# documentation and/or other materials provided with the distribution. 1466830Sobrien# 1566830Sobrien# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1666830Sobrien# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1766830Sobrien# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1866830Sobrien# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1966830Sobrien# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2066830Sobrien# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2166830Sobrien# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2266830Sobrien# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2366830Sobrien# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2466830Sobrien# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2566830Sobrien# SUCH DAMAGE. 2666830Sobrien# 2751231Ssheldonh# $FreeBSD: head/etc/rc.initdiskless 161533 2006-08-22 16:21:16Z ru $ 2851231Ssheldonh 2966830Sobrien# On entry to this script the entire system consists of a read-only root 30100280Sgordon# mounted via NFS. The kernel has run BOOTP and configured an interface 31100280Sgordon# (otherwise it would not have been able to mount the NFS root!) 32100280Sgordon# 33100280Sgordon# We use the contents of /conf to create and populate memory filesystems 34100280Sgordon# that are mounted on top of this root to implement the writable 35100280Sgordon# (and host-specific) parts of the root filesystem, and other volatile 3666830Sobrien# filesystems. 3755520Sluigi# 3855520Sluigi# The hierarchy in /conf has the form /conf/T/M/ where M are directories 3975746Sbsd# for which memory filesystems will be created and filled, 4088496Srwatson# and T is one of the "template" directories below: 4175746Sbsd# 4275746Sbsd# base universal base, typically a replica of the original root; 4343803Sdillon# default secondary universal base, typically overriding some 4455520Sluigi# of the files in the original root; 4589437Srwatson# ${ipba} where ${ipba} is the assigned broadcast IP address 4689437Srwatson# bcast/${ipba} same as above 4743803Sdillon# ${class} where ${class} is a list of directories supplied by 4843803Sdillon# bootp/dhcp through the T134 option. 4943803Sdillon# ${ipba} and ${class} are typicall used to configure features 5043803Sdillon# for group of diskless clients, or even individual features; 5143803Sdillon# ${ip} where ${ip} is the machine's assigned IP address, typically 5243803Sdillon# used to set host-specific features; 5343803Sdillon# ip/${ip} same as above 5451231Ssheldonh# 55100280Sgordon# Template directories are scanned in the order they are listed above, 56100280Sgordon# with each sucessive directory overriding (merged into) the previous one; 5751231Ssheldonh# non-existing directories are ignored. The subdirectory forms exist to 5851231Ssheldonh# help keep the top level /conf managable in large installations. 5951231Ssheldonh# 6051231Ssheldonh# The existence of a directory /conf/T/M causes this script to create a 6143803Sdillon# memory filesystem mounted as /M on the client. 6243803Sdillon# 6343803Sdillon# Some files in /conf have special meaning, namely: 6451231Ssheldonh# 6551231Ssheldonh# Filename Action 6643803Sdillon# ---------------------------------------------------------------- 6743803Sdillon# /conf/T/M/remount 68100280Sgordon# The contents of the file is a mount command. E.g. if 69100280Sgordon# /conf/1.2.3.4/foo/remount contains "mount -o ro /dev/ad0s3", 70103799Sphk# then /dev/ad0s3 will be be mounted on /conf/1.2.3.4/foo/ 7175931Simp# 7275931Simp# /conf/T/M/diskless_remount 7343803Sdillon# The contents of the file points to an NFS filesystem. E.g. if 7443803Sdillon# /conf/base/etc/diskless_remount contains "foo.com:/etc", 7555520Sluigi# then foo.com:/etc will be be mounted on /conf/base/etc/ 7643803Sdillon# If the file contains a pathname starting with "/", then 7751231Ssheldonh# the root path is prepended to it; this allows relocation of 7843803Sdillon# the root filesystem without changing configuration files. 7955520Sluigi# 8055520Sluigi# /conf/T/M/md_size 8155520Sluigi# The contents of the file specifies the size of the memory 8255520Sluigi# filesystem to be created, in 512 byte blocks. 8355520Sluigi# The default size is 10240 blocks (5MB). E.g. if 8455520Sluigi# /conf/base/etc/md_size contains "30000" then a 15MB MFS 8555520Sluigi# will be created. In case of multiple entries for the same 8655520Sluigi# directory M, the last one in the scanning order is used. 8755520Sluigi# NOTE: If you only need to create a memory filesystem but not 8855520Sluigi# initialize it from a template, it is preferrable to specify 8955520Sluigi# it in fstab e.g. as "md /tmp mfs -s=30m,rw 0 0" 9055520Sluigi# 9155520Sluigi# /conf/T/SUBDIR.cpio.gz 9255520Sluigi# The file is cpio'd into /SUBDIR (and a memory filesystem is 9355520Sluigi# created for /SUBDIR if necessary). The presence of this file 9455520Sluigi# prevents the copy from /conf/T/SUBDIR/ 9555520Sluigi# 9655520Sluigi# /conf/T/SUBDIR.remove 9755520Sluigi# The list of paths contained in the file are rm -rf'd 9855520Sluigi# relative to /SUBDIR. 9943803Sdillon# 10095280Sobrien# You will almost universally want to create the following files under /conf 101100280Sgordon# 102100280Sgordon# File Content 10395280Sobrien# ---------------------------- ---------------------------------- 10495280Sobrien# /conf/base/etc/md_size size of /etc filesystem 10595280Sobrien# /conf/base/etc/diskless_remount "/etc" 10691074Sobrien# /conf/default/etc/rc.conf generic diskless config parameters 10791074Sobrien# /conf/default/etc/fstab generic diskless fstab e.g. like this 10891074Sobrien# 10991074Sobrien# foo:/root_part / nfs ro 0 0 11075101Sbsd# foo:/usr_part /usr nfs ro 0 0 11191074Sobrien# foo:/home_part /home nfs rw 0 0 11291074Sobrien# md /tmp mfs -s=30m,rw 0 0 11391074Sobrien# md /var mfs -s=30m,rw 0 0 11443803Sdillon# proc /proc procfs rw 0 0 11589438Srwatson# 11689438Srwatson# plus, possibly, overrides for password files etc. 11789438Srwatson# 11889438Srwatson# NOTE! /var, /tmp, and /dev will be typically created elsewhere, e.g. 11943803Sdillon# as entries in the fstab as above. 12089438Srwatson# Those filesystems should not be specified in /conf. 12189438Srwatson# 12243803Sdillon# (end of documentation, now get to the real code) 12395280Sobrien 12475746Sbsddlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` 12575746Sbsd 12675746Sbsd# chkerr: 12775746Sbsd# 12875746Sbsd# Routine to check for error 12995280Sobrien# 13095280Sobrien# checks error code and drops into shell on failure. 13195280Sobrien# if shell exits, terminates script as well as /etc/rc. 13295280Sobrien# 13395280Sobrienchkerr() { 134100280Sgordon case $1 in 13595280Sobrien 0) 136100280Sgordon ;; 137100280Sgordon *) 138100280Sgordon echo "$2 failed: dropping into /bin/sh" 139100280Sgordon /bin/sh 14095280Sobrien # RESUME 141104334Sdd ;; 142104334Sdd esac 143} 144 145# Create a generic memory disk 146# 147mount_md() { 148 /sbin/mdmfs -S -i 4096 -s $1 -M md $2 149} 150 151# Create the memory filesystem if it has not already been created 152# 153create_md() { 154 if [ "x`eval echo \\$md_created_$1`" = "x" ]; then 155 if [ "x`eval echo \\$md_size_$1`" = "x" ]; then 156 md_size=10240 157 else 158 md_size=`eval echo \\$md_size_$1` 159 fi 160 mount_md $md_size /$1 161 /bin/chmod 755 /$1 162 eval md_created_$1=created 163 fi 164} 165 166# DEBUGGING 167# 168# set -v 169 170# Figure out our interface and IP. 171# 172bootp_ifc="" 173bootp_ipa="" 174bootp_ipbca="" 175class="" 176if [ ${dlv:=0} -ne 0 ] ; then 177 iflist=`ifconfig -l` 178 for i in ${iflist} ; do 179 set -- `ifconfig ${i}` 180 while [ $# -ge 1 ] ; do 181 if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then 182 bootp_ifc=${i} ; bootp_ipa=${2} ; shift 183 fi 184 if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then 185 bootp_ipbca=$2; shift 186 fi 187 shift 188 done 189 if [ "${bootp_ifc}" != "" ] ; then 190 break 191 fi 192 done 193 # Get the values passed with the T134 bootp cookie. 194 class="`/sbin/sysctl -qn kern.bootp_cookie`" 195 196 echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca} ${class}" 197fi 198 199# Figure out our NFS root path 200# 201set -- `mount -t nfs` 202while [ $# -ge 1 ] ; do 203 if [ "$2" = "on" -a "$3" = "/" ]; then 204 nfsroot="$1" 205 break 206 fi 207 shift 208done 209 210# The list of directories with template files 211templates="base default" 212if [ -n "${bootp_ipbca}" ]; then 213 templates="${templates} ${bootp_ipbca} bcast/${bootp_ipbca}" 214fi 215if [ -n "${class}" ]; then 216 templates="${templates} ${class}" 217fi 218if [ -n "${bootp_ipa}" ]; then 219 templates="${templates} ${bootp_ipa} ip/${bootp_ipa}" 220fi 221 222# The list of filesystems to umount after the copy 223to_umount="" 224 225# If /conf/diskless_remount exists, remount all of /conf. This allows 226# multiple roots to share the same conf files. 227if [ -d /conf -a -f /conf/diskless_remount ]; then 228 nfspt=`/bin/cat /conf/diskless_remount` 229 if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then 230 nfspt="${nfsroot}${nfspt}" 231 fi 232 mount_nfs $nfspt /conf 233 chkerr $? "mount_nfs $nfspt /conf" 234 to_umount="/conf" 235fi 236 237# Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca}, 238# and /conf/${bootp_ipa}. For each subdirectory found within these 239# directories: 240# 241# - calculate memory filesystem sizes. If the subdirectory (prior to 242# NFS remounting) contains the file 'md_size', the contents specified 243# in 512 byte sectors will be used to size the memory filesystem. Otherwise 244# 8192 sectors (4MB) is used. 245# 246# - handle NFS remounts. If the subdirectory contains the file 247# diskless_remount, the contents of the file is NFS mounted over 248# the directory. For example /conf/base/etc/diskless_remount 249# might contain 'myserver:/etc'. NFS remounts allow you to avoid 250# having to dup your system directories in /conf. Your server must 251# be sure to export those filesystems -alldirs, however. 252# If the diskless_remount file contains a string beginning with a 253# '/' it is assumed that the local nfsroot should be prepended to 254# it before attemping to the remount. This allows the root to be 255# relocated without needing to change the remount files. 256# 257for i in ${templates} ; do 258 for j in /conf/$i/* ; do 259 # memory filesystem size specification 260 # 261 subdir=${j##*/} 262 if [ -d $j -a -f $j/md_size ]; then 263 eval md_size_$subdir=`cat $j/md_size` 264 fi 265 266 # remount 267 # 268 if [ -d $j -a -f $j/remount ]; then 269 nfspt=`/bin/cat $j/remount` 270 $nfspt $j 271 chkerr $? "$nfspt $j" 272 to_umount="$j ${to_umount}" # XXX hope it is really a mount! 273 fi 274 275 # NFS remount 276 # 277 if [ -d $j -a -f $j/diskless_remount ]; then 278 nfspt=`/bin/cat $j/diskless_remount` 279 if [ `expr "$nfspt" : '\(.\)'` = "/" ]; then 280 nfspt="${nfsroot}${nfspt}" 281 fi 282 mount_nfs $nfspt $j 283 chkerr $? "mount_nfs $nfspt $j" 284 to_umount="$j ${to_umount}" 285 fi 286 done 287done 288 289# - Create all required MFS filesystems and populate them from 290# our templates. Support both a direct template and a dir.cpio.gz 291# archive. Support dir.remove files containing a list of relative 292# paths to remove. 293# 294# The dir.cpio.gz form is there to make the copy process more efficient, 295# so if the cpio archive is present, it prevents the files from dir/ 296# from being copied. 297 298for i in ${templates} ; do 299 for j in /conf/$i/* ; do 300 subdir=${j##*/} 301 if [ -d $j -a ! -f $j.cpio.gz ]; then 302 create_md $subdir 303 cp -Rp $j/* /$subdir 304 fi 305 done 306 for j in /conf/$i/*.cpio.gz ; do 307 subdir=${j%*.cpio.gz} 308 subdir=${subdir##*/} 309 if [ -f $j ]; then 310 create_md $subdir 311 echo "Loading /$subdir from cpio archive $j" 312 (cd / ; /rescue/tar -xpf $j) 313 fi 314 done 315 for j in /conf/$i/*.remove ; do 316 subdir=${j%*.remove} 317 subdir=${subdir##*/} 318 if [ -f $j ]; then 319 # doubly sure it is a memory disk before rm -rf'ing 320 create_md $subdir 321 (cd /$subdir; rm -rf `/bin/cat $j`) 322 fi 323 done 324done 325 326# umount partitions used to fill the memory filesystems 327[ -n "${to_umount}" ] && umount $to_umount 328