netif-emulab.in 5.45 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#
# Emulab version of netif to DHCP on all interfaces simultaneously.
# Runs before the standard netif, and effectively neuters that script.
# Note the dependencies below.  Every script that declares that it must run
# BEFORE netif is listed as a REQUIRE here so that we are properly ordered
# w.r.t. those scripts.
#

# PROVIDE: netif-emulab
# REQUIRE: atm1 ipfilter mountcritlocal pccard serial sppp sysctl
# REQUIRE: adjkerntz hostname ipfs ipnat kldxref random
# BEFORE: netif
# KEYWORD: nojail

. /etc/rc.subr
. /etc/network.subr

name="netif-emulab"
start_cmd="cnet_start"
stop_cmd="cnet_stop"
_cmdifn=

23
24
25
26
# some emulab paths
ELAB_LOGDIR=@CLIENT_VARDIR@/logs
ELAB_BOOTDIR=@CLIENT_VARDIR@/boot

27
28
29
30
# this is a separate function so we can redirect all the output below
cnet_dhcp()
{
	#
31
	# Argh! FreeBSD >= 6 dhclient also allows only a single interface
32
33
34
35
36
37
38
39
40
41
42
	# to be specified.  I tried to make this work by launching multiple,
	# simultaneous dhclients, but that caused EEPROM read failures on
	# em devices.  So we will use the "pure" port version if it is
	# installed.
	#
	if [ -x /usr/local/sbin/dhclient ]; then
	    echo "Using dhclient port..."
	    dhclient_program="/usr/local/sbin/dhclient"
	    ${dhclient_program} ${dhclient_flags} $*
	else
	    echo "Using default dhclient..."
43
44
45
46
47
48
49

	    #
	    # Argh redux! FreeBSD >= 6 has rewritten dhclient and it no longer
	    # takes the -i option.  So we remove that option here.
	    #
	    dhclient_flags=`echo $dhclient_flags | sed -e 's/-i [0-9][0-9]*//'`

50
	    for _if in $*; do
51
		${dhclient_program} ${dhclient_flags} -b $_if
52
53
54
	    done
	    wait
	fi
55
56
57
58
}

cnet_start()
{
59
	rm -f $ELAB_BOOTDIR/controlif
60

61
	#
62
	# Find all the relevant networks IFs.
63
64
65
66
67
68
69
	# Don't do loopback, funky tunnel devices
	# (e.g., IP-over-string-and-cans).
	#
	# At Utah Emulab we also exclude wireless interfaces as our
	# wireless machines are in a production ether and might get
	# an answer!
	#
70
71
72
73
74
75
	_ifs=""
	network_interfaces=`ifconfig -l | sed -e 's/lo0//'`
	for _if in $network_interfaces; do
	case $_if in
	lo*|gif*|faith*|tun*|plip*)
		;;
76
77
	ath*)
		;;
78
79
80
81
82
83
	*)
		_ifs="$_ifs $_if"
		;;
	esac
	done

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
	# 
	# For the widearea case: decide if we want static or dynamic 
	# network configuration based on a config file.
	# XXX: should try to look on other media as well, instead of just
	# the boot dongle.
	#
	WACONFIG=/etc/emulab/waconfig
	if [ -e "$WACONFIG" ]; then
	        # key=val pairs become shell vars
		. "$WACONFIG"

		echo "Found Emulab widearea config info..."

		if [ "$WA_BOOTMETHOD" = "static" -a "$WA_MAC" != "" ]; then 
			# try to find an iface matching WA_MAC
			cnetif=""
			for _if in $network_interfaces; do
			        mac=`ifconfig $_if | grep ether | sed -e 's/[[:space:]]*ether[[:space:]]*//'`
				echo "$mac" | grep -i "$WA_MAC"
				if [ "$?" = "0" ]; then
				        cnetif="$_if"
				        break
				fi
			done

David Johnson's avatar
David Johnson committed
109
			if [ "$cnetif" = "" ]; then
110
111
112
			        echo -n "Could not find iface with "
				echo "$WA_MAC; trying DHCP!"
			        WA_BOOTMETHOD="dhcp"
David Johnson's avatar
David Johnson committed
113
114
115
116
117
118
			elif [ "$WA_HOSTNAME" = "" \
			        -o "$WA_DOMAIN" = "" \
			        -o "$WA_IP_ADDR" = "" \
			        -o "$WA_IP_NETMASK" = "" \
			        -o "$WA_IP_GATEWAY" = "" \
			        -o "$WA_IP_DNS1" = ""  ]; then
119
120
121
122
123
				echo -n "Missing static IP config "
				echo "vars; trying DHCP!"
				WA_BOOTMETHOD="dhcp"
			fi
		fi
124
	else
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
		WA_BOOTMETHOD="dhcp"
	fi

	if [ "$WA_BOOTMETHOD" = "dhcp" ]; then
	        #
	        # use good ol' Emulab dhcp!
		#
		echo "Emulab looking for control net among: $_ifs ..."
		cnet_dhcp $_ifs >$ELAB_LOGDIR/netif-emulab.log 2>&1
		if [ -e $ELAB_BOOTDIR/controlif ]; then
			echo "Emulab control net is `cat $ELAB_BOOTDIR/controlif`"
		else
			echo "*** No Emulab control net found!"
		fi
	else
		#
		# configure according to static WA_X vars:
		#
		echo "Emulab statically configuring control net on $cnetif ..."
		ifconfig "$cnetif" inet "$WA_IP_ADDR" netmask "$WA_IP_NETMASK" up
		route add default "$WA_IP_GATEWAY"

		# bring up lo0 too, since we neuter netif
		ifconfig lo0 inet 127.0.0.1 up
		route add 127.0.0.1 -interface lo0

		# setup resolv.conf
		echo "search $WA_DOMAIN" > /etc/resolv.conf
		echo "nameserver $WA_IP_DNS1" >> /etc/resolv.conf
David Johnson's avatar
David Johnson committed
154
		if [ "$WA_IP_DNS2" != "" ]; then
155
156
157
158
			echo "nameserver $WA_IP_DNS2" >> /etc/resolv.conf
		fi

		# set hostname
159
160
161
162
163
164
165
166
		hosts_str="$WA_HOSTNAME"
                echo "$WA_HOSTNAME" | grep -q \\.
                if [ $? = 0 ]; then
                        hostname "$WA_HOSTNAME"
                else
                        hostname "${WA_HOSTNAME}.${WA_DOMAIN}"
			hosts_str="${WA_HOSTNAME}.${WA_DOMAIN} ${hosts_str}"
                fi
167
168

		# setup hosts file
169
		echo "$WA_IP_ADDR ${hosts_str}" >> /etc/hosts
170
171
172
173

		# setup a few necessary emulab files...
		echo "$cnetif" > $ELAB_BOOTDIR/controlif
		if [ -e "/etc/emulab/bossnode" ]; then
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
			bossnode=`cat /etc/emulab/bossnode`
			bossip=`host -t A "$bossnode"`
			retval="$?"
			i=0
			while [ "$retval" != "0" -a $i -lt 180 ]; do
				echo "Failed to resolve boss ($bossnode)!"
				sleep 5
				i=`expr $i + 1`
				bossip=`host -t A "$bossnode"`
				retval="$?"
			done
			if [ "$retval" = "0" ]; then
				echo "Resolved boss ($bossnode) to $bossip."
				echo `echo "$bossip" | sed -n -e 's/.*has address\s*\(.*\)/\1/p'` \
					> $ELAB_BOOTDIR/bossip
		        fi
190
191
192
193
194
		fi
		echo "$WA_HOSTNAME" > $ELAB_BOOTDIR/realname
		echo "$WA_IP_GATEWAY" > $ELAB_BOOTDIR/routerip
		echo "$WA_IP_ADDR" > $ELAB_BOOTDIR/myip
		echo "$WA_IP_NETMASK" > $ELAB_BOOTDIR/mynetmask
195
196
197
198
199
	fi
}

cnet_stop()
{
200
	rm -f $ELAB_BOOTDIR/controlif
201
202
203
204
}

load_rc_config $name
run_rc_command $*