rc.initdiskless revision 75746
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 75746 2001-04-20 23:10:11Z bsd $
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
3375746Sbsd# - copy /etc temporarily out of the way to /tmp/etc
3475746Sbsd# - mount /etc as an MFS
3575746Sbsd# - repopulate /etc with the saved off copy
3675746Sbsd# - override files in /etc with files from /conf/*/etc where
3775746Sbsd#   '*' is default, netmask of client, ip-address of client
3843803Sdillon#
3955520Sluigi# WARNING: i thing you should not change /etc/rc or strange things could
4055520Sluigi# happen.
4143803Sdillon#
4255520Sluigi# The operator is in charge of setting /conf/*/etc/* things as appropriate.
4355520Sluigi# Typically rc.conf and fstab need to be changed, but possibly
4455520Sluigi# also other files such as inetd.conf etc.
4543803Sdillon
4643803Sdillon# chkerr:
4743803Sdillon#
4843803Sdillon# Routine to check for error
4943803Sdillon#
5043803Sdillon#	checks error code and drops into shell on failure.
5143803Sdillon#	if shell exits, terminates script as well as /etc/rc.
5251231Ssheldonh#
5343803Sdillonchkerr() {
5451231Ssheldonh	case $1 in
5551231Ssheldonh	0)
5651231Ssheldonh		;;
5751231Ssheldonh	*)
5843803Sdillon		echo "$2 failed: dropping into /bin/sh"
5943803Sdillon		/bin/sh
6043803Sdillon		# RESUME
6151231Ssheldonh		;;
6251231Ssheldonh	esac
6343803Sdillon}
6443803Sdillon
6543803Sdillon# DEBUGGING
6643803Sdillon#
6755520Sluigi# set -v
6843803Sdillon
6951231Ssheldonh# Figure out our interface and IP.
7043803Sdillon#
7155520Sluigibootp_ifc=""
7255520Sluigibootp_ipa=""
7355520Sluigibootp_ipbca=""
7455520Sluigiiflist=`ifconfig -l`
7555520Sluigifor i in ${iflist} ; do
7655520Sluigi    set `ifconfig ${i}`
7755520Sluigi    while [ $# -ge 1 ] ; do
7855520Sluigi        if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
7955520Sluigi            bootp_ifc=${i} ; bootp_ipa=${2} ; shift
8055520Sluigi        fi
8155520Sluigi        if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
8255520Sluigi            bootp_ipbca=$2; shift
8355520Sluigi        fi
8455520Sluigi        shift
8555520Sluigi    done
8655520Sluigi    if [ "${bootp_ifc}" != "" ] ; then
8755520Sluigi        break
8855520Sluigi    fi
8955520Sluigidone
9055520Sluigiecho "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}"
9143803Sdillon
9275101Sbsd
9375746Sbsd# Create an MFS /tmp to temporarily hold files from /etc until we
9475746Sbsd# can bootstrap /etc as an MFS.
9575101Sbsd
9675746Sbsd/sbin/mount_mfs -s 4096 -T qp120at dummy /tmp
9775746Sbsdchkerr $? "MFS mount on /tmp"
9875746Sbsd/bin/cp -Rp /etc /tmp
9975746Sbsdchkerr $? "cp /etc to /tmp/etc MFS"
10075101Sbsd
10175746Sbsd/sbin/mount_mfs -s 4096 -T qp120at dummy /etc
10275746Sbsdchkerr $? "MFS mount on /etc"
10375746Sbsd/bin/chmod 755 /etc
10443803Sdillon
10575746Sbsd/bin/cp -Rp /tmp/etc/* /etc
10675746Sbsdchkerr $? "cp /tmp/etc to /etc MFS"
10775746Sbsd
10875746Sbsdrm -rf /tmp/etc
10975746Sbsd/sbin/umount /tmp
11075746Sbsd
11175746Sbsd
11275746Sbsd# Allow for override files to replace files in /etc.  Use /conf/*/etc
11375746Sbsd# to find the override files.  First choice is default files that
11475746Sbsd# always override, then files that from the directory that matches the
11575746Sbsd# client's broadcast address, finally followed by overrides that match
11675746Sbsd# the client's IP address.
11743803Sdillon#
11875746Sbsd# This way we have some flexibility to handle clusters of machines
11975746Sbsd# on separate subnets.
12075746Sbsd#
12143803Sdillon
12275746Sbsdfor i in default ${bootp_ipbca} ${bootp_ipa} ; do
12375746Sbsd	if [ -d /conf/${i}/etc ]; then
12475746Sbsd		cp -Rp /conf/${i}/etc/* /etc
12575746Sbsd	fi
12675746Sbsddone
12775746Sbsd
12843803Sdillon# Tell /etc/rc to run the specified script after
12943803Sdillon# it does its mounts but before it does anything
13043803Sdillon# else.
13143803Sdillon#
13243803Sdillon# This script is responsible for setting up the
13351231Ssheldonh# diskless mount environment.  This can be
13443803Sdillon# overriden by /conf/ME/rc.conf.local if, for
13543803Sdillon# example, you do not want to run the standard
13643803Sdillon# system /etc/rc.diskless2
13743803Sdillon
13843803Sdillondiskless_mount="/etc/rc.diskless2"
139