Fix autoinstalls to work around a modern d-i bug
[invirt/packages/invirt-xen-config.git] / vif-invirtroute
1 #!/bin/bash
2
3 #============================================================================
4 # /etc/xen/vif-route
5 #
6 # Script for configuring a vif in routed mode.
7 # The hotplugging system will call this script if it is specified either in
8 # the device configuration given to Xend, or the default Xend configuration
9 # in /etc/xen/xend-config.sxp.  If the script is specified in neither of those
10 # places, then vif-bridge is the default.
11 #
12 # Usage:
13 # vif-route (add|remove|online|offline)
14 #
15 # Environment vars:
16 # vif    vif interface name (required).
17 # XENBUS_PATH path to this device's details in the XenStore (required).
18 # Read from the store:
19 # ip     list of IP networks for the vif, space-separated (default given in
20 #        this script).
21 #
22 # This script will set up proxy arp  for any ip addresses that are being routed
23 # type read to determine if the device is ioemu
24
25 #============================================================================
26 dir=$(dirname "$0")
27 . "$dir/vif-common.sh"
28
29 main_ip=$(dom0_ip)
30 dev=${dev:-${vif}}
31
32 case "$command" in
33     online|add)
34         ifconfig ${dev} ${main_ip} netmask 255.255.255.255 up
35         echo 1 >/proc/sys/net/ipv4/conf/${dev}/proxy_arp
36         echo 1 >/proc/sys/net/ipv4/conf/${dev}/rp_filter 
37         xenstore-write "$XENBUS_PATH/feature-gso-tcpv4" 0
38         if [ x${qemu_online} != xyes ]; then
39           ethtool -K ${dev} tx off
40         fi
41         ipcmd='add'
42         ipt_action='-A'
43         cmdprefix=''
44         ;;
45     offline|remove)
46         do_without_error ifdown ${vif}
47         ipcmd='del'
48         ipt_action='-D'
49         cmdprefix='do_without_error'
50         ;;
51 esac
52
53 vif_type=$(xenstore_read_default "$XENBUS_PATH/type" "viffront")
54 if [  ${vif_type} != "ioemu"  -o  x${qemu_online} = xyes ] ; then
55     if [ "${ip}" ] ; then
56     # If we've been given a list of IP addresses, then add routes from dom0 to
57     # the guest using those addresses.
58         for addr in ${ip} ; do
59             # When PVHVM is enabled, Xen plugs two interfaces into
60             # HVMs - an emulated tap device and a paravirt vif device.
61             # vif-invirtroute (and vif-route, for that matter!) will
62             # fail when the second one is brought up, because the
63             # second invocation of 'ip route add' is identical to the
64             # first (same source and destination IPs) and the kernel
65             # rejects the new route.
66             #
67             # We work around this by adding the routes with different metrics.
68             # This should work because:
69             #
70             # 1) In the case of a pv-aware guest, the kernel will
71             # unplug the tap interface, which will bring down the tap
72             # interface's route, leaving only the one via the vif (and
73             # so the metric shouldn't matter, because it's the only
74             # route)
75             #
76             # 2) In the case of a non-pv-aware guest, the tap route
77             # (with metric 1) should take precedence over the vif
78             # route and carry all the traffic.
79
80             src=""
81             if [ $ipcmd == "add" ]; then
82                 src="src ${main_ip}"
83                 case $dev in
84                     vif*)
85                         metric="metric 2"
86                         ;;
87                     tap*)
88                         metric="metric 1"
89                         ;;
90                 esac
91             fi
92             ${cmdprefix} ip route ${ipcmd} ${addr} dev ${dev} ${src} $metric
93             case "$command" in
94                 online|add)
95                     timeout -s KILL 5 arpspoof -i $(invirt-getconf xen.iface) -t ${gateway} ${addr} || :
96                     ;;
97             esac
98         done 
99         if [ -n "$other_ip" ]; then
100             ${cmdprefix} ip route ${ipcmd} ${other_ip} dev ${dev} ${src} $metric
101             iptables -t nat ${ipt_action} PREROUTING -d ${other_ip} -j DNAT --to-destination ${addr}
102             case "$command" in
103                 online|add)
104                     timeout -s KILL 5 arpspoof -i $(invirt-getconf xen.iface) -t ${other_gateway} ${other_ip} || :
105                     ;;
106             esac
107         fi
108     fi
109 fi
110
111 log debug "Successful vif-route $command for $vif."
112 case "$command" in
113     online|add)
114         success
115         ;;
116 esac