Advertisement
Guest User

Untitled

a guest
Apr 20th, 2011
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 15.59 KB | None | 0 0
  1. #!/bin/bash
  2. #============================================================================
  3. # Default Xen network start/stop script.
  4. # Xend calls a network script when it starts.
  5. # The script name to use is defined in /etc/xen/xend-config.sxp
  6. # in the network-script field.
  7. #
  8. # This script creates a bridge (default ${netdev}), adds a device
  9. # (defaults to the device on the default gateway route) to it, copies
  10. # the IP addresses from the device to the bridge and adjusts the routes
  11. # accordingly.
  12. #
  13. # If all goes well, this should ensure that networking stays up.
  14. # However, some configurations are upset by this, especially
  15. # NFS roots. If the bridged setup does not meet your needs,
  16. # configure a different script, for example using routing instead.
  17. #
  18. # Usage:
  19. #
  20. # network-bridge (start|stop|status) {VAR=VAL}*
  21. #
  22. # Vars:
  23. #
  24. # bridge     The bridge to use (default ${netdev}).
  25. # netdev     The interface to add to the bridge (default gateway device).
  26. # antispoof  Whether to use iptables to prevent spoofing (default no).
  27. #
  28. # Internal Vars:
  29. # pdev="p${netdev}"
  30. # tdev=tmpbridge
  31. #
  32. # start:
  33. # Creates the bridge as tdev
  34. # Copies the IP and MAC addresses from pdev to bridge
  35. # Renames netdev to be pdev
  36. # Renames tdev to bridge
  37. # Enslaves pdev to bridge
  38. #
  39. # stop:
  40. # Removes pdev from the bridge
  41. # Transfers addresses, routes from bridge to pdev
  42. # Renames bridge to tdev
  43. # Renames pdev to netdev
  44. # Deletes tdev
  45. #
  46. # status:
  47. # Print addresses, interfaces, routes
  48. #
  49. #============================================================================
  50.  
  51.  
  52. dir=$(dirname "$0")
  53. dirname "$0"
  54. ++ dirname ./network-bridge
  55. + dir=.
  56. . "$dir/xen-script-common.sh"
  57. + . ./xen-script-common.sh
  58. #
  59. # Copyright (c) 2005 XenSource Ltd.
  60. #
  61. # This library is free software; you can redistribute it and/or
  62. # modify it under the terms of version 2.1 of the GNU Lesser General Public
  63. # License as published by the Free Software Foundation.
  64. #
  65. # This library is distributed in the hope that it will be useful,
  66. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  67. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  68. # Lesser General Public License for more details.
  69. #
  70. # You should have received a copy of the GNU Lesser General Public
  71. # License along with this library; if not, write to the Free Software
  72. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  73. #
  74.  
  75.  
  76. set -e
  77. ++ set -e
  78.  
  79.  
  80. evalVariables()
  81. {
  82.   for arg in "$@"
  83.   do
  84.     if expr 'index' "$arg" '=' '>' '1' >/dev/null
  85.     then
  86.       eval "$arg"
  87.     fi
  88.   done
  89. }
  90.  
  91.  
  92. findCommand()
  93. {
  94.   for arg in "$@"
  95.   do
  96.     if ! expr 'index' "$arg" '=' >/dev/null
  97.     then
  98.       command="$arg"
  99.       return
  100.     fi
  101.   done
  102. }
  103. . "$dir/xen-network-common.sh"
  104. + . ./xen-network-common.sh
  105. #
  106. # Copyright (c) 2005 XenSource Ltd.
  107. #
  108. # This library is free software; you can redistribute it and/or
  109. # modify it under the terms of version 2.1 of the GNU Lesser General Public
  110. # License as published by the Free Software Foundation.
  111. #
  112. # This library is distributed in the hope that it will be useful,
  113. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  114. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  115. # Lesser General Public License for more details.
  116. #
  117. # You should have received a copy of the GNU Lesser General Public
  118. # License along with this library; if not, write to the Free Software
  119. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  120. #
  121.  
  122.  
  123. # Gentoo doesn't have ifup/ifdown, so we define appropriate alternatives.
  124.  
  125. # Other platforms just use ifup / ifdown directly.
  126.  
  127. ##
  128. # preiftransfer
  129. #
  130. # @param $1 The current name for the physical device, which is also the name
  131. #           that the virtual device will take once the physical device has
  132. #           been renamed.
  133.  
  134. if ! which ifup >/dev/null 2>/dev/null
  135. then
  136.   preiftransfer()
  137.   {
  138.     true
  139.   }
  140.   ifup()
  141.   {
  142.     false
  143.   }
  144.   ifdown()
  145.   {
  146.     false
  147.   }
  148. else
  149.   preiftransfer()
  150.   {
  151.     true
  152.   }
  153. fi
  154. ++ which ifup
  155.  
  156.  
  157. first_file()
  158. {
  159.   t="$1"
  160.   shift
  161.   for file in $@
  162.   do
  163.     if [ "$t" "$file" ]
  164.     then
  165.       echo "$file"
  166.       return
  167.     fi
  168.   done
  169. }
  170.  
  171. find_dhcpd_conf_file()
  172. {
  173.   first_file -f /etc/dhcp3/dhcpd.conf /etc/dhcpd.conf
  174. }
  175.  
  176.  
  177. find_dhcpd_init_file()
  178. {
  179.   first_file -x /etc/init.d/{dhcp3-server,dhcp,dhcpd}
  180. }
  181.  
  182. find_dhcpd_arg_file()
  183. {
  184.   first_file -f /etc/sysconfig/dhcpd /etc/defaults/dhcp /etc/default/dhcp3-server
  185. }
  186.  
  187. # configure interfaces which act as pure bridge ports:
  188. setup_bridge_port() {
  189.     local dev="$1"
  190.  
  191.     # take interface down ...
  192.     ip link set ${dev} down
  193.  
  194.     # ... and configure it
  195.     ip addr flush ${dev}
  196. }
  197.  
  198. # Usage: create_bridge bridge
  199. create_bridge () {
  200.     local bridge=$1
  201.  
  202.     # Don't create the bridge if it already exists.
  203.     if [ ! -e "/sys/class/net/${bridge}/bridge" ]; then
  204.         brctl addbr ${bridge}
  205.         brctl stp ${bridge} off
  206.         brctl setfd ${bridge} 0
  207.         # Setting these to zero stops guest<->LAN traffic
  208.         # traversing the bridge from hitting the *tables
  209.         # rulesets. guest<->host traffic still gets processed
  210.         # by the host's iptables rules so this isn't a hole
  211.         sysctl -q -w "net.bridge.bridge-nf-call-arptables=0"
  212.         sysctl -q -w "net.bridge.bridge-nf-call-ip6tables=0"
  213.         sysctl -q -w "net.bridge.bridge-nf-call-iptables=0"
  214.     fi
  215. }
  216.  
  217. # Usage: add_to_bridge bridge dev
  218. add_to_bridge () {
  219.     local bridge=$1
  220.     local dev=$2
  221.  
  222.     # Don't add $dev to $bridge if it's already on a bridge.
  223.     if [ -e "/sys/class/net/${bridge}/brif/${dev}" ]; then
  224.         ip link set ${dev} up || true
  225.         return
  226.     fi
  227.     brctl addif ${bridge} ${dev}
  228.     ip link set ${dev} up
  229. }
  230.  
  231.  
  232. findCommand "$@"
  233. + findCommand start
  234. + for arg in '"$@"'
  235. + expr index start =
  236. + command=start
  237. + return
  238. evalVariables "$@"
  239. + evalVariables start
  240. + for arg in '"$@"'
  241. + expr index start = '>' 1
  242.  
  243. is_network_root () {
  244.     local rootfs=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $3; }}' /etc/mtab)
  245.     local rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' /etc/mtab)
  246.  
  247.     [[ "$rootfs" =~ "^nfs" ]] || [[ "$rootopts" =~ "_netdev" ]] && has_nfsroot=1 || has_nfsroot=0
  248.     if [ $has_nfsroot -eq 1 ]; then
  249.         local bparms=$(cat /proc/cmdline)
  250.         for p in $bparms; do
  251.             local ipaddr=$(echo $p | awk /nfsroot=/'{ print substr($1,9,index($1,":")-9) }')
  252.             if [ "$ipaddr" != "" ]; then
  253.                 local nfsdev=$(ip route get $ipaddr | awk /$ipaddr/'{ print $3 }')
  254.                 [[ "$nfsdev" == "$netdev" ]] && return 0 || return 1
  255.             fi
  256.         done
  257.     fi
  258.     return 1
  259. }
  260.  
  261. find_alt_device () {
  262.     local interf=$1
  263.     local prefix=${interf%[[:digit:]]}
  264.     local ifs=$(ip link show | grep " $prefix" |\
  265.                 gawk '{ printf ("%s",substr($2,1,length($2)-1)) }' |\
  266.                 sed s/$interf//)
  267.     echo "$ifs"
  268. }
  269.  
  270. netdev=${netdev:-$(ip route list 0.0.0.0/0  | \
  271.                    sed 's/.*dev \([a-z]\+[0-9]\+\).*$/\1/')}
  272. ip route list 0.0.0.0/0  |                    sed 's/.*dev \([a-z]\+[0-9]\+\).*$/\1/'
  273. ++ ip route list 0.0.0.0/0
  274. ++ sed 's/.*dev \([a-z]\+[0-9]\+\).*$/\1/'
  275. + netdev=eth0
  276. if is_network_root ; then
  277.     altdevs=$(find_alt_device $netdev)
  278.     for netdev in $altdevs; do break; done
  279.     if [ -z "$netdev" ]; then
  280.         [ -x /usr/bin/logger ] && /usr/bin/logger "network-bridge: bridging not supported on network root; not starting"
  281.         exit
  282.     fi
  283. fi
  284. + is_network_root
  285. awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $3; }}' /etc/mtab
  286. ++ awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $3; }}' /etc/mtab
  287. + local rootfs=ext3
  288. awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' /etc/mtab
  289. ++ awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' /etc/mtab
  290. + local rootopts=rw
  291. + [[ ext3 =~ ^nfs ]]
  292. + [[ rw =~ _netdev ]]
  293. + has_nfsroot=0
  294. + '[' 0 -eq 1 ']'
  295. + return 1
  296. netdev=${netdev:-eth0}
  297. + netdev=eth0
  298. bridge=${bridge:-${netdev}}
  299. + bridge=eth0
  300. antispoof=${antispoof:-no}
  301. + antispoof=no
  302.  
  303. pdev="p${netdev}"
  304. + pdev=peth0
  305. tdev=tmpbridge
  306. + tdev=tmpbridge
  307.  
  308. get_ip_info() {
  309.     addr_pfx=`ip addr show dev $1 | egrep '^ *inet' | sed -e 's/ *inet //' -e 's/ .*//'`
  310.     gateway=`ip route show dev $1 | fgrep default | sed 's/default via //'`
  311. }
  312.  
  313. do_ifup() {
  314.     if [ $1 != "${netdev}" ] || ! ifup $1 ; then
  315.         if [ -n "$addr_pfx" ] ; then
  316.             # use the info from get_ip_info()
  317.             ip addr flush $1
  318.             ip addr add ${addr_pfx} dev $1
  319.             ip link set dev $1 up
  320.             [ -n "$gateway" ] && ip route add default via ${gateway}
  321.         fi
  322.     fi
  323. }
  324.  
  325. # Usage: transfer_addrs src dst
  326. # Copy all IP addresses (including aliases) from device $src to device $dst.
  327. transfer_addrs () {
  328.     local src=$1
  329.     local dst=$2
  330.     # Don't bother if $dst already has IP addresses.
  331.     if ip addr show dev ${dst} | egrep -q '^ *inet ' ; then
  332.         return
  333.     fi
  334.     # Address lines start with 'inet' and have the device in them.
  335.     # Replace 'inet' with 'ip addr add' and change the device name $src
  336.     # to 'dev $src'.
  337.     ip addr show dev ${src} | egrep '^ *inet ' | sed -e "
  338. s/inet/ip addr add/
  339. s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+/[0-9]\+\)@\1@
  340. s/${src}/dev ${dst} label ${dst}/
  341. s/secondary//
  342. " | sh -e
  343.     # Remove automatic routes on destination device
  344.     ip route list | sed -ne "
  345. /dev ${dst}\( \|$\)/ {
  346.  s/^/ip route del /
  347.  p
  348. }" | sh -e
  349. }
  350.  
  351. # Usage: transfer_routes src dst
  352. # Get all IP routes to device $src, delete them, and
  353. # add the same routes to device $dst.
  354. # The original routes have to be deleted, otherwise adding them
  355. # for $dst fails (duplicate routes).
  356. transfer_routes () {
  357.     local src=$1
  358.     local dst=$2
  359.     # List all routes and grep the ones with $src in.
  360.     # Stick 'ip route del' on the front to delete.
  361.     # Change $src to $dst and use 'ip route add' to add.
  362.     ip route list | sed -ne "
  363. /dev ${src}\( \|$\)/ {
  364.  h
  365.  s/^/ip route del /
  366.  P
  367.  g
  368.  s/${src}/${dst}/
  369.  s/^/ip route add /
  370.  P
  371.  d
  372. }" | sh -e
  373. }
  374.  
  375.  
  376. ##
  377. # link_exists interface
  378. #
  379. # Returns 0 if the interface named exists (whether up or down), 1 otherwise.
  380. #
  381. link_exists()
  382. {
  383.     if ip link show "$1" >/dev/null 2>/dev/null
  384.     then
  385.         return 0
  386.     else
  387.         return 1
  388.     fi
  389. }
  390.  
  391. # Set the default forwarding policy for $dev to drop.
  392. # Allow forwarding to the bridge.
  393. antispoofing () {
  394.     iptables -P FORWARD DROP
  395.     iptables -F FORWARD
  396.     iptables -A FORWARD -m physdev --physdev-in ${pdev} -j ACCEPT
  397. }
  398.  
  399. # Usage: show_status dev bridge
  400. # Print ifconfig and routes.
  401. show_status () {
  402.     local dev=$1
  403.     local bridge=$2
  404.  
  405.     echo '============================================================'
  406.     ip addr show ${dev}
  407.     ip addr show ${bridge}
  408.     echo ' '
  409.     brctl show ${bridge}
  410.     echo ' '
  411.     ip route list
  412.     echo ' '
  413.     route -n
  414.     echo '============================================================'
  415. }
  416.  
  417. op_start () {
  418.     if [ "${bridge}" = "null" ] ; then
  419.         return
  420.     fi
  421.  
  422.     if link_exists "$pdev"; then
  423.         # The device is already up.
  424.         return
  425.     fi
  426.  
  427.     create_bridge ${tdev}
  428.  
  429.     preiftransfer ${netdev}
  430.     transfer_addrs ${netdev} ${tdev}
  431.     # Remember slaves for bonding interface.
  432.     if [ -e /sys/class/net/${netdev}/bonding/slaves ]; then
  433.         slaves=`cat /sys/class/net/${netdev}/bonding/slaves`
  434.     fi
  435.     # Remember the IP details for do_ifup.
  436.     get_ip_info ${netdev}
  437.     if ! ifdown ${netdev}; then
  438.         ip link set ${netdev} down
  439.         ip addr flush ${netdev}
  440.     fi
  441.     sleep 2
  442.     ip link set ${netdev} name ${pdev}
  443.     ip link set ${tdev} name ${bridge}
  444.  
  445.     setup_bridge_port ${pdev}
  446.  
  447.     # Restore slaves
  448.     if [ -n "${slaves}" ]; then
  449.         ip link set ${pdev} up
  450.         ifenslave ${pdev} ${slaves}
  451.     fi
  452.     add_to_bridge2 ${bridge} ${pdev}
  453.     do_ifup ${bridge}
  454.  
  455.     if [ ${antispoof} = 'yes' ] ; then
  456.         antispoofing
  457.     fi
  458. }
  459.  
  460. op_stop () {
  461.     if [ "${bridge}" = "null" ]; then
  462.         return
  463.     fi
  464.     if ! link_exists "$bridge"; then
  465.         return
  466.     fi
  467.  
  468.     transfer_addrs ${bridge} ${pdev}
  469.     if ! ifdown ${bridge}; then
  470.         get_ip_info ${bridge}
  471.     fi
  472.     ip link set ${pdev} down
  473.     ip addr flush ${bridge}
  474.  
  475.     brctl delif ${bridge} ${pdev}
  476.     ip link set ${bridge} down
  477.  
  478.     ip link set ${bridge} name ${tdev}
  479.     ip link set ${pdev} name ${netdev}
  480.     do_ifup ${netdev}
  481.  
  482.     brctl delbr ${tdev}
  483. }
  484.  
  485. # adds $dev to $bridge but waits for $dev to be in running state first
  486. add_to_bridge2() {
  487.     local bridge=$1
  488.     local dev=$2
  489.     local maxtries=10
  490.  
  491.     echo -n "Waiting for ${dev} to negotiate link."
  492.     ip link set ${dev} up
  493.     for i in `seq ${maxtries}` ; do
  494.         if ifconfig ${dev} | grep -q RUNNING ; then
  495.             break
  496.         else
  497.             echo -n '.'
  498.             sleep 1
  499.         fi
  500.     done
  501.  
  502.     if [ ${i} -eq ${maxtries} ] ; then echo -n '(link isnt in running state)' ; fi
  503.     echo
  504.  
  505.     add_to_bridge ${bridge} ${dev}
  506. }
  507.  
  508. case "$command" in
  509.     start)
  510.         op_start
  511.         ;;
  512.  
  513.     stop)
  514.         op_stop
  515.         ;;
  516.  
  517.     status)
  518.         show_status ${netdev} ${bridge}
  519.         ;;
  520.  
  521.     *)
  522.         echo "Unknown command: $command" >&2
  523.         echo 'Valid commands are: start, stop, status' >&2
  524.         exit 1
  525. esac
  526. + case "$command" in
  527. + op_start
  528. + '[' eth0 = null ']'
  529. + link_exists peth0
  530. + ip link show peth0
  531. + return 1
  532. + create_bridge tmpbridge
  533. + local bridge=tmpbridge
  534. + '[' '!' -e /sys/class/net/tmpbridge/bridge ']'
  535. + brctl addbr tmpbridge
  536. + brctl stp tmpbridge off
  537. + brctl setfd tmpbridge 0
  538. + sysctl -q -w net.bridge.bridge-nf-call-arptables=0
  539. + sysctl -q -w net.bridge.bridge-nf-call-ip6tables=0
  540. + sysctl -q -w net.bridge.bridge-nf-call-iptables=0
  541. + preiftransfer eth0
  542. + true
  543. + transfer_addrs eth0 tmpbridge
  544. + local src=eth0
  545. + local dst=tmpbridge
  546. + ip addr show dev tmpbridge
  547. + egrep -q '^ *inet '
  548. + ip addr show dev eth0
  549. + egrep '^ *inet '
  550. + sed -e '
  551. s/inet/ip addr add/
  552. s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+/[0-9]\+\)@\1@
  553. s/eth0/dev tmpbridge label tmpbridge/
  554. s/secondary//
  555. '
  556. + sh -e
  557. + ip route list
  558. + sed -ne '
  559. /dev tmpbridge\( \|$\)/ {
  560.  s/^/ip route del /
  561.  p
  562. }'
  563. + sh -e
  564. + '[' -e /sys/class/net/eth0/bonding/slaves ']'
  565. + get_ip_info eth0
  566. ip addr show dev $1 | egrep '^ *inet' | sed -e 's/ *inet //' -e 's/ .*//'
  567. ++ ip addr show dev eth0
  568. ++ egrep '^ *inet'
  569. ++ sed -e 's/ *inet //' -e 's/ .*//'
  570. + addr_pfx=46.4.95.215
  571. ip route show dev $1 | fgrep default | sed 's/default via //'
  572. ++ ip route show dev eth0
  573. ++ fgrep default
  574. ++ sed 's/default via //'
  575. + gateway='46.4.95.193 '
  576. + ifdown eth0
  577. + false
  578. + ip link set eth0 down
  579. + ip addr flush eth0
  580. + sleep 2
  581. + ip link set eth0 name peth0
  582. + ip link set tmpbridge name eth0
  583. + setup_bridge_port peth0
  584. + local dev=peth0
  585. + ip link set peth0 down
  586. + ip addr flush peth0
  587. Nothing to flush.
  588. + '[' -n '' ']'
  589. + add_to_bridge2 eth0 peth0
  590. + local bridge=eth0
  591. + local dev=peth0
  592. + local maxtries=10
  593. + echo -n 'Waiting for peth0 to negotiate link.'
  594. Waiting for peth0 to negotiate link.+ ip link set peth0 up
  595. seq ${maxtries}
  596. ++ seq 10
  597. + for i in '`seq ${maxtries}`'
  598. + ifconfig peth0
  599. + grep -q RUNNING
  600. + break
  601. + '[' 1 -eq 10 ']'
  602. + echo
  603.  
  604. + add_to_bridge eth0 peth0
  605. + local bridge=eth0
  606. + local dev=peth0
  607. + '[' -e /sys/class/net/eth0/brif/peth0 ']'
  608. + brctl addif eth0 peth0
  609. + ip link set peth0 up
  610. + do_ifup eth0
  611. + '[' eth0 '!=' eth0 ']'
  612. + ifup eth0
  613. + false
  614. + '[' -n 46.4.95.215 ']'
  615. + ip addr flush eth0
  616. + ip addr add 46.4.95.215 dev eth0
  617. + ip link set dev eth0 up
  618. + '[' -n '46.4.95.193 ' ']'
  619. + ip route add default via 46.4.95.193
  620. RTNETLINK answers: Network is unreachable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement