#!/bin/sh PROGRAM=setup-interfaces PREFIX= for i in ./libalpine.sh $PREFIX/lib/libalpine.sh; do [ -e $i ] && . $i && break done bridges="" unconfigured_detect() { local i= for i in ${INTERFACES:-$(available_ifaces)}; do if [ "$i" != "lo" ]; then touch $i.noconf 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 ] } 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:/.*::' 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}' } config_iface() { local iface=$1 local prefix=$2 local address local netmask local gateway local bridge local conf=$prefix$iface.conf local answer= while [ -n "$ask_bridge" ]; do ask "Do you want to bridge the interface $iface?" no answer=$resp case "$answer" in yes|y) answer=yes; break;; no|n) break;; esac done if [ "$answer" = "yes" ]; then bridge="br"`echo $iface | sed 's/[^0-9]//g'` while [ 1 ]; do ask "Name of the bridge you would like to create:" $bridge bridge=$resp `echo "$bridges" | grep -q "$bridge"` || break echo "Name already in use, please choose another one" done echo "bridge=${bridge}" > $conf bridges="$bridges $bridge" 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')" $address address=$resp [ "$address" = "abort" ] && return if [ "$address" = "dhcp" ] ; then HAS_DHCP=yes echo "type=dhcp" >> $conf unconfigured_del $iface return fi 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 /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 /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 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]*~} . ./$i if [ -n "$bridge" ]; then echo "auto $iface $bridge" >> interfaces echo "" >> interfaces echo "iface $iface inet manual" >> interfaces echo -e "\tup ip link set \$IFACE up" >> interfaces echo -e "\tdown ip link set \$IFACE down" >> interfaces echo "" >> interfaces echo "iface $bridge inet $type" >> interfaces echo -e "\tpre-up brctl addbr $bridge" >> interfaces echo -e "\tpre-up brctl addif $bridge $iface" >> interfaces else echo "auto $iface" >> interfaces echo "iface $iface inet $type" >> 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