2
#============================================================================
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.
12
# vif-nat (add|remove|online|offline)
15
# vif vif interface name (required).
16
# XENBUS_PATH path to this device's details in the XenStore (required).
19
# dhcp Whether to alter the local DHCP configuration to include this
20
# new host (default no).
22
# Read from the store:
23
# ip list of IP networks for the vif, space-separated (default given in
25
#============================================================================
29
. "$dir/vif-common.sh"
31
# turn on dhcp feature by default if dhcpd is installed
32
if [ -f /etc/dhcpd.conf ]
39
if [ "$dhcp" != 'no' ]
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" ]
46
echo 'Failed to find dhcpd configuration or init or args file.' >&2
52
domid=$(xenstore_read "$XENBUS_PATH/frontend-id")
53
vifid=$(xenstore_read "$XENBUS_PATH/handle")
54
vifid=$(( $vifid + 1 ))
59
local domid1=$(( $domid / 256 ))
60
local domid2=$(( $domid % 256 ))
62
echo "10.$domid1.$domid2.$vifid/16"
68
echo $(echo $1 | awk -F. '{print $1"."$2"."$3"."$4 + 127}')
75
$(( ($1 & 0xFF000000) >> 24))\
76
.$(( ($1 & 0x00FF0000) >> 16))\
77
.$(( ($1 & 0x0000FF00) >> 8 ))\
78
.$(( $1 & 0x000000FF ))
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" ]
95
hostname="$hostname-$vifid"
98
dhcparg_remove_entry()
100
local tmpfile=$(mktemp)
101
sed -e "s/$vif //" "$dhcpd_arg_file" >"$tmpfile"
102
if diff "$tmpfile" "$dhcpd_arg_file" >/dev/null
106
mv "$tmpfile" "$dhcpd_arg_file"
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"
126
local tmpfile=$(mktemp)
127
grep -v "host $hostname" "$dhcpd_conf_file" >"$tmpfile"
128
if diff "$tmpfile" "$dhcpd_conf_file" >/dev/null
132
mv "$tmpfile" "$dhcpd_conf_file"
140
claim_lock "vif-nat-dhcp"
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\"; }"
146
release_lock "vif-nat-dhcp"
147
"$dhcpd_init_file" restart || true
153
claim_lock "vif-nat-dhcp"
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
167
if ip route | grep -q "dev $vif"
169
log debug "$vif already up"
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
180
[ "$dhcp" != 'no' ] && dhcp_down
181
do_without_error ifconfig "$vif" down
188
log debug "Successful vif-nat $command for $vif."
189
if [ "$command" = "online" ]