summaryrefslogtreecommitdiff
path: root/setup-interfaces.in
blob: c3bff78c6fcecefd5f1025b2e3a0af7b4120a8ea (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/bin/sh

PROGRAM=setup-interfaces
PREFIX=

for i in ./libalpine.sh $PREFIX/lib/libalpine.sh; do
	[ -e $i ] && . $i && break
done

unconfigured_add() {
	touch $1.noconf
}

unconfigured_detect() {
	local i=
	for i in ${INTERFACES:-$(available_ifaces)}; do
		if [ "$i" != "lo" ]; then
			unconfigured_add $i
		fi
	done
}

unconfigured_get_first() {
	ls *.noconf 2>/dev/null | head -n 1 | sed 's/.noconf//'
}

unconfigured_del() {
	rm $1.noconf
}

unconfigured_all_done() {
	local i=
	for i in *.noconf; do
		[ -e $i ] && return 1
	done
	return 0
}

unconfigured_list() {
	local list= i=
	for i in *.noconf; do
		[ -e "$i" ] || continue
		list="${list} ${i%.noconf}"
	done
	echo $list
}

unconfigured_isin() {
	[ -f $1.noconf ]
}

iface_exists() {
	test -e /sys/class/net/$1
}

get_default_addr() {
	# check if dhcpcd is running
	if pidof dhcpcd > /dev/null && [ -f "$ROOT/var/lib/dhcpc/dhcpcd-$1.info" ]; then
		echo dhcp
	elif iface_exists; then
		ip addr show $1 | awk '/inet / {print $2}' | head -n 1 | sed 's:/.*::'
	fi
}

get_default_mask() {
	if [ "$1" ] ; then
		ipcalc -m $1 | sed 's/.*=//'
	else
		echo "255.255.255.0"
	fi
}

get_default_gateway() {
	ip route show dev $1 | awk '/^default/ {print $3}'
}

ipaddr_help() {
	cat <<__EOF__

For static ip:
Enter the ip address in the format x.x.x.x

For dhcp:
Enter 'dhcp'

To add this interface to a bridge enter the bridge name (eg 'br0' or 'bridge0')

__EOF__
}

bridge_add_iface() {
	local bridge=$1 iface=
	shift
	for iface; do
		echo $iface >> $bridge.bridge_ports
		unconfigured_add $bridge
		unconfigured_del $iface
	done
}

config_iface() {
	local iface=$1
	local prefix=$2
	local address= netmask= gateway= bridge_ports=
	local bridge
	local conf=$prefix$iface.conf
	local answer=

	while [ -n "$ask_bridge" ]; do
		ask "Do you want to bridge the interface $iface?" no
		case "$resp" in
			yes|y) resp=yes; break;;
			no|n) break;;
		esac
	done

	if [ "$resp" = "yes" ]; then
		bridge="br"`echo $iface | sed 's/[^0-9]//g'`
		ask "Name of the bridge you want add $iface to:" $bridge
		bridge_add_iface $resp $iface
		return
	fi

	if [ -r "$iface.bridge_ports" ]; then
		bridge_ports=$(sed 's/\n/ /' $iface.bridge_ports)
		echo "bridge_ports=\"$bridge_ports\"" >> $conf
	fi
	# use ipcalc to validate the address. we do accept /mask
	# we are no interested in the result, only error code, so
	# we send result to /dev/null
	while ! ipcalc -s -m $address >/dev/null 2>&1; do
		address=`get_default_addr $iface`
		[ -z "$address" ] && address="dhcp"
		ask "Ip address for $iface? (or 'dhcp' or '?')" $address
		address=$resp
		case "$resp" in
		'?')	ipaddr_help;;
		"abort") return;;
		"dhcp")
			HAS_DHCP=yes
			echo "type=dhcp" >> $conf
			unconfigured_del $iface
			return ;;
		br[0-9]*|bridge[0-9]*)
			case "$iface" in
				# we dont allow bridge bridges
				br[0-9]*|bridge[0-9]*) continue;;
			esac
			bridge_add_iface $resp $iface
			return ;;
		esac
	done

	# extract netmask if entered together with address
	if [ "$address" != "${address%%/*}" ]; then
		netmask=$(ipcalc -s -m $address | cut -d= -f2)
	fi

	# use ipcalc -m to validate netmask. we dont accept <addr>/mask suffix
	# so we pass on a dummy mask to ipcalc.
	while ! ipcalc -s -m $netmask/0 >/dev/null 2>&1; do
		netmask=`get_default_mask $address`
		ask "Netmask?" $netmask
		netmask=$resp
		[ "$netmask" = "abort" ] && return
	done

	# use ipcalc -m to validate netmask. we dont accept <addr>/mask suffix
	# so we pass on a dummy mask to ipcalc.
	while ! ipcalc -s -m $gateway/0 >/dev/null 2>&1; do
		gateway=`get_default_gateway $iface`
		[ -z "$gateway" ] && gateway=none
		ask "Gateway? (or 'none')" $gateway
		gateway=$resp
		[ "$gateway" = "abort" ] && return
		[ "$gateway" = "none" ] && gateway=""
		[ -z "$gateway" ] && break
	done
	
	echo "type=static" >> $conf
	if [ -n "$bridge_ports" ]; then
		echo "bridge_ports=$bridge_ports" >> $conf
	fi
	echo "address=${address%%/*}" >> $conf  #strip off /mask if there
	echo "netmask=$netmask" >> $conf
	echo "gateway=$gateway" >> $conf


	# print summary
	echo "Configuration for $iface:"
	sed 's/^/  /' $conf
	
	unconfigured_del $iface
}

usage() {
        cat <<__EOF__
usage: setup-interfaces [-bhi] [-p ROOT]

Setup network interfaces

options:
 -b  Ask for bridging of interfaces
 -h  Show this help
 -i  Read new contents of /etc/network/interfaces from stdin
 -p  Use ROOT as system prefix
__EOF__
        exit 1
}

prompt_for_interfaces() {
	init_tmpdir TMP
	
	cd $TMP
	unconfigured_detect
	
	index=1
	while ! unconfigured_all_done; do
		echo "Available interfaces are: $(unconfigured_list)."
		ask "Which one do you want to initialize? (or 'done')" \
			$(unconfigured_get_first)
		iface=$resp
	
		[ "$iface" = "done" ] && break
		unconfigured_isin $iface || continue
		config_iface $iface $(printf "%.3d~" $index)
		index=$(( $index + 1 ))
	done
	
	echo "type=loopback" > 000~lo.conf
	echo "" > interface
	hostname=$(cat $ROOT/etc/hostname 2>/dev/null)
	
	for i in *.conf ; do
		iface=`basename $i .conf`
		iface=${iface#[0-9]*~}
		bridge_ports=
		address=
		type=
		gateway=
		. ./$i
		echo "auto $iface" >> interfaces
		echo "iface $iface inet $type" >> interfaces
		if [ -n "$bridge_ports" ]; then
			echo -e "\tbridge-ports $bridge_ports" >> interfaces
		fi
		case $type in
		dhcp)
			[ -n "$hostname" ] \
				&& echo -e "\thostname $hostname" >> interfaces
			;;
		static)
			echo -e "\taddress $address" >> interfaces
			echo -e "\tnetmask $netmask" >> interfaces
			[ "$gateway" ] \
				&& echo -e "\tgateway $gateway" >> interfaces
			;;
		esac
		if [ -n "$bridge" ]; then
			echo -e "\tpost-down brctl delif $bridge $iface" >> interfaces
			echo -e "\tpost-down brctl delbr $bridge" >> interfaces
		fi
		echo "" >> interfaces
		bridge=""
	done
	
	while [ "$answer" != "yes" ] && [ "$answer" != "no" ] ; do
		ask "Do you want to do any manual network configuration?" no
		case $resp in
			y) answer=yes;;
			n) answer=no;;
			*) answer=$resp;;
		esac
	done

	if yesno "$answer"; then
		case "$EDITOR" in
			nano)	apk add nano;;
			vim)	apk add vim;;
		esac
		${EDITOR:-vi} interfaces
	fi
		
	mkdir -p $ROOT/etc/network
	cp interfaces $ROOT/etc/network/
}

ask_bridge=
is_xen_dom0 && ask_bridge=1

while getopts "bhip:" opt; do
        case $opt in
		b) ask_bridge=1;;
                h) usage;;
		i) STDINPUT=1;;
		p) ROOT=$OPTARG;;
        esac
done

mkdir -p $ROOT/etc/network
if [ "$STDINPUT" = "1" ]; then
	cat > $ROOT/etc/network/interfaces
else
	prompt_for_interfaces
fi