From: B Thomas Subject: make network-bridge work in more environments Date: Thu, 29 Dec 2005 10:58:04 -0500 Message-ID: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============1720580068==" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Mime-version: 1.0 Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org --===============1720580068== Content-Type: multipart/alternative; boundary="----=_Part_43580_7217740.1135871884807" ------=_Part_43580_7217740.1135871884807 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline # Made network-bridge script work on ALL systems missing ifup/down. # This is done by defining ifup/down both to 'false' when they're # missing; then instead of attempting to parse the IP params from # the kernel command line, we get them from 'ip' instead. So this # works even with (for example) ip=3Ddhcp on the kernel command line. # # Also made a few minor syntax changes (in particular, replaced "=3D=3D" # with "=3D", and ">&foo" with ">foo 2>foo") so this works with more # limited shells. # # Signed-off-by: Ben Thomas # diff -Nru examples.orig/network-bridge examples/network-bridge --- examples.orig/network-bridge 2005-12-15 10:48:47.000000000 -0500 +++ examples/network-bridge 2005-12-15 10:51:20.000000000 -0500 @@ -68,48 +68,19 @@ vdev=3D"veth${vifnum}" vif0=3D"vif0.${vifnum}" -legacy_mask_to_prefix() { - mask=3D$1 - first=3D${mask%%.*} - second=3D${mask#*.} - third=3D${second#*.} - fourth=3D${third#*.} - second=3D${second%%.*} - third=3D${third%%.*} - declare -i INT FULLMASK BIT - INT=3D$((((($first*256)+$second)*256+$third)*256+$fourth)) - FULLMASK=3D4294967295 - BIT=3D1 - for bit in `seq 32 -1 0`; do - if test $FULLMASK -eq $INT; then PREFIX=3D$bit; return; fi - FULLMASK=3D$(($FULLMASK-$BIT)) - BIT=3D$((BIT*2)) - done - echo "ERROR converting netmask $mask to prefix" - exit 1 -} - -parse_kernel_ip() { - if egrep 'ip=3D[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:' /proc/cmdline; then - kip=3D`sed -e 's!.*ip=3D\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\):.*!\1!' /proc/cmdline` - kmask=3D`sed -e 's!.*ip=3D[^:]*:[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline` - kgate=3D`sed -e 's!.*ip=3D[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline` - fi +get_ip_info() { + addr_pfx=3D`ip addr show dev $1 | egrep '^ *inet' | sed -e 's/ *inet /= /' -e 's/ .*//'` + gateway=3D`ip route show dev $1 | fgrep default | sed 's/default via /= /'` } - + do_ifup() { if ! ifup $1 ; then - if [ ${kip} ] ; then - # use the addresses we grocked from /proc/cmdline - if [ -z "${kmask}" ]; then - PREFIX=3D32 - else - legacy_mask_to_prefix ${kmask} - fi + if [ ${addr_pfx} ] ; then + # use the info from get_ip_info() ip addr flush $1 - ip addr add ${kip}/${PREFIX} dev $1 + ip addr add ${addr_pfx} dev $1 ip link set dev $1 up - [ ${kgate} ] && ip route add default via ${kgate} + [ ${gateway} ] && ip route add default via ${gateway} fi fi } @@ -171,7 +142,7 @@ # link_exists() { - if ip link show "$1" >&/dev/null + if ip link show "$1" >/dev/null 2>/dev/null then return 0 else @@ -231,7 +202,7 @@ } op_start () { - if [ "${bridge}" =3D=3D "null" ] ; then + if [ "${bridge}" =3D "null" ] ; then return fi @@ -259,9 +230,8 @@ preiftransfer ${netdev} transfer_addrs ${netdev} ${vdev} if ! ifdown ${netdev}; then - # If ifdown fails, take the IP details from the kernel command - # line. - parse_kernel_ip + # If ifdown fails, remember the IP details. + get_ip_info ${netdev} ip link set ${netdev} down ip addr flush ${netdev} fi @@ -283,13 +253,13 @@ transfer_routes ${netdev} ${bridge} fi - if [ ${antispoof} =3D=3D 'yes' ] ; then + if [ ${antispoof} =3D 'yes' ] ; then antispoofing fi } op_stop () { - if [ "${bridge}" =3D=3D "null" ]; then + if [ "${bridge}" =3D "null" ]; then return fi if ! link_exists "$bridge"; then @@ -301,7 +271,7 @@ mac=3D`ip link show ${netdev} | grep 'link\/ether' | sed -e 's/.*ether \(..:..:..:..:..:..\).*/\1/'` transfer_addrs ${netdev} ${pdev} if ! ifdown ${netdev}; then - parse_kernel_ip + get_ip_info ${netdev} fi ip link set ${netdev} down arp off ip link set ${netdev} addr fe:ff:ff:ff:ff:ff diff -Nru examples.orig/xen-network-common.sh examples/xen-network-common.s= h --- examples.orig/xen-network-common.sh 2005-12-15 10:48:47.000000000-05= 00 +++ examples/xen-network-common.sh 2005-12-15 10:52:38.000000000 -0500 @@ -42,7 +42,7 @@ { /sbin/ifup ${HWD_CONFIG_0} $1 } -elif ! which ifup >&/dev/null +elif ! which ifup >/dev/null 2>/dev/null then if [ -e /etc/conf.d/net ] then @@ -59,9 +59,18 @@ /etc/init.d/net.$1 stop } else - logger -p "daemon.crit" -- \ - "You don't have ifup and don't seem to be running Gentoo either!" - exit 1 + preiftransfer() + { + true + } + ifup() + { + false + } + ifdown() + { + false + } fi else preiftransfer() ------=_Part_43580_7217740.1135871884807 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline # Made network-bridge script work on ALL systems missing ifup/down.
# Th= is is done by defining ifup/down both to 'false' when they're
# missing;= then instead of attempting to parse the IP params from
# the kernel com= mand line, we get them from 'ip' instead.  So this
# works even with (for example) ip=3Ddhcp on the kernel command line.#
# Also made a few minor syntax changes (in particular, replaced &quo= t;=3D=3D"
# with "=3D", and ">&foo" with= ">foo 2>foo") so this works with more
# limited shells.
#
# Signed-off-by: Ben Thomas <bjthomas3@gmail.com>
#
diff -Nru exa= mples.orig/network-bridge examples/network-bridge
--- examples.orig/netw= ork-bridge    2005-12-15 10:48: 47.000000000 -0500
+++ examples/network-bridge    2005-12= -15 10:51:20.000000000 -0500
@@ -68,48 +68,19 @@
 vdev=3D"v= eth${vifnum}"
 vif0=3D"vif0.${vifnum}"
 
= -legacy_mask_to_prefix() {
-    mask=3D$1
-    first=3D${mask%%.*}-    second=3D${mask#*.}
-    third=3D${s= econd#*.}
-    fourth=3D${third#*.}
-   = ; second=3D${second%%.*}
-    third=3D${third%%.*}
-&n= bsp;   declare -i INT FULLMASK BIT
-    INT=3D$= ((((($first*256)+$second)*256+$third)*256+$fourth))
-    FULLMASK=3D4294967295
-    BIT=3D= 1
-    for bit in `seq 32 -1 0`; do
-   &nbs= p;if test $FULLMASK -eq $INT; then PREFIX=3D$bit; return; fi
- &nbs= p;  FULLMASK=3D$(($FULLMASK-$BIT))
-    BIT=3D$((BIT= *2))
-    done
-    echo "ERROR converting netmask $mask to prefix&quo= t;
-    exit 1
-}
-
-parse_kernel_ip() {
-&nb= sp;   if egrep 'ip=3D[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:' /proc/cmdli= ne; then
-    kip=3D`sed -e 's!.*ip=3D\([0-9]\+\.[0-9]\+\= .[0-9]\+\.[0-9]\+\):.*!\1!' /proc/cmdline`
-    kmask=3D`sed -e 's!.*ip=3D[^:]*:[^:]*:[^:]*:\([^:]*= \):.*!\1!' /proc/cmdline`
-    kgate=3D`sed -e 's!.*ip= =3D[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline`
-    fi+get_ip_info() {
+    addr_pfx=3D`ip addr show dev $1 |= egrep '^ *inet' | sed -e 's/ *inet //' -e 's/ .*//'`
+    gateway=3D`ip route show dev $1 | fgrep default | s= ed 's/default via //'`
 }
-
+    
 do_= ifup() {
     if ! ifup $1 ; then
-  &n= bsp;     if [ ${kip} ] ; then
-   &nb= sp;        # use the addresses we grocke= d from /proc/cmdline
-            if = [ -z "${kmask}" ]; then
-      =           PREFIX=3D32
-&nbs= p;           else
-&n= bsp;            = ;   legacy_mask_to_prefix ${kmask}
-    &n= bsp;       fi
+    &nb= sp;   if [ ${addr_pfx} ] ; then
+     = ;       # use the info from get_ip_info()
            = ; ip addr flush $1
-        &nbs= p;   ip addr add ${kip}/${PREFIX} dev $1
+   &n= bsp;        ip addr add ${addr_pfx} dev = $1
           &nb= sp; ip link set dev $1 up
-       &nb= sp;    [ ${kgate} ] && ip route add default via ${kg= ate}
+            [ $= {gateway} ] && ip route add default via ${gateway}
  &= nbsp;      fi
     fi
&n= bsp;}
@@ -171,7 +142,7 @@
 #
 link_exists()
 {-    if ip link show "$1" >&/dev/null
+    if ip link show "$1" >/dev/null 2>/= dev/null
     then
     =     return 0
     else
@@ -231,7 += 202,7 @@
 }
 
 op_start () {
-   = if [ "${bridge}" =3D=3D "null" ] ; then
+    if [ "${bridge}" =3D "null" ] ;= then
     return
     fi
=  
@@ -259,9 +230,8 @@
     preiftransfer ${n= etdev}
     transfer_addrs ${netdev} ${vdev}
&nbs= p;    if ! ifdown ${netdev}; then
-        # If ifdown fails, take the IP d= etails from the kernel command
-        # = line.
-        parse_kernel_ip
+ &= nbsp;      # If ifdown fails, remember the IP details.<= br>+        get_ip_info ${netdev}
 &n= bsp;       ip link set ${netdev} down
         ip addr flush ${netdev}
=      fi
@@ -283,13 +253,13 @@
    =  transfer_routes ${netdev} ${bridge}
     fi 
-    if [ ${antispoof} =3D=3D 'yes' ] ; then
+=     if [ ${antispoof} =3D 'yes' ] ; then
     antispoofing
     fi
&nb= sp;}
 
 op_stop () {
-    if [ "${br= idge}" =3D=3D "null" ]; then
+    if [ &qu= ot;${bridge}" =3D "null" ]; then
     = ;return
     fi
     if ! lin= k_exists "$bridge"; then
@@ -301,7 +271,7 @@
     mac=3D`ip link show ${n= etdev} | grep 'link\/ether' | sed -e 's/.*ether \(..:..:..:..:..:..\).*/\1/= '`
     transfer_addrs ${netdev} ${pdev}
 &n= bsp;   if ! ifdown ${netdev}; then
-     &= nbsp;  parse_kernel_ip
+        get_ip_info ${netdev}
 &= nbsp;   fi
     ip link set ${netdev} down= arp off
     ip link set ${netdev} addr fe:ff:ff:ff= :ff:ff
diff -Nru examples.orig/xen-network-common.sh examples/xen-networ= k-common.sh
--- examples.orig/xen-network-common.sh    2005-12-15 10= :48:47.000000000 -0500
+++ examples/xen-network-common.sh   &n= bsp;2005-12-15 10:52:38.000000000 -0500
@@ -42,7 +42,7 @@
  = ; {
     /sbin/ifup ${HWD_CONFIG_0} $1
   }
-elif ! which ifup >&/dev/null
+elif ! whic= h ifup >/dev/null 2>/dev/null
 then
   if [ -e /= etc/conf.d/net ]
   then
@@ -59,9 +59,18 @@
  =      /etc/init.d/net.$1 stop
    = ; }
   else
-    logger -p "daemon.crit" = -- \
-      "You don't have ifup and don't= seem to be running Gentoo either!"
-    exit 1
+=     preiftransfer()
+    {
+  = ;    true
+    }
+    if= up()
+    {
+      false
+&nbs= p;   }
+    ifdown()
+    {+      false
+    }
 &= nbsp; fi
 else
   preiftransfer()




<= br>

------=_Part_43580_7217740.1135871884807-- --===============1720580068== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --===============1720580068==--