Files
xen/tools/examples/network-nat
T
emellor@leeni.uk.xensource.com 5941ac94f2 Added DHCP server support, configured by a dhcp=yes parameter. This will write
the appropriate entries into dom0's DHCP server configuration file, allowing
that server to provide info for the guest when it starts.

If an IP address is not configured for vif-nat, invent one using the domain
ID and vif ID.

Fix the vif-nat script.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
2005-11-29 14:55:48 +00:00

112 lines
2.3 KiB
Bash

#!/bin/sh
#============================================================================
# Default Xen network start/stop script when using NAT.
# Xend calls a network script when it starts.
# The script name to use is defined in /etc/xen/xend-config.sxp
# in the network-script field.
#
# Usage:
#
# network-nat (start|stop|status) {VAR=VAL}*
#
# Vars:
#
# netdev The gateway interface (default eth0).
# antispoof Whether to use iptables to prevent spoofing (default no).
# dhcp Whether to alter the local DHCP configuration (default no).
#
#============================================================================
dir=$(dirname "$0")
. "$dir/xen-script-common.sh"
. "$dir/xen-network-common.sh"
findCommand "$@"
evalVariables "$@"
netdev=${netdev:-eth0}
# antispoofing not yet implemented
antispoof=${antispoof:-no}
dhcp=${dhcp:-no}
if [ "$dhcp" != 'no' ]
then
dhcpd_conf_file=$(find_dhcpd_conf_file)
dhcpd_init_file=$(find_dhcpd_init_file)
if [ -z "$dhcpd_conf_file" ] || [ -z "$dhcpd_init_file" ]
then
echo 'Failed to find dhcpd configuration or init file.' >&2
exit 1
fi
fi
function dhcp_start()
{
if ! grep -q "subnet 10.0.0.0" "$dhcpd_conf_file"
then
echo >>"$dhcpd_conf_file" "subnet 10.0.0.0 netmask 255.255.0.0 {}"
fi
"$dhcpd_init_file" restart
}
function dhcp_stop()
{
local tmpfile=$(mktemp)
grep -v "subnet 10.0.0.0" "$dhcpd_conf_file" >"$tmpfile"
if diff "$tmpfile" "$dhcpd_conf_file" >&/dev/null
then
rm "$tmpfile"
else
mv "$tmpfile" "$dhcpd_conf_file"
fi
"$dhcpd_init_file" restart
}
op_start() {
echo 1 >/proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o ${netdev} -j MASQUERADE
[ "$dhcp" != 'no' ] && dhcp_start
}
op_stop() {
[ "$dhcp" != 'no' ] && dhcp_stop
iptables -t nat -D POSTROUTING -o ${netdev} -j MASQUERADE
}
show_status() {
echo '============================================================'
ifconfig
echo ' '
ip route list
echo ' '
route -n
echo '============================================================'
}
case "$command" in
start)
op_start
;;
stop)
op_stop
;;
status)
show_status
;;
*)
echo "Unknown command: $command" >&2
echo 'Valid commands are: start, stop, status' >&2
exit 1
esac