get dhcp working for network-nat
PATCH 1/1 get dhcp working for network-nat
Has anybody successfully used the network-nat script, with or without dhcp? The existing documentation around this is fairly poor, so if you’ve seen it “just work” (or with only some minor tweaks), please reply. I’m not much of a networking expert so I hope my time on this wasn’t wasted.
Assuming not (and supported by google search), perhaps network-nat has been broken for a long time? It certainly has not received much love recently and many mailing list queries about it have gone unanswered.
That said, here's my situation:
Due to limited IP availability in my office, I wanted to configure a setup where dom0 works like an inexpensive home router (e.g. Linksys, Dlink,Belkin), which does NAT and DHCP for multiple attached (or wireless) machines. So I need dom0 to both provide network address translation and to serve dhcp addresses to any VM's launched on it.
It appears that this should work. Or maybe once upon a time it did work. But I had to muck around with it quite a bit and spin a patch to get it work with 3.1 bits. And since the key files haven't been changed in over a year, though untested as of yet, I think the same patch and process is necessary on xen-unstable.
Also, this is tested on a RHEL5-based dom0. I don't know much about dhcp implementations on other distros and haven't tried this recipe on other distros, so your mileage may vary but please do provide feedback if this patch will fail on your favorite distro.
Here's the process I used:
1) Install the dhcp package using your distro's tools
2) Modify /etc/dhcpd.conf to look like this:
ddns-update-style interim;
deny client-updates;
authoratitive;
option domain-name-servers dnsip1 dnsip2;
option ntp-servers ntpip1 ntpip2 ntpip3;
default-lease-time 14400;
max-lease-time 172800;
subnet 10.0.0.0 netmask 255.0.0.0 {} where dnsip1 etc are of course all real IPv4 addresses. The ntp-servers line can be removed if desired.
3) Apply the attached patch. The patch applies to xen-3.1-testing.hg and you will need to apply it to the same files in /etc/xen/scripts.
4) Modify /etc/xen/xend-config.sxp
- comment the line (network-script network-bridge)
- comment the line (vif-script vif-bridge)
- uncomment (network-script network-nat)
- uncomment (vif-script vif-nat)
5) Modify /etc/xen/scripts/network-nat
- change dhcp=${dhcp:-no} from no to yes
6) Modify /etc/xen/scripts/vif-nat
- change dhcp=${dhcp:-no} from no to yes
7) Restart xend (probably "service xend restart")
8) Launch a domain for which the primary interface is configured for dhcp (pv and pvhvm tested so far; don't know if hvm works yet)
- change name= in your config file to something useful first as this will be the machine name given to it by dhcp
9) Try out networking in your VM!
This patch could use some tweaking... the code to add and remove vif’s from /etc/sysconfig/dhcpd needs some work. But I thought it would be good to get others trying this version out. If the patch doesn’t work on your distro, this is a good place to tweak.
Finally, I’m not sure if nat without dhcp is used much. I’d like to propose that the default for dhcp (see steps 5 and 6) be changed from no to yes.
Thanks for any feedback!
Dan
tools/examples/vif-nat +26 -2
@@ -34,9 +34,10 @@ then then dhcpd_conf_file=$(find_dhcpd_conf_file) dhcpd_init_file=$(find_dhcpd_init_file) - if [ -z "$dhcpd_conf_file" ] || [ -z "$dhcpd_init_file" ] + dhcpd_arg_file=$(find_dhcpd_arg_file) + if [ -z "$dhcpd_conf_file" ] || [ -z "$dhcpd_init_file" ] || [ -z "$dhcpd_arg_file" ] then - echo 'Failed to find dhcpd configuration or init file.' >&2 + echo 'Failed to find dhcpd configuration or init or args file.' >&2 exit 1 fi fi
@@ -88,6 +89,27 @@ then hostname="$hostname-$vifid" fi +dhcparg_remove_entry() +{ + local tmpfile=$(mktemp) + sed -e "s/ *$vif//" "$dhcpd_arg_file" >"$tmpfile" + if diff "$tmpfile" "$dhcpd_arg_file" >/dev/null + then + rm "$tmpfile" + else + mv "$tmpfile" "$dhcpd_arg_file" + fi +} + +dhcparg_add_entry() +{ + dhcparg_remove_entry + local tmpfile=$(mktemp) +# this could use some work to handle more cases +# currently causes problems for "service dhcpd status" + sed -e "s/\(D.*\)$/\1 $vif/" "$dhcpd_arg_file" >"$tmpfile" && + mv "$tmpfile" "$dhcpd_arg_file" +} dhcp_remove_entry() {
@@ -99,6 +121,7 @@ dhcp_remove_entry() else mv "$tmpfile" "$dhcpd_conf_file" fi + dhcparg_remove_entry }
@@ -109,6 +132,7 @@ dhcp_up() mac=$(xenstore_read "$XENBUS_PATH/mac") echo >>"$dhcpd_conf_file" \ "host $hostname { hardware ethernet $mac; fixed-address $vif_ip; option routers $router_ip; option host-name \"$hostname\"; }" + dhcparg_add_entry release_lock "vif-nat-dhcp" "$dhcpd_init_file" restart || true }
tools/examples/xen-network-common.sh +5 -0
@@ -87,6 +87,11 @@ find_dhcpd_init_file() find_dhcpd_init_file() { first_file -x /etc/init.d/{dhcp3-server,dhcp,dhcpd} +} + +find_dhcpd_arg_file() +{ + first_file -f /etc/sysconfig/dhcpd } # configure interfaces which act as pure bridge ports:
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 1/1 get dhcp working for network-nat (Take 2)
I fixed up the previous patch to resolve a couple of problems and make a couple of things more automatic. The patch now works on RHEL5, SLES10, and Ubuntu7.04, and applies to xen-unstable. With this patch, it is now (relatively) easy to use dom0 as a DHCP server for all domU's running on the machine.
Please apply.
Thanks,
Dan
P.S. Here's the manual steps required to to get it to work.
1) Install the dhcp package on dom0 using your distro's tools
2) Modify /etc/dhcpd.conf to look like this:
ddns-update-style interim; deny client-updates; authoritative; option domain-name-servers dnsip1 dnsip2; option ntp-servers ntpip1 ntpip2 ntpip3; default-lease-time 14400; max-lease-time 172800;
where dnsip1 etc are of course all real IPv4 addresses. The ntp-servers line can be removed if desired. A subnet line for 10.0.0.0/8 is added automagically by the tools.
3) Modify /etc/xen/xend-config.sxp
- comment the line (network-script network-bridge)
- comment the line (vif-script vif-bridge)
- uncomment (network-script network-nat)
- uncomment (vif-script vif-nat)
4) Reboot or restart xend
5) Launch a domain for which the primary interface is configured for dhcp
- change name= in your config file to something useful first as this will be the machine name given to it by dhcp
6) Try out networking in your VM!
Quoted 76 lines
-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Wednesday, January 16, 2008 3:49 PM
To: '[email protected]'
Subject: [PATCH] get dhcp working for network-natHas anybody successfully used the network-nat script, with or without dhcp? The existing documentation around this is fairly poor, so if you’ve seen it “just work” (or with only some minor tweaks), please reply. I’m not much of a networking expert so I hope my time on this wasn’t wasted.
Assuming not (and supported by google search), perhaps network-nat has been broken for a long time? It certainly has not received much love recently and many mailing list queries about it have gone unanswered.
That said, here's my situation:
Due to limited IP availability in my office, I wanted to configure a setup where dom0 works like an inexpensive home router (e.g. Linksys, Dlink,Belkin), which does NAT and DHCP for multiple attached (or wireless) machines. So I need dom0 to both provide network address translation and to serve dhcp addresses to any VM's launched on it.
It appears that this should work. Or maybe once upon a time it did work. But I had to muck around with it quite a bit and spin a patch to get it work with 3.1 bits. And since the key files haven't been changed in over a year, though untested as of yet, I think the same patch and process is necessary on xen-unstable.
Also, this is tested on a RHEL5-based dom0. I don't know much about dhcp implementations on other distros and haven't tried this recipe on other distros, so your mileage may vary but please do provide feedback if this patch will fail on your favorite distro.
Here's the process I used:
1) Install the dhcp package using your distro's tools
2) Modify/etc/dhcpd.confto look like this:ddns-update-style interim; deny client-updates; authoratitive; option domain-name-servers dnsip1 dnsip2; option ntp-servers ntpip1 ntpip2 ntpip3; default-lease-time 14400; max-lease-time 172800; subnet 10.0.0.0 netmask 255.0.0.0 {}where dnsip1 etc are of course all real IPv4 addresses. The ntp-servers line can be removed if desired.
3) Apply the attached patch. The patch applies to xen-3.1-testing.hg and you will need to apply it to the same files in/etc/xen/scripts.
4) Modify/etc/xen/xend-config.sxp
- comment the line (network-script network-bridge)
- comment the line (vif-script vif-bridge)
- uncomment (network-script network-nat)
- uncomment (vif-script vif-nat)
5) Modify/etc/xen/scripts/network-nat
- change dhcp=${dhcp:-no} from no to yes
6) Modify/etc/xen/scripts/vif-nat
- change dhcp=${dhcp:-no} from no to yes
7) Restart xend (probably "service xend restart")
8) Launch a domain for which the primary interface is configured for dhcp (pv and pvhvm tested so far; don't know if hvm works yet)
- change name= in your config file to something useful first as this will be the machine name given to it by dhcp
9) Try out networking in your VM!This patch could use some tweaking... the code to add and remove vif’s from
/etc/sysconfig/dhcpdneeds some work. But I thought it would be good to get others trying this version out. If the patch doesn’t work on your distro, this is a good place to tweak.
Quoted 7 lines
Finally, I’m not sure if nat without dhcp is used much. I’d like to propose that the default for dhcp (see steps 5 and 6) be changed from no to yes.
Thanks for any feedback!
Dan
tools/examples/network-nat +10 -2
@@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash -x #============================================================================ # Default Xen network start/stop script when using NAT. # Xend calls a network script when it starts.
@@ -27,7 +27,15 @@ netdev=${netdev:-eth0} netdev=${netdev:-eth0} # antispoofing not yet implemented antispoof=${antispoof:-no} -dhcp=${dhcp:-no} + +# turn on dhcp feature by default if dhcpd is installed +if [ -f /etc/dhcpd.conf ] +then + dhcp=${dhcp:-yes} +else + dhcp=${dhcp:-no} +fi + if [ "$dhcp" != 'no' ] then
tools/examples/vif-nat +37 -3
@@ -28,15 +28,22 @@ dir=$(dirname "$0") dir=$(dirname "$0") . "$dir/vif-common.sh" -dhcp=${dhcp:-no} +# turn on dhcp feature by default if dhcpd is installed +if [ -f /etc/dhcpd.conf ] +then + dhcp=${dhcp:-yes} +else + dhcp=${dhcp:-no} +fi 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" ] + dhcpd_arg_file=$(find_dhcpd_arg_file) + if [ -z "$dhcpd_conf_file" ] || [ -z "$dhcpd_init_file" ] || [ -z "$dhcpd_arg_file" ] then - echo 'Failed to find dhcpd configuration or init file.' >&2 + echo 'Failed to find dhcpd configuration or init or args file.' >&2 exit 1 fi fi
@@ -88,6 +95,31 @@ then hostname="$hostname-$vifid" fi +dhcparg_remove_entry() +{ + local tmpfile=$(mktemp) + sed -e "s/$vif //" "$dhcpd_arg_file" >"$tmpfile" + if diff "$tmpfile" "$dhcpd_arg_file" >/dev/null + then + rm "$tmpfile" + else + mv "$tmpfile" "$dhcpd_arg_file" + fi +} + +dhcparg_add_entry() +{ + dhcparg_remove_entry + local tmpfile=$(mktemp) + # handle Red Hat, SUSE, and Debian styles, with or without quotes + sed -e 's/^DHCPDARGS="*\([^"]*\)"*/DHCPDARGS="\1'"$vif "'"/' \ + "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file" + sed -e 's/^DHCPD_INTERFACE="*\([^"]*\)"*/DHCPD_INTERFACE="\1'"$vif "'"/' \ + "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file" + sed -e 's/^INTERFACES="*\([^"]*\)"*/INTERFACES="\1'"$vif "'"/' \ + "$dhcpd_arg_file" >"$tmpfile" && mv "$tmpfile" "$dhcpd_arg_file" + rm -f "$tmpfile" +} dhcp_remove_entry() {
@@ -99,6 +131,7 @@ dhcp_remove_entry() else mv "$tmpfile" "$dhcpd_conf_file" fi + dhcparg_remove_entry }
@@ -109,6 +142,7 @@ dhcp_up() mac=$(xenstore_read "$XENBUS_PATH/mac") echo >>"$dhcpd_conf_file" \ "host $hostname { hardware ethernet $mac; fixed-address $vif_ip; option routers $router_ip; option host-name \"$hostname\"; }" + dhcparg_add_entry release_lock "vif-nat-dhcp" "$dhcpd_init_file" restart || true }
tools/examples/xen-network-common.sh +5 -0
@@ -72,6 +72,11 @@ find_dhcpd_init_file() find_dhcpd_init_file() { first_file -x /etc/init.d/{dhcp3-server,dhcp,dhcpd} +} + +find_dhcpd_arg_file() +{ + first_file -f /etc/sysconfig/dhcpd /etc/defaults/dhcp } # configure interfaces which act as pure bridge ports:
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
RE: [PATCH] get dhcp working for network-nat (Take 2)
I just noticed a small bug in this that could cause some problems, but am not sure how to fix it. It seems that when a domain is "xm destroy'ed" the cleanup method dhcp_remove_entry() is invoked, but this doesn't happen when a domain exits (or reboots) on its own accord. This is especially noticeable for domains that have "on_reboot=restart"... the DHCPDARGS= line in /etc/sysconfig/dhcpd (Redhat-ish systems) grows fairly long.
As far as I can tell, this is just a minor annoyance but I thought I would report it for posterity or in case anyone else sees this turn into a real problem.
Quoted 67 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]]On Behalf Of Dan Magenheimer
Sent: Wednesday, January 23, 2008 4:29 PM
To: [email protected]
Subject: [Xen-devel] [PATCH] get dhcp working for network-nat (Take 2)I fixed up the previous patch to resolve a couple of problems and make a couple of things more automatic. The patch now works on RHEL5, SLES10, and Ubuntu7.04, and applies to xen-unstable. With this patch, it is now (relatively) easy to use dom0 as a DHCP server for all domU's running on the machine.
Please apply.
Signed-off-by Dan Magenheimer <[email protected]>Thanks,
Dan
P.S. Here's the manual steps required to to get it to work.1) Install the dhcp package on dom0 using your distro's tools
2) Modify/etc/dhcpd.confto look like this:ddns-update-style interim; deny client-updates; authoritative; option domain-name-servers dnsip1 dnsip2; option ntp-servers ntpip1 ntpip2 ntpip3; default-lease-time 14400; max-lease-time 172800;where dnsip1 etc are of course all real IPv4 addresses. The ntp-servers line can be removed if desired. A subnet line for 10.0.0.0/8 is added automagically by the tools.
3) Modify/etc/xen/xend-config.sxp
- comment the line (network-script network-bridge)
- comment the line (vif-script vif-bridge)
- uncomment (network-script network-nat)
- uncomment (vif-script vif-nat)
4) Reboot or restart xend
5) Launch a domain for which the primary interface is configured for dhcp
- change name= in your config file to something useful first as this will be the machine name given to it by dhcp
6) Try out networking in your VM!Quoted 8 lines
-----Original Message-----
From: Dan Magenheimer [mailto:[email protected]]
Sent: Wednesday, January 16, 2008 3:49 PM
To: '[email protected]'
Subject: [PATCH] get dhcp working for network-natHas anybody successfully used the network-nat script, with
or without
Quoted 6 lines
dhcp? The existing documentation around this is fairly poor, so if you’ve seen it “just work” (or with only some minor tweaks), please reply. I’m not much of a networking expert so I hope my time on this wasn’t wasted.
Assuming not (and supported by google search), perhaps
network-nat has
Quoted 1 line
been broken for a long time? It certainly has not received
much love
Quoted 71 lines
Quoted 1 line
recently and many mailing list queries about it have gone
unanswered.
Quoted 5 lines
That said, here's my situation:
Due to limited IP availability in my office, I wanted to configure a setup where dom0 works like an inexpensive home router
(e.g. Linksys,
Quoted 62 lines
Dlink,Belkin), which does NAT and DHCP for multiple attached (or wireless) machines. So I need dom0 to both provide network address translation and to serve dhcp addresses to any VM's launched on it.
It appears that this should work. Or maybe once upon a time it did work. But I had to muck around with it quite a bit and spin a patch to get it work with 3.1 bits. And since the key files haven't been changed in over a year, though untested as of yet, I think the same patch and process is necessary on xen-unstable.
Also, this is tested on a RHEL5-based dom0. I don't know much about dhcp implementations on other distros and haven't tried this recipe on other distros, so your mileage may vary but please do provide feedback if this patch will fail on your favorite distro.
Here's the process I used:
1) Install the dhcp package using your distro's tools
2) Modify/etc/dhcpd.confto look like this:ddns-update-style interim; deny client-updates; authoratitive; option domain-name-servers dnsip1 dnsip2; option ntp-servers ntpip1 ntpip2 ntpip3; default-lease-time 14400; max-lease-time 172800; subnet 10.0.0.0 netmask 255.0.0.0 {}where dnsip1 etc are of course all real IPv4 addresses. The ntp-servers line can be removed if desired.
3) Apply the attached patch. The patch applies to xen-3.1-testing.hg and you will need to apply it to the same files in/etc/xen/scripts.
4) Modify/etc/xen/xend-config.sxp
- comment the line (network-script network-bridge)
- comment the line (vif-script vif-bridge)
- uncomment (network-script network-nat)
- uncomment (vif-script vif-nat)
5) Modify/etc/xen/scripts/network-nat
- change dhcp=${dhcp:-no} from no to yes
6) Modify/etc/xen/scripts/vif-nat
- change dhcp=${dhcp:-no} from no to yes
7) Restart xend (probably "service xend restart")
8) Launch a domain for which the primary interface is configured for dhcp (pv and pvhvm tested so far; don't know if hvm works yet)
- change name= in your config file to something useful first as this will be the machine name given to it by dhcp
9) Try out networking in your VM!This patch could use some tweaking... the code to add and remove vif’s from
/etc/sysconfig/dhcpdneeds some work. But I thought it would be good to get others trying this version out. If the patch doesn’t work on your distro, this is a good place to tweak.Finally, I’m not sure if nat without dhcp is used much. I’d like to propose that the default for dhcp (see steps 5 and 6) be changed from no to yes.
Thanks for any feedback!
Dan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel