rc.initdiskless revision 100280
1100280Sgordon#!/bin/sh
2100280Sgordon#
366830Sobrien# Copyright (c) 1999  Matt Dillion
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 100280 2002-07-18 05:00:17Z gordon $
2851231Ssheldonh#
2966830Sobrien
30100280Sgordon# PROVIDE: initdiskless
31100280Sgordon# KEYWORD: FreeBSD
32100280Sgordon
33100280Sgordondlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
34100280Sgordon[ ${dlv:=0} -eq 0 ] && exit 0
35100280Sgordon
3666830Sobrien#
3755520Sluigi# BOOTP has mounted / for us.  Assume a read-only mount.  We must then
3855520Sluigi# - figure out our IP by querying the interface
3975746Sbsd# - mount /etc as an MFS
4088496Srwatson# - populate /etc from /conf/default version
4175746Sbsd# - override files in /etc with files from /conf/*/etc where
4275746Sbsd#   '*' is default, netmask of client, ip-address of client
4343803Sdillon#
4455520Sluigi# The operator is in charge of setting /conf/*/etc/* things as appropriate.
4589437Srwatson# Typically rc.conf and fstab need to be changed, but possibly also other
4689437Srwatson# files such as inetd.conf etc.
4743803Sdillon
4843803Sdillon# chkerr:
4943803Sdillon#
5043803Sdillon# Routine to check for error
5143803Sdillon#
5243803Sdillon#	checks error code and drops into shell on failure.
5343803Sdillon#	if shell exits, terminates script as well as /etc/rc.
5451231Ssheldonh#
55100280Sgordonchkerr()
56100280Sgordon{
5751231Ssheldonh	case $1 in
5851231Ssheldonh	0)
5951231Ssheldonh		;;
6051231Ssheldonh	*)
6143803Sdillon		echo "$2 failed: dropping into /bin/sh"
6243803Sdillon		/bin/sh
6343803Sdillon		# RESUME
6451231Ssheldonh		;;
6551231Ssheldonh	esac
6643803Sdillon}
6743803Sdillon
68100280Sgordonmount_md()
69100280Sgordon{
7075931Simp	/sbin/mdconfig -a -t malloc -s $1 -u $3
7175931Simp	/sbin/disklabel -r -w md$3 auto
7275931Simp	/sbin/newfs /dev/md$3c
7375931Simp	/sbin/mount /dev/md$3c $2
7475931Simp}
7575931Simp
7643803Sdillon# DEBUGGING
7743803Sdillon#
7855520Sluigi# set -v
7943803Sdillon
8051231Ssheldonh# Figure out our interface and IP.
8143803Sdillon#
8255520Sluigibootp_ifc=""
8355520Sluigibootp_ipa=""
8455520Sluigibootp_ipbca=""
8555520Sluigiiflist=`ifconfig -l`
8655520Sluigifor i in ${iflist} ; do
8755520Sluigi    set `ifconfig ${i}`
8855520Sluigi    while [ $# -ge 1 ] ; do
8955520Sluigi        if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
9055520Sluigi            bootp_ifc=${i} ; bootp_ipa=${2} ; shift
9155520Sluigi        fi
9255520Sluigi        if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
9355520Sluigi            bootp_ipbca=$2; shift
9455520Sluigi        fi
9555520Sluigi        shift
9655520Sluigi    done
9755520Sluigi    if [ "${bootp_ifc}" != "" ] ; then
9855520Sluigi        break
9955520Sluigi    fi
10055520Sluigidone
10155520Sluigiecho "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}"
10243803Sdillon
10395280Sobrienif [ -z "`hostname -s`" ]; then
104100280Sgordon	hostname=`kenv dhcp.host-name`
105100280Sgordon	hostname $hostname
10695280Sobrien	echo "Hostname is $hostname"
10795280Sobrienfi
10895280Sobrien
10991074Sobrienif [ -d /conf/default/etc ]; then
11091074Sobrien	mount_md 4096 /etc 0
11191074Sobrien	chkerr $? "MFS mount on /etc"
11291074Sobrien	/bin/chmod 755 /etc
11375101Sbsd
11491074Sobrien	/bin/cp -Rp /conf/default/etc/* /etc
11591074Sobrien	chkerr $? "cp /conf/default/etc to /etc MFS"
11691074Sobrienfi
11743803Sdillon
11889438Srwatson# Allow for override files to replace files in /etc.  Use /conf/*/etc to find
11989438Srwatson# the override files.  First choice is default files that # always override,
12089438Srwatson# then files that from the directory that matches the client's broadcast
12189438Srwatson# address, finally followed by overrides that match the client's IP address.
12243803Sdillon#
12389438Srwatson# This way we have some flexibility to handle clusters of machines on
12489438Srwatson# separate subnets.
12543803Sdillon
12695280Sobrienfor i in ${bootp_ipbca} ${bootp_ipa} ${hostname} ; do
12775746Sbsd	if [ -d /conf/${i}/etc ]; then
12875746Sbsd		cp -Rp /conf/${i}/etc/* /etc
12975746Sbsd	fi
13075746Sbsddone
13175746Sbsd
13295280Sobrien#
13395280Sobrien# if the info is available via dhcp/kenv
13495280Sobrien# build the resolv.conf
13595280Sobrien#
13695280Sobrienif [ ! -e /etc/resolv.conf ]; then
137100280Sgordon	echo domain `kenv dhcp.domain-name` > /etc/resolv.conf
13895280Sobrien
139100280Sgordon	set `kenv dhcp.domain-name-servers`
140100280Sgordon	for ns in `IFS=','; echo $*`; do
141100280Sgordon		echo nameserver $ns >> /etc/resolv.conf;
142100280Sgordon	done
14395280Sobrienfi
144