rc.initdiskless revision 66830
166830Sobrien# Copyright (c) 1999  Matt Dillion
266830Sobrien# All rights reserved.
366830Sobrien#
466830Sobrien# Redistribution and use in source and binary forms, with or without
566830Sobrien# modification, are permitted provided that the following conditions
666830Sobrien# are met:
766830Sobrien# 1. Redistributions of source code must retain the above copyright
866830Sobrien#    notice, this list of conditions and the following disclaimer.
966830Sobrien# 2. Redistributions in binary form must reproduce the above copyright
1066830Sobrien#    notice, this list of conditions and the following disclaimer in the
1166830Sobrien#    documentation and/or other materials provided with the distribution.
1266830Sobrien#
1366830Sobrien# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1466830Sobrien# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1566830Sobrien# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1666830Sobrien# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1766830Sobrien# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1866830Sobrien# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1966830Sobrien# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2066830Sobrien# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2166830Sobrien# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2266830Sobrien# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2366830Sobrien# SUCH DAMAGE.
2466830Sobrien#
2551231Ssheldonh# $FreeBSD: head/etc/rc.initdiskless 66830 2000-10-08 19:20:36Z obrien $
2651231Ssheldonh#
2766830Sobrien
2866830Sobrien#
2955520Sluigi# /etc/rc.diskless1 - general BOOTP startup
3043803Sdillon#
3155520Sluigi# BOOTP has mounted / for us.  Assume a read-only mount.  We must then
3255520Sluigi# - figure out our IP by querying the interface
3355520Sluigi# - fill /conf/etc (writable) with files from /etc, and then update
3455520Sluigi#   per-machine files from /conf/*/ where * is the IP of the host,
3555520Sluigi#   the IP of the subnet, "default", or nothing.
3655520Sluigi# - mount /conf/etc over /etc so we can see the new files.
3743803Sdillon#
3855520Sluigi# WARNING: i thing you should not change /etc/rc or strange things could
3955520Sluigi# happen.
4043803Sdillon#
4155520Sluigi# The operator is in charge of setting /conf/*/etc/* things as appropriate.
4255520Sluigi# Typically rc.conf and fstab need to be changed, but possibly
4355520Sluigi# also other files such as inetd.conf etc.
4443803Sdillon
4543803Sdillon# chkerr:
4643803Sdillon#
4743803Sdillon# Routine to check for error
4843803Sdillon#
4943803Sdillon#	checks error code and drops into shell on failure.
5043803Sdillon#	if shell exits, terminates script as well as /etc/rc.
5151231Ssheldonh#
5243803Sdillonchkerr() {
5351231Ssheldonh	case $1 in
5451231Ssheldonh	0)
5551231Ssheldonh		;;
5651231Ssheldonh	*)
5743803Sdillon		echo "$2 failed: dropping into /bin/sh"
5843803Sdillon		/bin/sh
5943803Sdillon		# RESUME
6051231Ssheldonh		;;
6151231Ssheldonh	esac
6243803Sdillon}
6343803Sdillon
6443803Sdillon# DEBUGGING
6543803Sdillon#
6655520Sluigi# set -v
6743803Sdillon
6851231Ssheldonh# Figure out our interface and IP.
6943803Sdillon#
7055520Sluigibootp_ifc=""
7155520Sluigibootp_ipa=""
7255520Sluigibootp_ipbca=""
7355520Sluigiiflist=`ifconfig -l`
7455520Sluigifor i in ${iflist} ; do
7555520Sluigi    set `ifconfig ${i}`
7655520Sluigi    while [ $# -ge 1 ] ; do
7755520Sluigi        if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
7855520Sluigi            bootp_ifc=${i} ; bootp_ipa=${2} ; shift
7955520Sluigi        fi
8055520Sluigi        if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
8155520Sluigi            bootp_ipbca=$2; shift
8255520Sluigi        fi
8355520Sluigi        shift
8455520Sluigi    done
8555520Sluigi    if [ "${bootp_ifc}" != "" ] ; then
8655520Sluigi        break
8755520Sluigi    fi
8855520Sluigidone
8955520Sluigiecho "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}"
9055520Sluigi 
9155520Sluigi# Files in /etc are copied to /conf/etc which is writable. Then
9255520Sluigi# per-machine configs from /conf/ip.address/etc are copied onto this
9355520Sluigi# directory. First choice is using the client's IP, then the client's
9455520Sluigi# broadcast address, then a default configuration.
9555520Sluigi# This way we have some flexibility to handle clusters of machines
9655520Sluigi# on separate subnets.
9743803Sdillon#
9843803Sdillon# WARNING! null mounts cannot handle mmap, and since many programs
9943803Sdillon# use mmap (such as 'cp'), we have to copy.
10051231Ssheldonh#
10155520Sluigimount_mfs -s 2048 -T qp120at dummy /conf/etc
10255520Sluigicp -Rp /etc/* /conf/etc
10355520Sluigichkerr $? "MFS mount on /conf/etc"
10443803Sdillon
10555520Sluigiif [ -d /conf/${bootp_ipa} ] ; then
10655520Sluigi        cp -Rp /conf/${bootp_ipa}/etc/* /conf/etc
10755520Sluigielif [ -d /conf/${bootp_ipbca} ] ; then
10855520Sluigi        cp -Rp /conf/${bootp_ipbca}/etc/* /conf/etc
10955520Sluigielse
11055520Sluigi        cp -Rp /conf/default/etc/* /conf/etc
11155520Sluigifi
11243803Sdillon
11355520Sluigi# Make the new directory available as /etc
11443803Sdillon#
11555520Sluigimount_null /conf/etc /etc
11643803Sdillon
11743803Sdillon# Tell /etc/rc to run the specified script after
11843803Sdillon# it does its mounts but before it does anything
11943803Sdillon# else.
12043803Sdillon#
12143803Sdillon# This script is responsible for setting up the
12251231Ssheldonh# diskless mount environment.  This can be
12343803Sdillon# overriden by /conf/ME/rc.conf.local if, for
12443803Sdillon# example, you do not want to run the standard
12543803Sdillon# system /etc/rc.diskless2
12643803Sdillon
12743803Sdillondiskless_mount="/etc/rc.diskless2"
128