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

« back to all changes in this revision

Viewing changes to tools/hotplug/Linux/network-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 -x
 
2
#============================================================================
 
3
# Default Xen network start/stop script when using NAT.
 
4
# Xend calls a network script when it starts.
 
5
# The script name to use is defined in /etc/xen/xend-config.sxp
 
6
# in the network-script field.
 
7
#
 
8
# Usage:
 
9
#
 
10
# network-nat (start|stop|status) {VAR=VAL}*
 
11
#
 
12
# Vars:
 
13
#
 
14
# netdev     The gateway interface (default eth0).
 
15
# antispoof  Whether to use iptables to prevent spoofing (default no).
 
16
# dhcp       Whether to alter the local DHCP configuration (default no).
 
17
#
 
18
#============================================================================
 
19
 
 
20
dir=$(dirname "$0")
 
21
. "$dir/xen-script-common.sh"
 
22
. "$dir/xen-network-common.sh"
 
23
 
 
24
findCommand "$@"
 
25
evalVariables "$@"
 
26
 
 
27
netdev=${netdev:-eth0}
 
28
# antispoofing not yet implemented
 
29
antispoof=${antispoof:-no}
 
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
 
 
40
if [ "$dhcp" != 'no' ]
 
41
then
 
42
  dhcpd_conf_file=$(find_dhcpd_conf_file)
 
43
  dhcpd_init_file=$(find_dhcpd_init_file)
 
44
  if [ -z "$dhcpd_conf_file" ] || [ -z "$dhcpd_init_file" ]
 
45
  then
 
46
    echo 'Failed to find dhcpd configuration or init file.' >&2
 
47
    exit 1
 
48
  fi
 
49
fi
 
50
 
 
51
domain_name=`cat /etc/resolv.conf | grep -v "#" | grep -E 'search|domain' -i | tail -n 1 | awk '{ print $2 }'`
 
52
nameserver=`cat /etc/resolv.conf | grep -v "#" | grep "nameserver" -i -m 1 | awk '{ print $2 }'`
 
53
 
 
54
function dhcp_start()
 
55
{
 
56
  if ! grep -q "subnet 10.0.0.0" "$dhcpd_conf_file"
 
57
  then
 
58
    echo >>"$dhcpd_conf_file" "subnet 10.0.0.0 netmask 255.255.0.0 {\
 
59
 option domain-name \"$domain_name\";\
 
60
 option domain-name-servers $nameserver; }"
 
61
  fi
 
62
 
 
63
  "$dhcpd_init_file" restart
 
64
}
 
65
 
 
66
 
 
67
function dhcp_stop()
 
68
{
 
69
  local tmpfile=$(mktemp)
 
70
  grep -v "subnet 10.0.0.0" "$dhcpd_conf_file" >"$tmpfile"
 
71
  if diff "$tmpfile" "$dhcpd_conf_file" >&/dev/null
 
72
  then
 
73
    rm "$tmpfile"
 
74
  else
 
75
    mv "$tmpfile" "$dhcpd_conf_file"
 
76
  fi
 
77
 
 
78
  "$dhcpd_init_file" restart
 
79
}
 
80
 
 
81
 
 
82
op_start() {
 
83
        echo 1 >/proc/sys/net/ipv4/ip_forward
 
84
        iptables -t nat -A POSTROUTING -o ${netdev} -j MASQUERADE
 
85
        [ "$dhcp" != 'no' ] && dhcp_start
 
86
}
 
87
 
 
88
 
 
89
op_stop() {
 
90
        [ "$dhcp" != 'no' ] && dhcp_stop
 
91
        iptables -t nat -D POSTROUTING -o ${netdev} -j MASQUERADE
 
92
}
 
93
 
 
94
 
 
95
show_status() {
 
96
    echo '============================================================'
 
97
    ifconfig
 
98
    echo ' '
 
99
    ip route list
 
100
    echo ' '
 
101
    route -n
 
102
    echo '============================================================'
 
103
 
 
104
}
 
105
 
 
106
case "$command" in
 
107
    start)
 
108
        op_start
 
109
        ;;
 
110
    
 
111
    stop)
 
112
        op_stop
 
113
        ;;
 
114
 
 
115
    status)
 
116
        show_status
 
117
       ;;
 
118
 
 
119
    *)
 
120
       echo "Unknown command: $command" >&2
 
121
       echo 'Valid commands are: start, stop, status' >&2
 
122
       exit 1
 
123
esac