ia64/xen-unstable

view tools/hotplug/Linux/vif-nat @ 19818:415505139333

Get rid of hardcoded pathes for stubdom and hotplug scripts

- Have the buid system generate a file which exports the install paths
for the hotplug scripts and stubdom / stubdom-dm
- Move file generation code from tools/python/Makefile into a gmake
macro
in Config.mk to avoid maintenance of three duplicates each with its
own
tweaks and bugs
- Export gmake variables into ioemu as shell variables
for upcoming ioemu patches
- Do above as a gmake macro to avoid maintenance of several duplicates
- Adjust hotplug scripts to find the right xen binaries from the
install directory
- Adjust stubdom-dm to use the install directories

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Jun 23 17:25:51 2009 +0100 (2009-06-23)
parents 4bfc67b09e9c
children
line source
1 #!/bin/bash
2 #============================================================================
3 # ${XEN_SCRIPT_DIR}/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 ${XEN_CONFIG_DIR}/xend-config.sxp. If the script is specified in
9 # neither of those 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 # turn on dhcp feature by default if dhcpd is installed
32 if [ -f /etc/dhcpd.conf ]
33 then
34 dhcp=${dhcp:-yes}
35 else
36 dhcp=${dhcp:-no}
37 fi
39 if [ "$dhcp" != 'no' ]
40 then
41 dhcpd_conf_file=$(find_dhcpd_conf_file)
42 dhcpd_init_file=$(find_dhcpd_init_file)
43 dhcpd_arg_file=$(find_dhcpd_arg_file)
44 if [ -z "$dhcpd_conf_file" ] || [ -z "$dhcpd_init_file" ] || [ -z "$dhcpd_arg_file" ]
45 then
46 echo 'Failed to find dhcpd configuration or init or args file.' >&2
47 exit 1
48 fi
49 fi
52 domid=$(xenstore_read "$XENBUS_PATH/frontend-id")
53 vifid=$(xenstore_read "$XENBUS_PATH/handle")
54 vifid=$(( $vifid + 1 ))
57 ip_from_dom()
58 {
59 local domid1=$(( $domid / 256 ))
60 local domid2=$(( $domid % 256 ))
62 echo "10.$domid1.$domid2.$vifid/16"
63 }
66 routing_ip()
67 {
68 echo $(echo $1 | awk -F. '{print $1"."$2"."$3"."$4 + 127}')
69 }
72 dotted_quad()
73 {
74 echo\
75 $(( ($1 & 0xFF000000) >> 24))\
76 .$(( ($1 & 0x00FF0000) >> 16))\
77 .$(( ($1 & 0x0000FF00) >> 8 ))\
78 .$(( $1 & 0x000000FF ))
79 }
82 if [ "$ip" = "" ]
83 then
84 ip=$(ip_from_dom)
85 fi
87 router_ip=$(routing_ip "$ip")
89 # Split the given IP/bits pair.
90 vif_ip=`echo ${ip} | awk -F/ '{print $1}'`
92 hostname=$(xenstore_read "$XENBUS_PATH/domain" | tr -- '_.:/+' '-----')
93 if [ "$vifid" != "1" ]
94 then
95 hostname="$hostname-$vifid"
96 fi
98 dhcparg_remove_entry()
99 {
100 local tmpfile=$(mktemp)
101 sed -e "s/$vif //" "$dhcpd_arg_file" >"$tmpfile"
102 if diff "$tmpfile" "$dhcpd_arg_file" >/dev/null
103 then
104 rm "$tmpfile"
105 else
106 mv "$tmpfile" "$dhcpd_arg_file"
107 fi
108 }
110 dhcparg_add_entry()
111 {
112 dhcparg_remove_entry
113 local tmpfile=$(mktemp)
114 # handle Red Hat, SUSE, and Debian styles, with or without quotes
115 sed -e 's/^DHCPDARGS="*\([^"]*\)"*/DHCPDARGS="\1'"$vif "'"/' \
116 "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
117 sed -e 's/^DHCPD_INTERFACE="*\([^"]*\)"*/DHCPD_INTERFACE="\1'"$vif "'"/' \
118 "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
119 sed -e 's/^INTERFACES="*\([^"]*\)"*/INTERFACES="\1'"$vif "'"/' \
120 "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file"
121 rm -f "$tmpfile"
122 }
124 dhcp_remove_entry()
125 {
126 local tmpfile=$(mktemp)
127 grep -v "host $hostname" "$dhcpd_conf_file" >"$tmpfile"
128 if diff "$tmpfile" "$dhcpd_conf_file" >/dev/null
129 then
130 rm "$tmpfile"
131 else
132 mv "$tmpfile" "$dhcpd_conf_file"
133 fi
134 dhcparg_remove_entry
135 }
138 dhcp_up()
139 {
140 claim_lock "vif-nat-dhcp"
141 dhcp_remove_entry
142 mac=$(xenstore_read "$XENBUS_PATH/mac")
143 echo >>"$dhcpd_conf_file" \
144 "host $hostname { hardware ethernet $mac; fixed-address $vif_ip; option routers $router_ip; option host-name \"$hostname\"; }"
145 dhcparg_add_entry
146 release_lock "vif-nat-dhcp"
147 "$dhcpd_init_file" restart || true
148 }
151 dhcp_down()
152 {
153 claim_lock "vif-nat-dhcp"
154 dhcp_remove_entry
155 release_lock "vif-nat-dhcp"
156 "$dhcpd_init_file" restart || true # We need to ignore failure because
157 # ISC dhcpd 3 borks if there is nothing
158 # for it to do, which is the case if
159 # the outgoing interface is not
160 # configured to offer leases and there
161 # are no vifs.
162 }
165 case "$command" in
166 online)
167 if ip route | grep -q "dev $vif"
168 then
169 log debug "$vif already up"
170 exit 0
171 fi
173 do_or_die ip link set "$vif" up arp on
174 do_or_die ip addr add "$router_ip" dev "$vif"
175 do_or_die ip route add "$vif_ip" dev "$vif" src "$router_ip"
176 echo 1 >/proc/sys/net/ipv4/conf/${vif}/proxy_arp
177 [ "$dhcp" != 'no' ] && dhcp_up
178 ;;
179 offline)
180 [ "$dhcp" != 'no' ] && dhcp_down
181 do_without_error ifconfig "$vif" down
182 ;;
183 esac
186 handle_iptable
188 log debug "Successful vif-nat $command for $vif."
189 if [ "$command" = "online" ]
190 then
191 success
192 fi