ia64/xen-unstable

annotate tools/hotplug/Linux/vif-nat @ 19497:2f6ed9318c03

network-bridge: Fix do_ifup in the case of ${bridge} != ${netdev}

On RHEL5.2, ifup ${bridge} fails if ${bridge} != ${netdev},
because RHEL5.2's ifup ${bridge} runs the following sequence:

1. Search CONFIG that has the same mac address of ${bridge}.=20
ifcfg-${netdev} is found.
2. Run "ip link set dev ${netdev} up".
# ${bridge} is expected.
3. Output "Failed to bring up ${netdev}."
Because ${netdev} does not exist.

Thus, do_ifup() should not use ifup if ${bridge} != ${netdev}.

Signed-off-by: KUWAMURA Shin'ya <kuwa@jp.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Apr 02 11:48:10 2009 +0100 (2009-04-02)
parents 4bfc67b09e9c
children 415505139333
rev   line source
keir@18770 1 #!/bin/bash
keir@18770 2 #============================================================================
keir@18770 3 # /etc/xen/vif-nat
keir@18770 4 #
keir@18770 5 # Script for configuring a vif in routed-nat mode.
keir@18770 6 # The hotplugging system will call this script if it is specified either in
keir@18770 7 # the device configuration given to Xend, or the default Xend configuration
keir@18770 8 # in /etc/xen/xend-config.sxp. If the script is specified in neither of those
keir@18770 9 # places, then vif-bridge is the default.
keir@18770 10 #
keir@18770 11 # Usage:
keir@18770 12 # vif-nat (add|remove|online|offline)
keir@18770 13 #
keir@18770 14 # Environment vars:
keir@18770 15 # vif vif interface name (required).
keir@18770 16 # XENBUS_PATH path to this device's details in the XenStore (required).
keir@18770 17 #
keir@18770 18 # Parameters:
keir@18770 19 # dhcp Whether to alter the local DHCP configuration to include this
keir@18770 20 # new host (default no).
keir@18770 21 #
keir@18770 22 # Read from the store:
keir@18770 23 # ip list of IP networks for the vif, space-separated (default given in
keir@18770 24 # this script).
keir@18770 25 #============================================================================
keir@18770 26
keir@18770 27
keir@18770 28 dir=$(dirname "$0")
keir@18770 29 . "$dir/vif-common.sh"
keir@18770 30
keir@18770 31 # turn on dhcp feature by default if dhcpd is installed
keir@18770 32 if [ -f /etc/dhcpd.conf ]
keir@18770 33 then
keir@18770 34 dhcp=${dhcp:-yes}
keir@18770 35 else
keir@18770 36 dhcp=${dhcp:-no}
keir@18770 37 fi
keir@18770 38
keir@18770 39 if [ "$dhcp" != 'no' ]
keir@18770 40 then
keir@18770 41 dhcpd_conf_file=$(find_dhcpd_conf_file)
keir@18770 42 dhcpd_init_file=$(find_dhcpd_init_file)
keir@18770 43 dhcpd_arg_file=$(find_dhcpd_arg_file)
keir@18770 44 if [ -z "$dhcpd_conf_file" ] || [ -z "$dhcpd_init_file" ] || [ -z "$dhcpd_arg_file" ]
keir@18770 45 then
keir@18770 46 echo 'Failed to find dhcpd configuration or init or args file.' >&2
keir@18770 47 exit 1
keir@18770 48 fi
keir@18770 49 fi
keir@18770 50
keir@18770 51
keir@18770 52 domid=$(xenstore_read "$XENBUS_PATH/frontend-id")
keir@18770 53 vifid=$(xenstore_read "$XENBUS_PATH/handle")
keir@18770 54 vifid=$(( $vifid + 1 ))
keir@18770 55
keir@18770 56
keir@18770 57 ip_from_dom()
keir@18770 58 {
keir@18770 59 local domid1=$(( $domid / 256 ))
keir@18770 60 local domid2=$(( $domid % 256 ))
keir@18770 61
keir@18770 62 echo "10.$domid1.$domid2.$vifid/16"
keir@18770 63 }
keir@18770 64
keir@18770 65
keir@18770 66 routing_ip()
keir@18770 67 {
keir@18770 68 echo $(echo $1 | awk -F. '{print $1"."$2"."$3"."$4 + 127}')
keir@18770 69 }
keir@18770 70
keir@18770 71
keir@18770 72 dotted_quad()
keir@18770 73 {
keir@18770 74 echo\
keir@18770 75 $(( ($1 & 0xFF000000) >> 24))\
keir@18770 76 .$(( ($1 & 0x00FF0000) >> 16))\
keir@18770 77 .$(( ($1 & 0x0000FF00) >> 8 ))\
keir@18770 78 .$(( $1 & 0x000000FF ))
keir@18770 79 }
keir@18770 80
keir@18770 81
keir@18770 82 if [ "$ip" = "" ]
keir@18770 83 then
keir@18770 84 ip=$(ip_from_dom)
keir@18770 85 fi
keir@18770 86
keir@18770 87 router_ip=$(routing_ip "$ip")
keir@18770 88
keir@18770 89 # Split the given IP/bits pair.
keir@18770 90 vif_ip=`echo ${ip} | awk -F/ '{print $1}'`
keir@18770 91
keir@18770 92 hostname=$(xenstore_read "$XENBUS_PATH/domain" | tr -- '_.:/+' '-----')
keir@18770 93 if [ "$vifid" != "1" ]
keir@18770 94 then
keir@18770 95 hostname="$hostname-$vifid"
keir@18770 96 fi
keir@18770 97
keir@18770 98 dhcparg_remove_entry()
keir@18770 99 {
keir@18770 100 local tmpfile=$(mktemp)
keir@18770 101 sed -e "s/$vif //" "$dhcpd_arg_file" >"$tmpfile"
keir@18770 102 if diff "$tmpfile" "$dhcpd_arg_file" >/dev/null
keir@18770 103 then
keir@18770 104 rm "$tmpfile"
keir@18770 105 else
keir@18770 106 mv "$tmpfile" "$dhcpd_arg_file"
keir@18770 107 fi
keir@18770 108 }
keir@18770 109
keir@18770 110 dhcparg_add_entry()
keir@18770 111 {
keir@18770 112 dhcparg_remove_entry
keir@18770 113 local tmpfile=$(mktemp)
keir@18770 114 # handle Red Hat, SUSE, and Debian styles, with or without quotes
keir@18770 115 sed -e 's/^DHCPDARGS="*\([^"]*\)"*/DHCPDARGS="\1'"$vif "'"/' \
keir@18770 116 "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
keir@18770 117 sed -e 's/^DHCPD_INTERFACE="*\([^"]*\)"*/DHCPD_INTERFACE="\1'"$vif "'"/' \
keir@18770 118 "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
keir@18770 119 sed -e 's/^INTERFACES="*\([^"]*\)"*/INTERFACES="\1'"$vif "'"/' \
keir@18770 120 "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
keir@18770 121 rm -f "$tmpfile"
keir@18770 122 }
keir@18770 123
keir@18770 124 dhcp_remove_entry()
keir@18770 125 {
keir@18770 126 local tmpfile=$(mktemp)
keir@18770 127 grep -v "host $hostname" "$dhcpd_conf_file" >"$tmpfile"
keir@18770 128 if diff "$tmpfile" "$dhcpd_conf_file" >/dev/null
keir@18770 129 then
keir@18770 130 rm "$tmpfile"
keir@18770 131 else
keir@18770 132 mv "$tmpfile" "$dhcpd_conf_file"
keir@18770 133 fi
keir@18770 134 dhcparg_remove_entry
keir@18770 135 }
keir@18770 136
keir@18770 137
keir@18770 138 dhcp_up()
keir@18770 139 {
keir@18770 140 claim_lock "vif-nat-dhcp"
keir@18770 141 dhcp_remove_entry
keir@18770 142 mac=$(xenstore_read "$XENBUS_PATH/mac")
keir@18770 143 echo >>"$dhcpd_conf_file" \
keir@18770 144 "host $hostname { hardware ethernet $mac; fixed-address $vif_ip; option routers $router_ip; option host-name \"$hostname\"; }"
keir@18770 145 dhcparg_add_entry
keir@18770 146 release_lock "vif-nat-dhcp"
keir@18770 147 "$dhcpd_init_file" restart || true
keir@18770 148 }
keir@18770 149
keir@18770 150
keir@18770 151 dhcp_down()
keir@18770 152 {
keir@18770 153 claim_lock "vif-nat-dhcp"
keir@18770 154 dhcp_remove_entry
keir@18770 155 release_lock "vif-nat-dhcp"
keir@18770 156 "$dhcpd_init_file" restart || true # We need to ignore failure because
keir@18770 157 # ISC dhcpd 3 borks if there is nothing
keir@18770 158 # for it to do, which is the case if
keir@18770 159 # the outgoing interface is not
keir@18770 160 # configured to offer leases and there
keir@18770 161 # are no vifs.
keir@18770 162 }
keir@18770 163
keir@18770 164
keir@18770 165 case "$command" in
keir@18770 166 online)
keir@18770 167 if ip route | grep -q "dev $vif"
keir@18770 168 then
keir@18770 169 log debug "$vif already up"
keir@18770 170 exit 0
keir@18770 171 fi
keir@18770 172
keir@18770 173 do_or_die ip link set "$vif" up arp on
keir@18770 174 do_or_die ip addr add "$router_ip" dev "$vif"
keir@18770 175 do_or_die ip route add "$vif_ip" dev "$vif" src "$router_ip"
keir@18770 176 echo 1 >/proc/sys/net/ipv4/conf/${vif}/proxy_arp
keir@18770 177 [ "$dhcp" != 'no' ] && dhcp_up
keir@18770 178 ;;
keir@18770 179 offline)
keir@18770 180 [ "$dhcp" != 'no' ] && dhcp_down
keir@18770 181 do_without_error ifconfig "$vif" down
keir@18770 182 ;;
keir@18770 183 esac
keir@18770 184
keir@18770 185
keir@18770 186 handle_iptable
keir@18770 187
keir@18770 188 log debug "Successful vif-nat $command for $vif."
keir@18770 189 if [ "$command" = "online" ]
keir@18770 190 then
keir@18770 191 success
keir@18770 192 fi