direct-io.hg

view tools/examples/vif-nat @ 8334:b4dfb49332d2

Remove unused netmask,network etc. These were used when we called ifconfig to
set up the networking, but now that we use ip directly, we don't need them, and
they were in any case broken when the ip address was specified without a suffix.

This work is by James Dykman <dykman@us.ibm.com>.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Mon Dec 12 11:15:29 2005 +0000 (2005-12-12)
parents 10220071deff
children ef2fff896422
line source
1 #!/bin/sh
2 #============================================================================
3 # /etc/xen/vif-nat
4 #
5 # Script for configuring a vif in routed-nat mode.
6 # The hotplugging system will call this script if it is specified either in
7 # the device configuration given to Xend, or the default Xend configuration
8 # in /etc/xen/xend-config.sxp. If the script is specified in neither of those
9 # places, then vif-bridge is the default.
10 #
11 # Usage:
12 # vif-nat (add|remove|online|offline)
13 #
14 # Environment vars:
15 # vif vif interface name (required).
16 # XENBUS_PATH path to this device's details in the XenStore (required).
17 #
18 # Parameters:
19 # dhcp Whether to alter the local DHCP configuration to include this
20 # new host (default no).
21 #
22 # Read from the store:
23 # ip list of IP networks for the vif, space-separated (default given in
24 # this script).
25 #============================================================================
28 dir=$(dirname "$0")
29 . "$dir/vif-common.sh"
31 dhcp=${dhcp:-no}
33 if [ "$dhcp" != 'no' ]
34 then
35 dhcpd_conf_file=$(find_dhcpd_conf_file)
36 dhcpd_init_file=$(find_dhcpd_init_file)
37 if [ -z "$dhcpd_conf_file" ] || [ -z "$dhcpd_init_file" ]
38 then
39 echo 'Failed to find dhcpd configuration or init file.' >&2
40 exit 1
41 fi
42 fi
45 domid=$(echo "$XENBUS_PATH" | sed -n 's#.*/\([0-9]*\)/[0-9]*$#\1#p')
46 vifid=$(echo "$XENBUS_PATH" | sed -n 's#.*/[0-9]*/\([0-9]*\)$#\1#p')
47 vifid=$(( $vifid + 1 ))
50 ip_from_dom()
51 {
52 local domid1=$(( $domid / 256 ))
53 local domid2=$(( $domid % 256 ))
55 echo "10.$domid1.$domid2.$vifid/16"
56 }
59 routing_ip()
60 {
61 echo $(echo $1 | awk -F. '{print $1"."$2"."$3"."$4 + 127}')
62 }
65 dotted_quad()
66 {
67 echo\
68 $(( ($1 & 0xFF000000) >> 24))\
69 .$(( ($1 & 0x00FF0000) >> 16))\
70 .$(( ($1 & 0x0000FF00) >> 8 ))\
71 .$(( $1 & 0x000000FF ))
72 }
75 if [ "$ip" == "" ]
76 then
77 ip=$(ip_from_dom)
78 fi
80 router_ip=$(routing_ip "$ip")
82 # Split the given IP/bits pair.
83 vif_ip=`echo ${ip} | awk -F/ '{print $1}'`
85 hostname=$(xenstore_read "$XENBUS_PATH/domain" | tr -- '_.:/+' '-----')
86 if [ "$vifid" != "1" ]
87 then
88 hostname="$hostname-$vifid"
89 fi
92 dhcp_remove_entry()
93 {
94 local tmpfile=$(mktemp)
95 grep -v "host $hostname" "$dhcpd_conf_file" >"$tmpfile"
96 if diff "$tmpfile" "$dhcpd_conf_file" >/dev/null
97 then
98 rm "$tmpfile"
99 else
100 mv "$tmpfile" "$dhcpd_conf_file"
101 fi
102 }
105 dhcp_up()
106 {
107 claim_lock "vif-nat-dhcp"
108 dhcp_remove_entry
109 mac=$(xenstore_read "$XENBUS_PATH/mac")
110 echo >>"$dhcpd_conf_file" \
111 "host $hostname { hardware ethernet $mac; fixed-address $vif_ip; option routers $router_ip; option host-name \"$hostname\"; }"
112 release_lock "vif-nat-dhcp"
113 "$dhcpd_init_file" restart || true
114 }
117 dhcp_down()
118 {
119 claim_lock "vif-nat-dhcp"
120 dhcp_remove_entry
121 release_lock "vif-nat-dhcp"
122 "$dhcpd_init_file" restart || true # We need to ignore failure because
123 # ISC dhcpd 3 borks if there is nothing
124 # for it to do, which is the case if
125 # the outgoing interface is not
126 # configured to offer leases and there
127 # are no vifs.
128 }
131 case "$command" in
132 online)
133 if ip route | grep -q "dev $vif"
134 then
135 log debug "$vif already up"
136 exit 0
137 fi
139 do_or_die ip link set "$vif" up arp on
140 do_or_die ip addr add "$router_ip" dev "$vif"
141 do_or_die ip route add "$vif_ip" dev "$vif" src "$router_ip"
142 echo 1 >/proc/sys/net/ipv4/conf/${vif}/proxy_arp
143 [ "$dhcp" != 'no' ] && dhcp_up
144 ;;
145 offline)
146 [ "$dhcp" != 'no' ] && dhcp_down
147 do_without_error ifconfig "$vif" down
148 ;;
149 esac
152 handle_iptable
154 log debug "Successful vif-nat $command for $vif."
155 if [ "$command" == "online" ]
156 then
157 success
158 fi