~ubuntu-branches/ubuntu/trusty/xen-common/trusty

« back to all changes in this revision

Viewing changes to tools/examples/vif-nat

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2009-11-22 16:51:53 UTC
  • mfrom: (5.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20091122165153-d36l98kbx8a930h2
Tags: 3.4.2-2
* Redefine Xen version tests to allow detection of bare metal.
  (closes: #556859)
* Support oldstyle Xen kernel without xenfs. (closes: #557151)
* Use debhelper compat level 7.
* Remove oldstable-only conflicts.
* Add wrapper for xenpm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/bash
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
 
#============================================================================
26
 
 
27
 
 
28
 
dir=$(dirname "$0")
29
 
. "$dir/vif-common.sh"
30
 
 
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
38
 
 
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
50
 
 
51
 
 
52
 
domid=$(xenstore_read "$XENBUS_PATH/frontend-id")
53
 
vifid=$(xenstore_read "$XENBUS_PATH/handle")
54
 
vifid=$(( $vifid + 1 ))
55
 
 
56
 
 
57
 
ip_from_dom()
58
 
{
59
 
  local domid1=$(( $domid / 256 ))
60
 
  local domid2=$(( $domid % 256 ))
61
 
 
62
 
  echo "10.$domid1.$domid2.$vifid/16"
63
 
}
64
 
 
65
 
 
66
 
routing_ip()
67
 
{
68
 
  echo $(echo $1 | awk -F. '{print $1"."$2"."$3"."$4 + 127}')
69
 
}
70
 
 
71
 
 
72
 
dotted_quad()
73
 
{
74
 
 echo\
75
 
 $(( ($1 & 0xFF000000) >> 24))\
76
 
.$(( ($1 & 0x00FF0000) >> 16))\
77
 
.$(( ($1 & 0x0000FF00) >> 8 ))\
78
 
.$((  $1 & 0x000000FF       ))
79
 
}
80
 
 
81
 
 
82
 
if [ "$ip" = "" ]
83
 
then
84
 
  ip=$(ip_from_dom)
85
 
fi
86
 
 
87
 
router_ip=$(routing_ip "$ip")
88
 
 
89
 
# Split the given IP/bits pair.
90
 
vif_ip=`echo ${ip} | awk -F/ '{print $1}'`
91
 
 
92
 
hostname=$(xenstore_read "$XENBUS_PATH/domain" | tr -- '_.:/+' '-----')
93
 
if [ "$vifid" != "1" ]
94
 
then
95
 
  hostname="$hostname-$vifid"
96
 
fi
97
 
 
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
 
}
109
 
 
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
 
}
123
 
 
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
 
}
136
 
 
137
 
 
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
 
}
149
 
 
150
 
 
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
 
}
163
 
 
164
 
 
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
172
 
 
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
184
 
 
185
 
 
186
 
handle_iptable
187
 
 
188
 
log debug "Successful vif-nat $command for $vif."
189
 
if [ "$command" = "online" ]
190
 
then
191
 
  success
192
 
fi