Commit 5ba66334 authored by Natanael Copa's avatar Natanael Copa
Browse files

setup-interfaces: support creating bridge when asking for ip addr

parent 0ca8a4bd
...@@ -9,11 +9,15 @@ done ...@@ -9,11 +9,15 @@ done
bridges="" bridges=""
unconfigured_add() {
touch $1.noconf
}
unconfigured_detect() { unconfigured_detect() {
local i= local i=
for i in ${INTERFACES:-$(available_ifaces)}; do for i in ${INTERFACES:-$(available_ifaces)}; do
if [ "$i" != "lo" ]; then if [ "$i" != "lo" ]; then
touch $i.noconf unconfigured_add $i
fi fi
done done
} }
...@@ -47,11 +51,15 @@ unconfigured_isin() { ...@@ -47,11 +51,15 @@ unconfigured_isin() {
[ -f $1.noconf ] [ -f $1.noconf ]
} }
iface_exists() {
test -e /sys/class/net/$1
}
get_default_addr() { get_default_addr() {
# check if dhcpcd is running # check if dhcpcd is running
if pidof dhcpcd > /dev/null && [ -f "$ROOT/var/lib/dhcpc/dhcpcd-$1.info" ]; then if pidof dhcpcd > /dev/null && [ -f "$ROOT/var/lib/dhcpc/dhcpcd-$1.info" ]; then
echo dhcp echo dhcp
else elif iface_exists; then
ip addr show $1 | awk '/inet / {print $2}' | head -n 1 | sed 's:/.*::' ip addr show $1 | awk '/inet / {print $2}' | head -n 1 | sed 's:/.*::'
fi fi
} }
...@@ -68,13 +76,24 @@ get_default_gateway() { ...@@ -68,13 +76,24 @@ get_default_gateway() {
ip route show dev $1 | awk '/^default/ {print $3}' 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__
}
config_iface() { config_iface() {
local iface=$1 local iface=$1
local prefix=$2 local prefix=$2
local address local address= netmask= gateway= bridge_ports=
local netmask
local gateway
local bridge local bridge
local conf=$prefix$iface.conf local conf=$prefix$iface.conf
local answer= local answer=
...@@ -100,21 +119,36 @@ config_iface() { ...@@ -100,21 +119,36 @@ config_iface() {
bridges="$bridges $bridge" bridges="$bridges $bridge"
fi 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 # use ipcalc to validate the address. we do accept /mask
# we are no interested in the result, only error code, so # we are no interested in the result, only error code, so
# we send result to /dev/null # we send result to /dev/null
while ! ipcalc -s -m $address >/dev/null 2>&1; do while ! ipcalc -s -m $address >/dev/null 2>&1; do
address=`get_default_addr $iface` address=`get_default_addr $iface`
[ -z "$address" ] && address="dhcp" [ -z "$address" ] && address="dhcp"
ask "Ip address for $iface? (or 'dhcp')" $address ask "Ip address for $iface? (or 'dhcp' or '?')" $address
address=$resp address=$resp
[ "$address" = "abort" ] && return case "$resp" in
if [ "$address" = "dhcp" ] ; then '?') ipaddr_help;;
"abort") return;;
"dhcp")
HAS_DHCP=yes HAS_DHCP=yes
echo "type=dhcp" >> $conf echo "type=dhcp" >> $conf
unconfigured_del $iface unconfigured_del $iface
return return ;;
fi br[0-9]*|bridge[0-9]*)
case "$iface" in
# we dont allow bridge bridges
br[0-9]*|bridge[0-9]*) continue;;
esac
echo "$iface" >> $resp.bridge_ports
unconfigured_add $resp
unconfigured_del $iface
return ;;
esac
done done
# extract netmask if entered together with address # extract netmask if entered together with address
...@@ -144,10 +178,14 @@ config_iface() { ...@@ -144,10 +178,14 @@ config_iface() {
done done
echo "type=static" >> $conf 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 "address=${address%%/*}" >> $conf #strip off /mask if there
echo "netmask=$netmask" >> $conf echo "netmask=$netmask" >> $conf
echo "gateway=$gateway" >> $conf echo "gateway=$gateway" >> $conf
# print summary # print summary
echo "Configuration for $iface:" echo "Configuration for $iface:"
sed 's/^/ /' $conf sed 's/^/ /' $conf
...@@ -196,6 +234,10 @@ prompt_for_interfaces() { ...@@ -196,6 +234,10 @@ prompt_for_interfaces() {
for i in *.conf ; do for i in *.conf ; do
iface=`basename $i .conf` iface=`basename $i .conf`
iface=${iface#[0-9]*~} iface=${iface#[0-9]*~}
bridge_ports=
address=
type=
gateway=
. ./$i . ./$i
if [ -n "$bridge" ]; then if [ -n "$bridge" ]; then
echo "auto $iface $bridge" >> interfaces echo "auto $iface $bridge" >> interfaces
...@@ -211,6 +253,9 @@ prompt_for_interfaces() { ...@@ -211,6 +253,9 @@ prompt_for_interfaces() {
echo "auto $iface" >> interfaces echo "auto $iface" >> interfaces
echo "iface $iface inet $type" >> interfaces echo "iface $iface inet $type" >> interfaces
fi fi
if [ -n "$bridge_ports" ]; then
echo -e "\tbridge-ports $bridge_ports" >> interfaces
fi
case $type in case $type in
dhcp) dhcp)
[ -n "$hostname" ] \ [ -n "$hostname" ] \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment