Xen DomU using dynamic IP and hostname

During the last months, I’ve been experimenting with Xen virtualization. An old computer, equipped with a Pentium III running at 700Mhz, 512MB of RAM and an 160GB IDE HDD runs four installations of my favorite Linux distribution, CentOS, one as a Dom0 and the other three as DomUs with 64MB of memory each.

I remember that one of the first things I needed to test was whether all those virtual machines could properly use my DHCP service, located on a separate machine within my LAN, and get a dynamic IP. Furthermore, each time dhclient ran on the Dom0 or any of the DomUs in order to obtain a dynamic IP address, it also sent a name (non FQDN) to the DHCP service. This name represented the name the machine wished to use as a hostname. The DHCP server should generate an FQDN out of that name and update the forward and reverse zones on the DNS server, so that each virtual machine’s FQDN name could properly resolve to the relevant IP address, which had been allocated to the VM by the DHCP service. All this had already been set up in my home LAN since a long time ago, but had never actually tested it with multiple computers or other devices and virtualization definitely provided me with this option.

While I was performing the operating system installations, everything seemed to work as expected. Each virtual machine obtained a dynamic IP address and the DNS records were updated correctly. Glad that things work, I turned off Dom0 after the tests.

At a later time, when I switched Dom0 on again, I noticed that I could not reach any of the virtual machines. Their fully qualified domain names that had been allocated to them by the DHCP service would not resolve to the proper IP. To make it worse, it seemed that the virtual machines had no active leases at all!

Soon I discovered that xendomains, the service responsible for starting and stopping the unprivileged Xen domains (DomU), whenever Domain Zero was shut down, it just saved the domain U’s state for later restoration instead of initiating the DomU’s shutdown procedure. When Dom0 was brought up again, xendomains restored the saved DomU states instead of forcing the domains to boot up normally.

I assume this is like suspending a laptop, which has obtained its IP automatically from a DHCP service within LAN A, moving it to a LAN B, and then bringing it up again within that LAN. It is almost certain that for an arbitrary amount of time you would experience connectivity problems.

In both cases above, at the time the machine resumes operations, the dhclient script (or any other similar script) is not run in order to renew the machine’s IP address.

Fortunately, this behaviour of the xendomains service is configurable. In order to force all DomUs to shutdown, instead of suspending, during Domain 0’s shutdown, all you have to do is make following changes in /etc/sysconfig/xendomains:

Leave empty the XENDOMAINS_SAVE variable. By default, it uses /var/lib/xen/save as the directory where the states of the DomUs are saved. By leaving it empty, the states of the virtual machines are not saved, but they are shut down as usual.


The second modification involves setting the XENDOMAINS_RESTORE variable to false. This determines whether saved domains will be restored from the directory set in the XENDOMAINS_SAVE variable during the Dom0 startup:


From now on, whenever Dom0 starts up, it will force all U domains to boot up normally. Contrariwise, whenever Dom0 shuts down, it will force all DomU to shut down as well. No domain state saving or restoration happens any more.

This means that the network service on each DomU, hence the dhclient script, will run every time the domain U starts up or shuts down, ensuring proper allocation of dynamic IP addresses and hostnames by the DHCP service. Of course, this adds much overhead to both the boot and power off process of Domain Zero (Dom0), but it seems that there is no other way to overcome this problem. If you know of a better way, please let me know.

Xen DomU using dynamic IP and hostname by George Notaras is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Copyright © 2009 - Some Rights Reserved

2 responses on “Xen DomU using dynamic IP and hostname

  1. Jxn Permalink →

    You might want to lower the timeout that the dhcp server let the clients hold their IP number to a time which will be shorter than the time the Dom0 normally will be shut down. Then the timeout will force the dhcpclient on the DomU to renew it’s IP numbers from the dhcp server when they start again.
    It will get some more calls over your local net to the dhcp server, but this should not be a big problem on a local network.

    An alternative way is to reserve an IP address for each of your DomU servers. This is the propper way of handling servers any way. They should have a static address, even if you hand the address out from your dhcp server.
    Your DomU servers will ask for latest used address anyway, and with static address in the dhcp server this address will not be given away to another machine. A final pro with this is that if no dhcp server responds, they will use their lates given information.

    1. George Notaras Post authorPermalink →

      I recall that I had tried using the following settings on the DHCP server:

      default-lease-time 600;
      max-lease-time 900;
      min-lease-time 300;

      Even if the DomU was shutdown for more than 900 seconds, then, when it was brought up again, it would not renew its IP address regardless of the fact that the lease time was over.

      About assigning static IP addresses to each of the DomUs either directly by configuring their networking scripts or through the DHCP server, I agree that this is the Proper Way™ to do this. But, since this exists in my home, I tried to experiment a bit :)

      Thanks for your feedback