rc.initdiskless revision 100280
1#!/bin/sh
2#
3# Copyright (c) 1999  Matt Dillion
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: head/etc/rc.initdiskless 100280 2002-07-18 05:00:17Z gordon $
28#
29
30# PROVIDE: initdiskless
31# KEYWORD: FreeBSD
32
33dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
34[ ${dlv:=0} -eq 0 ] && exit 0
35
36#
37# BOOTP has mounted / for us.  Assume a read-only mount.  We must then
38# - figure out our IP by querying the interface
39# - mount /etc as an MFS
40# - populate /etc from /conf/default version
41# - override files in /etc with files from /conf/*/etc where
42#   '*' is default, netmask of client, ip-address of client
43#
44# The operator is in charge of setting /conf/*/etc/* things as appropriate.
45# Typically rc.conf and fstab need to be changed, but possibly also other
46# files such as inetd.conf etc.
47
48# chkerr:
49#
50# Routine to check for error
51#
52#	checks error code and drops into shell on failure.
53#	if shell exits, terminates script as well as /etc/rc.
54#
55chkerr()
56{
57	case $1 in
58	0)
59		;;
60	*)
61		echo "$2 failed: dropping into /bin/sh"
62		/bin/sh
63		# RESUME
64		;;
65	esac
66}
67
68mount_md()
69{
70	/sbin/mdconfig -a -t malloc -s $1 -u $3
71	/sbin/disklabel -r -w md$3 auto
72	/sbin/newfs /dev/md$3c
73	/sbin/mount /dev/md$3c $2
74}
75
76# DEBUGGING
77#
78# set -v
79
80# Figure out our interface and IP.
81#
82bootp_ifc=""
83bootp_ipa=""
84bootp_ipbca=""
85iflist=`ifconfig -l`
86for i in ${iflist} ; do
87    set `ifconfig ${i}`
88    while [ $# -ge 1 ] ; do
89        if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
90            bootp_ifc=${i} ; bootp_ipa=${2} ; shift
91        fi
92        if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
93            bootp_ipbca=$2; shift
94        fi
95        shift
96    done
97    if [ "${bootp_ifc}" != "" ] ; then
98        break
99    fi
100done
101echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}"
102
103if [ -z "`hostname -s`" ]; then
104	hostname=`kenv dhcp.host-name`
105	hostname $hostname
106	echo "Hostname is $hostname"
107fi
108
109if [ -d /conf/default/etc ]; then
110	mount_md 4096 /etc 0
111	chkerr $? "MFS mount on /etc"
112	/bin/chmod 755 /etc
113
114	/bin/cp -Rp /conf/default/etc/* /etc
115	chkerr $? "cp /conf/default/etc to /etc MFS"
116fi
117
118# Allow for override files to replace files in /etc.  Use /conf/*/etc to find
119# the override files.  First choice is default files that # always override,
120# then files that from the directory that matches the client's broadcast
121# address, finally followed by overrides that match the client's IP address.
122#
123# This way we have some flexibility to handle clusters of machines on
124# separate subnets.
125
126for i in ${bootp_ipbca} ${bootp_ipa} ${hostname} ; do
127	if [ -d /conf/${i}/etc ]; then
128		cp -Rp /conf/${i}/etc/* /etc
129	fi
130done
131
132#
133# if the info is available via dhcp/kenv
134# build the resolv.conf
135#
136if [ ! -e /etc/resolv.conf ]; then
137	echo domain `kenv dhcp.domain-name` > /etc/resolv.conf
138
139	set `kenv dhcp.domain-name-servers`
140	for ns in `IFS=','; echo $*`; do
141		echo nameserver $ns >> /etc/resolv.conf;
142	done
143fi
144