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

Laine Stump justsendmailnothingelse at gmail.com
Mon Oct 7 11:10:47 UTC 2013


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'/>"




More information about the libvirt-users mailing list