]> scripts.mit.edu Git - xen.git/blob - scripts/network-nat
Allow memory to balloon to 8GiB
[xen.git] / scripts / network-nat
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
52 function dhcp_start()
53 {
54   if ! grep -q "subnet 10.0.0.0" "$dhcpd_conf_file"
55   then
56     echo >>"$dhcpd_conf_file" "subnet 10.0.0.0 netmask 255.255.0.0 {}"
57   fi
58
59   "$dhcpd_init_file" restart
60 }
61
62
63 function dhcp_stop()
64 {
65   local tmpfile=$(mktemp)
66   grep -v "subnet 10.0.0.0" "$dhcpd_conf_file" >"$tmpfile"
67   if diff "$tmpfile" "$dhcpd_conf_file" >&/dev/null
68   then
69     rm "$tmpfile"
70   else
71     mv "$tmpfile" "$dhcpd_conf_file"
72   fi
73
74   "$dhcpd_init_file" restart
75 }
76
77
78 op_start() {
79         echo 1 >/proc/sys/net/ipv4/ip_forward
80         iptables -t nat -A POSTROUTING -o ${netdev} -j MASQUERADE
81         [ "$dhcp" != 'no' ] && dhcp_start
82 }
83
84
85 op_stop() {
86         [ "$dhcp" != 'no' ] && dhcp_stop
87         iptables -t nat -D POSTROUTING -o ${netdev} -j MASQUERADE
88 }
89
90
91 show_status() {
92     echo '============================================================'
93     ifconfig
94     echo ' '
95     ip route list
96     echo ' '
97     route -n
98     echo '============================================================'
99
100 }
101
102 case "$command" in
103     start)
104         op_start
105         ;;
106     
107     stop)
108         op_stop
109         ;;
110
111     status)
112         show_status
113        ;;
114
115     *)
116        echo "Unknown command: $command" >&2
117        echo 'Valid commands are: start, stop, status' >&2
118        exit 1
119 esac