summaryrefslogtreecommitdiffstats
path: root/etc/rc.initdiskless
blob: 1b720090783e7ec8733f0d17009901a2741994e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/sh
#
# Copyright (c) 1999  Matt Dillion
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#

# PROVIDE: initdiskless
# KEYWORD: FreeBSD

dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null`
[ ${dlv:=0} -eq 0 ] && exit 0

#
# BOOTP has mounted / for us.  Assume a read-only mount.  We must then
# - figure out our IP by querying the interface
# - mount /etc as an MFS
# - populate /etc from /conf/default version
# - override files in /etc with files from /conf/*/etc where
#   '*' is default, netmask of client, ip-address of client
#
# The operator is in charge of setting /conf/*/etc/* things as appropriate.
# Typically rc.conf and fstab need to be changed, but possibly also other
# files such as inetd.conf etc.

# chkerr:
#
# Routine to check for error
#
#	checks error code and drops into shell on failure.
#	if shell exits, terminates script as well as /etc/rc.
#
chkerr()
{
	case $1 in
	0)
		;;
	*)
		echo "$2 failed: dropping into /bin/sh"
		/bin/sh
		# RESUME
		;;
	esac
}

mount_md()
{
	/sbin/mdconfig -a -t malloc -s $1 -u $3
	/sbin/disklabel -r -w md$3 auto
	/sbin/newfs /dev/md$3c
	/sbin/mount /dev/md$3c $2
}

# DEBUGGING
#
# set -v

# Figure out our interface and IP.
#
bootp_ifc=""
bootp_ipa=""
bootp_ipbca=""
iflist=`ifconfig -l`
for i in ${iflist} ; do
    set `ifconfig ${i}`
    while [ $# -ge 1 ] ; do
        if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then
            bootp_ifc=${i} ; bootp_ipa=${2} ; shift
        fi
        if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then
            bootp_ipbca=$2; shift
        fi
        shift
    done
    if [ "${bootp_ifc}" != "" ] ; then
        break
    fi
done
echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca}"

if [ -z "`hostname -s`" ]; then
	hostname=`kenv dhcp.host-name`
	hostname $hostname
	echo "Hostname is $hostname"
fi

if [ -d /conf/default/etc ]; then
	mount_md 4096 /etc 0
	chkerr $? "MFS mount on /etc"
	/bin/chmod 755 /etc

	/bin/cp -Rp /conf/default/etc/* /etc
	chkerr $? "cp /conf/default/etc to /etc MFS"
fi

# Allow for override files to replace files in /etc.  Use /conf/*/etc to find
# the override files.  First choice is default files that # always override,
# then files that from the directory that matches the client's broadcast
# address, finally followed by overrides that match the client's IP address.
#
# This way we have some flexibility to handle clusters of machines on
# separate subnets.

for i in ${bootp_ipbca} ${bootp_ipa} ${hostname} ; do
	if [ -d /conf/${i}/etc ]; then
		cp -Rp /conf/${i}/etc/* /etc
	fi
done

#
# if the info is available via dhcp/kenv
# build the resolv.conf
#
if [ ! -e /etc/resolv.conf ]; then
	echo domain `kenv dhcp.domain-name` > /etc/resolv.conf

	set `kenv dhcp.domain-name-servers`
	for ns in `IFS=','; echo $*`; do
		echo nameserver $ns >> /etc/resolv.conf;
	done
fi
OpenPOWER on IntegriCloud