libvirt: Change dhcp-setup without restarting

10,069

Solution 1

After a diskussion on the Mailinglist I found out: this currently is not possible at all!

Solution 2

The added net-update command in virsh should allow an dhcp-host update without restarting the virtual network (I have not tested it yet).

man virsh:

net-update network command section xml [--parent-index index] [[--live] [--config] | [--current]]

Update the given section of an existing network definition, with the changes optionally taking effect immediately, without needing to destroy and re-start the network.

command is one of "add-first", "add-last", "add" (a synonym for add-last), "delete", or "modify".

section is one of "bridge", "domain", "ip", "ip-dhcp-host", "ip-dhcp-range", "forward", "forward-interface", "forward-pf", "portgroup", "dns-host", "dns-txt", or "dns-srv", each section being named by a concatenation of the xml element hierarchy leading to the element being changed. For example, "ip-dhcp-host" will change a <host> element that is contained inside a <dhcp> element inside an <ip> element of the network.

xml is either the text of a complete xml element of the type being changed (e.g. "<host mac="00:11:22:33:44:55' ip='1.2.3.4'/>", or the name of a file that contains a complete xml element. Disambiguation is done by looking at the first character of the provided text - if the first character is "<", it is xml text, if the first character is not "<", it is the name of a file that contains the xml text to be used.

The --parent-index option is used to specify which of several parent elements the requested element is in (0-based). For example, a dhcp <host> element could be in any one of multiple <ip> elements in the network; if a parent-index isn't provided, the "most appropriate" <ip> element will be selected (usually the only one that already has a <dhcp> element), but if --parent-index is given, that particular instance of <ip> will get the modification.

If --live is specified, affect a running network. If --config is specified, affect the next startup of a persistent network. If --current is specified, affect the current network state. Both --live and --config flags may be given, but --current is exclusive. Not specifying any flag is the same as specifying --current.

Solution 3

The following procedure worked for me with libvirt Version: 0.7.5-5ubuntu27 on a Ubuntu 10.04.4 LTS host:

  virsh net-dumpxml default > default.xml

  $EDITOR default.xml

  virsh net-destroy default
  virsh net-define default.xml
  virsh net-start default

This also works for other networks beside the default network. Just replace the word default in the example with the name of the network.

Solution 4

@pefu's solution did it for CentOS 6.4 too!

Using net-edit in virsh did not update the XML file at /var/lib/libvirt/network/default.xml nor did it change the state of the running network config.

Re-running net-edit showed the edited version even after a reboot but it just was not loaded!

Exporting it to real XML file, editing and reloading it works!

Solution 5

I think libvirt uses dnsmaq for its dhcp server. Maybe you could just restart dnsmasq and all would be well?

Share:
10,069

Related videos on Youtube

theomega
Author by

theomega

Java, Python, FP, Scala SOreadytohelp

Updated on September 17, 2022

Comments

  • theomega
    theomega almost 2 years

    I'm using libvirt and kvm to virtualize my server. I configured libvirt to start a dhcp-server on the bridge-network-interface to give the vm's their ips. Every VM's gets always the same ip based on its mac, this is configured like this:

    <dhcp>
     <range start='10.1.1.2' end='10.1.1.254' />
     <host mac='54:52:00:21:01:ba' name='virstvm' ip='10.1.1.10' />
     <host mac='00:16:36:2d:71:f9' name='secvm' ip='10.1.1.20' />
    </dhcp>
    

    The problem: Whenever I add a new host to the dhcp section I have to restart libvirt-bin which restarts all my vm's. This cant be the solution because it means a downtime every time I add a new Server. Is there a solution?

    • 3dinfluence
      3dinfluence over 14 years
      Good question I figured it was just a matter of recreating the network after the changes to the network using virsh after you edited the xml file like it is with domains. But this appears to not be the case. As virsh net-dumpxml clearly shows that libvirt still has the original file cached.
  • user1202136
    user1202136 over 10 years
    I just tried this one. While the new network configuration did indeed take effect, all VMs lost network connectivity and needed to be rebooted.
  • pefu
    pefu over 10 years
    @user1202136 : What kind of network configuration change did you perform? And what kind of VMs were you using?
  • user1202136
    user1202136 over 10 years
    I used Xen para-virtualized VMs. Regarding, network configuration change, I added a new static DHCP entry.
  • bkzland
    bkzland over 8 years
    I don't know if this is recommended, but I usually just kill the dnsmasq process and start it using the arguments it was running with again.
  • Breno Leitão
    Breno Leitão over 8 years
    Other than restarting the dnsmaq manually, you would prefer to restart the networking service.
  • pefu
    pefu over 8 years
    @Breno Leitão: How exactly? And to which versions of Linux Distribution flavours does your comment apply to?
  • pefu
    pefu over 8 years
    The answer should specify the Linux Distribution and version it applies to. As I've demonstrated below, it is possible in Ubuntu 10.04 LTS and newer.