setup-interfaces.in 4.36 KB
Newer Older
Natanael Copa's avatar
Natanael Copa committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/bin/sh

PROGRAM=setup-interfaces
PREFIX=

. $PREFIX/lib/libalpine.sh


detect_interfaces() {
	ip addr | grep -v ' lo:' | awk -F : '/^[0-9]*:/ { print $2}'
}

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

get_default_addr() {
	# check if dhcpcd is running
	if pidof dhcpcd > /dev/null && [ -f "$ROOT/var/lib/dhcpc/dhcpcd-$1.info" ]; then
		echo dhcp
	else
		ip addr show $1 | awk '/inet / {print $2}' | head -n 1 | sed 's:/.*::'
23
	fi
Natanael Copa's avatar
Natanael Copa committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
}

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}'
}


config_iface() {
	local iface=$1
41
	local prefix=$2
Natanael Copa's avatar
Natanael Copa committed
42 43 44
	local address
	local netmask
	local gateway
45
	local conf=$prefix$iface.conf
46 47 48 49 50

	# 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
Natanael Copa's avatar
Natanael Copa committed
51
		address=`get_default_addr $iface`
52
		[ -z "$address" ] && address="dhcp"
Natanael Copa's avatar
Natanael Copa committed
53 54 55 56 57
		echon "Ip address for $iface? (or 'dhcp') [$address] "
		default_read address $address
		[ "$address" = "abort" ] && return
		if [ "$address" = "dhcp" ] ; then
			HAS_DHCP=yes
58
			echo "type=dhcp" > $conf
Natanael Copa's avatar
Natanael Copa committed
59 60 61 62 63
			rm $iface.noconf
			return
		fi
	done

64 65 66 67 68 69 70 71
	# 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
Natanael Copa's avatar
Natanael Copa committed
72 73 74 75 76 77
		netmask=`get_default_mask $address`
		echon "Netmask? [$netmask] "
		default_read netmask "$netmask"
		[ "$netmask" = "abort" ] && return
	done

78 79 80
	# 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
Natanael Copa's avatar
Natanael Copa committed
81 82 83 84 85 86 87 88 89
		gateway=`get_default_gateway $iface`
		[ -z "$gateway" ] && gateway=none
		echon "Gateway? (or 'none') [$gateway] "
		default_read gateway $gateway
		[ "$gateway" = "abort" ] && return
		[ "$gateway" = "none" ] && gateway=""
		[ -z "$gateway" ] && break
	done
	
90 91 92 93
	echo "type=static" > $conf
	echo "address=${address%%/*}" >> $conf  #strip off /mask if there
	echo "netmask=$netmask" >> $conf
	echo "gateway=$gateway" >> $conf
94 95 96

	# print summary
	echo "Configuration for $iface:"
97
	sed 's/^/  /' $conf
Natanael Copa's avatar
Natanael Copa committed
98 99 100 101
	
	rm $iface.noconf
}

102 103 104
usage() {
        cat <<__EOF__
usage: setup-interfaces [-h] [-i read custom /etc/network/interfaces from stdin]
Natanael Copa's avatar
Natanael Copa committed
105

106
Setup network interfaces
Natanael Copa's avatar
Natanael Copa committed
107

108 109 110 111 112 113
options:
 -h  Show this help
 -i  Read new contents of /etc/network/interfaces from stdin
__EOF__
        exit 1
}
Natanael Copa's avatar
Natanael Copa committed
114

115 116 117 118 119
while getopts "hi" opt; do
        case $opt in
                h) usage;;
		i) STDINPUT=1;;
        esac
Natanael Copa's avatar
Natanael Copa committed
120 121
done

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
if [ "$STDINPUT" != "1" ]; then
	init_tmpdir TMP
	
	cd $TMP
	for i in $(detect_interfaces); do
		touch $i.noconf
	done
	
	index=1
	while ls *.noconf > /dev/null 2>&1 ; do
		echon "Available interfaces are:"
		for i in *.noconf; do
			echon " `basename $i .noconf`"
		done
		echo "."
	
		firstif=`get_first_unconfigured`
		echon "Which one do you want to initialize? (or 'done') [$firstif] "
		default_read iface "$firstif"
	
		[ "$iface" = "done" ] && break
		[ -f $iface.noconf ] || continue
		config_iface $iface $(printf "%.3d~" $index)
		index=$(( $index + 1 ))
	done
	
	echo "type=loopback" > 000~lo.conf
	echo "" > interface
	hostname=$(cat /etc/hostname 2>/dev/null)
	
	for i in *.conf ; do
		iface=`basename $i .conf`
		iface=${iface#[0-9]*~}
		. ./$i
		echo "auto $iface" >> interfaces
		echo "iface $iface inet $type" >> interfaces
		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
		echo "" >> interfaces
	done
	
	while [ "$answer" != "yes" ] && [ "$answer" != "no" ] ; do
		echon "Do you want to do any manual network configuration? [no] "
		default_read answer no
	done
Natanael Copa's avatar
Natanael Copa committed
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191
	if [ "$answer" = "yes" ]; then
		case "$EDITOR" in
			nano)	pkg_inst nano;;
			vim)	pkg_inst vim;;
		esac
		${EDITOR:-vi} interfaces
	fi
		
	mkdir -p $ROOT/etc/network
	cp interfaces $ROOT/etc/network/
else
	cat /dev/stdin > interfaces
	mkdir -p $ROOT/etc/network
	cp interfaces $ROOT/etc/network
Natanael Copa's avatar
Natanael Copa committed
192
fi