From 9de2034832586afc0eef0fa82032d9d74620e9b8 Mon Sep 17 00:00:00 2001 From: Sheng Yang Date: Wed, 10 Jul 2013 15:53:31 -0700 Subject: [PATCH] CLOUDSTACK-3436: Fix inconsistent ip routing table between redundant virtual routers --- .../debian/config/opt/cloud/bin/ipassoc.sh | 8 +++---- patches/systemvm/debian/config/root/func.sh | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/patches/systemvm/debian/config/opt/cloud/bin/ipassoc.sh b/patches/systemvm/debian/config/opt/cloud/bin/ipassoc.sh index d23ec00de5e..9efae26877e 100755 --- a/patches/systemvm/debian/config/opt/cloud/bin/ipassoc.sh +++ b/patches/systemvm/debian/config/opt/cloud/bin/ipassoc.sh @@ -106,8 +106,8 @@ remove_routing() { local tableNo=$(echo $ethDev | awk -F'eth' '{print $2}') local tableName="Table_$ethDev" - local ethMask=$(ip route list scope link dev $ethDev | awk '{print $1}') - if [ "$ethMask" == "" ] + local remainip=`ip addr show $ethDev | grep "inet "` + if [ "$remainip" == "" ] then # rules and routes will be deleted for the last ip of the interface. sudo ip rule delete fwmark $tableNo table $tableName @@ -125,7 +125,7 @@ copy_routes_from_main() { #get the network masks from the main table local eth0Mask=$(ip route list scope link dev eth0 | awk '{print $1}') local eth1Mask=$(ip route list scope link dev eth1 | awk '{print $1}') - local ethMask=$(ip route list scope link dev $ethDev | awk '{print $1}') + local ethMask=$(getcidr $ethDev) # eth0,eth1 and other know routes will be skipped, so as main routing table will decide the route. This will be useful if the interface is down and up. sudo ip route add throw $eth0Mask table $tableName proto static @@ -164,7 +164,7 @@ add_routing() { sudo ip route add default via $defaultGwIP table $tableName proto static sudo ip route flush cache - local ethMask=$(ip route list scope link dev $ethDev | awk '{print $1}') + local ethMask=$(getcidr $ethDev) local rulePresent=$(ip rule show | grep $ethMask) if [ "$rulePresent" == "" ] then diff --git a/patches/systemvm/debian/config/root/func.sh b/patches/systemvm/debian/config/root/func.sh index 8cc96082cc2..9c7c494aa5a 100644 --- a/patches/systemvm/debian/config/root/func.sh +++ b/patches/systemvm/debian/config/root/func.sh @@ -95,3 +95,26 @@ unlock_exit() { exit $1 } +# calcuate the ip & network mask +rangecalc(){ + local IFS='.' + local -a oct mask ip + + read -ra oct <<<"$1" + read -ra mask <<<"$2" + for i in {0..3} + do + ip+=( "$(( oct[i] & mask[i] ))" ) + done + echo "${ip[*]}" +} + +#get cidr of the nic +getcidr(){ + local dev=$1 + local mask=`ifconfig $dev|grep "Mask"|cut -d ":" -f 4` + local cidrsize=`ip addr show $dev|grep inet|head -n 1|awk '{print $2}'|cut -d '/' -f 2` + local ipaddr=`ip addr show $dev|grep inet|head -n 1|awk '{print $2}'|cut -d '/' -f 1` + local base=$(rangecalc $ipaddr $mask) + echo $base/$cidrsize +}