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.
10
# network-nat (start|stop|status) {VAR=VAL}*
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).
18
#============================================================================
21
. "$dir/xen-script-common.sh"
22
. "$dir/xen-network-common.sh"
27
netdev=${netdev:-eth0}
28
# antispoofing not yet implemented
29
antispoof=${antispoof:-no}
31
# turn on dhcp feature by default if dhcpd is installed
32
if [ -f /etc/dhcpd.conf ]
40
if [ "$dhcp" != 'no' ]
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" ]
46
echo 'Failed to find dhcpd configuration or init file.' >&2
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 }'`
56
if ! grep -q "subnet 10.0.0.0" "$dhcpd_conf_file"
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; }"
63
"$dhcpd_init_file" restart
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
75
mv "$tmpfile" "$dhcpd_conf_file"
78
"$dhcpd_init_file" restart
83
echo 1 >/proc/sys/net/ipv4/ip_forward
84
iptables -t nat -A POSTROUTING -o ${netdev} -j MASQUERADE
85
[ "$dhcp" != 'no' ] && dhcp_start
90
[ "$dhcp" != 'no' ] && dhcp_stop
91
iptables -t nat -D POSTROUTING -o ${netdev} -j MASQUERADE
96
echo '============================================================'
102
echo '============================================================'
120
echo "Unknown command: $command" >&2
121
echo 'Valid commands are: start, stop, status' >&2