[libvirt-users] Automatically assign static ipv4 via dhcp to new VMs

Hi guys, I'm running a KVM/libvirt host in a datacenter and got a fancy IPv4 subnet from my provider. I'm able to assign IPs from that subnet via dhcp to my VMs: host01 ~ # virsh net-dumpxml internet <network connections='3'> <name>internet</name> <uuid>37b888cc-510f-46f1-9246-346da96222ed</uuid> <forward dev='enp5s0f0' mode='route'> <interface dev='enp5s0f0'/> </forward> <bridge name='virbr1' stp='on' delay='0'/> <mac address='52:54:00:13:39:d5'/> <ip address='XX.YY.253.2' netmask='255.255.255.224'> <dhcp> <range start='XX.YY.253.4' end='XX.YY.253.30'/> <bootp file='pxelinux.0' server='XX.YY.99.100'/> </dhcp> </ip> </network> What I would like to do: 1) Right know it seems like dnsmasq assigns ip-addresses randomly to VMs, is there any way to assign them in ascending order? 2) IPs are not statically assigned. Is it possible to add something like this automatically at the first VM-start to the xml definition?: <host mac='AA:BB:CC:DD:EE:FF' ip='192.168.122.2' name='foobar'/>

On 10/04/2013 12:09 PM, Tim wrote:
Hi guys,
I'm running a KVM/libvirt host in a datacenter and got a fancy IPv4 subnet from my provider. I'm able to assign IPs from that subnet via dhcp to my VMs:
host01 ~ # virsh net-dumpxml internet <network connections='3'> <name>internet</name> <uuid>37b888cc-510f-46f1-9246-346da96222ed</uuid> <forward dev='enp5s0f0' mode='route'> <interface dev='enp5s0f0'/> </forward> <bridge name='virbr1' stp='on' delay='0'/> <mac address='52:54:00:13:39:d5'/> <ip address='XX.YY.253.2' netmask='255.255.255.224'> <dhcp> <range start='XX.YY.253.4' end='XX.YY.253.30'/> <bootp file='pxelinux.0' server='XX.YY.99.100'/> </dhcp> </ip> </network>
What I would like to do:
1) Right know it seems like dnsmasq assigns ip-addresses randomly to VMs, is there any way to assign them in ascending order?
Whene there is no <host> entry for a particular mac address, dnsmasq decides on the IP address to assign to each client based on some sort of hash of the mac address (and maybe some other info). This makes it as stable as possible without requiring user intervention.
2) IPs are not statically assigned. Is it possible to add something like this automatically at the first VM-start to the xml definition?: <host mac='AA:BB:CC:DD:EE:FF' ip='192.168.122.2' name='foobar'/>
That is exactly what needs to be added to the <dhcp> section of the libvirt network *just before* starting the guest for the first time. If you add it using "virsh net-edit", you will have to net-destroy then net-start the network in order for it to take effect though, and that disconnects all current guests from the network, which you certainly don't want. Fortunately, in libvirt 0.10.0 and later (I think that is the version it was added) you can use the "virsh net-update" command to add net static host entries immediately without restarting the network; something like this: virsh net-update MyGuest --live internet add \ ip-dhcp-host "<host mac='AA:BB:CC:DD:EE:FF' ip='192.168.122.2' name='foobar'/>"

Thanks for the hint with net-update, thats exactly that what I was looking for. Am 07.10.2013 13:10, schrieb Laine Stump:
On 10/04/2013 12:09 PM, Tim wrote:
Hi guys,
I'm running a KVM/libvirt host in a datacenter and got a fancy IPv4 subnet from my provider. I'm able to assign IPs from that subnet via dhcp to my VMs:
host01 ~ # virsh net-dumpxml internet <network connections='3'> <name>internet</name> <uuid>37b888cc-510f-46f1-9246-346da96222ed</uuid> <forward dev='enp5s0f0' mode='route'> <interface dev='enp5s0f0'/> </forward> <bridge name='virbr1' stp='on' delay='0'/> <mac address='52:54:00:13:39:d5'/> <ip address='XX.YY.253.2' netmask='255.255.255.224'> <dhcp> <range start='XX.YY.253.4' end='XX.YY.253.30'/> <bootp file='pxelinux.0' server='XX.YY.99.100'/> </dhcp> </ip> </network>
What I would like to do:
1) Right know it seems like dnsmasq assigns ip-addresses randomly to VMs, is there any way to assign them in ascending order?
Whene there is no <host> entry for a particular mac address, dnsmasq decides on the IP address to assign to each client based on some sort of hash of the mac address (and maybe some other info). This makes it as stable as possible without requiring user intervention.
2) IPs are not statically assigned. Is it possible to add something like this automatically at the first VM-start to the xml definition?: <host mac='AA:BB:CC:DD:EE:FF' ip='192.168.122.2' name='foobar'/>
That is exactly what needs to be added to the <dhcp> section of the libvirt network *just before* starting the guest for the first time. If you add it using "virsh net-edit", you will have to net-destroy then net-start the network in order for it to take effect though, and that disconnects all current guests from the network, which you certainly don't want.
Fortunately, in libvirt 0.10.0 and later (I think that is the version it was added) you can use the "virsh net-update" command to add net static host entries immediately without restarting the network; something like this:
virsh net-update MyGuest --live internet add \ ip-dhcp-host "<host mac='AA:BB:CC:DD:EE:FF' ip='192.168.122.2' name='foobar'/>"

On 10/18/2013 12:31 AM, Tim wrote:
Thanks for the hint with net-update, thats exactly that what I was looking for.
Am 07.10.2013 13:10, schrieb Laine Stump:
Fortunately, in libvirt 0.10.0 and later (I think that is the version it was added) you can use the "virsh net-update" command to add net static host entries immediately without restarting the network; something like this:
virsh net-update MyGuest --live internet add \ ip-dhcp-host "<host mac='AA:BB:CC:DD:EE:FF' ip='192.168.122.2' name='foobar'/>"
I just happened across this message when going through the backlog and noticed that I forgot to add "--config" to the above commandline - without --config, the new entry will only survive until the network is destroyed (e.g. the host is rebooted), after which it will be lost. Assuming you've already added several hosts without using --config, you can transfer them into the permanent network config like this: virsh net-dumpxml MyGuest .... virsh net-edit MyGuest [cut-paste the <host> lines from the dumpxml output into the configuration file] Sorry for the omission (and any surprises it may have caused).
participants (3)
-
Laine Stump
-
Laine Stump
-
Tim