Saturday, July 9, 2016

KVM libvirt assign static guest IP addresses using DHCP on the virtual machine

http://www.cyberciti.biz/faq/linux-kvm-libvirt-dnsmasq-dhcp-static-ip-address-configuration-for-guest-os

I am using KVM/libvirt on Linux operating system and how do I assign static IP address using dnsmasq dhcpd server for my default virtual network switch?

By default, an instance of dnsmasq dhcpd server is automatically configured and started by libvirt for each virtual network switch needing it. Each virtual network switch can given a range of IP addresses provided to guests through DHCP. The default networking switch uses dnsmasq server.
Fig.01: Libvirt uses a program, dnsmasq for DNS and DHCP for default network.
Fig.01: Libvirt uses a program, dnsmasq for DNS and DHCP for default network.

View the current dnsmasq DHCP configuration

Type the following command to list networks
# virsh net-list
Sample outputs:
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes
To see the default network information, enter:
# virsh net-dumpxml default
Sample outputs:
 connections='2'>
  >default
> >e346291e-f86b-4f2f-a16e-654136441805> mode='nat'> > start='1024' end='65535'/> > > name='virbr0' stp='on' delay='0'/> address='52:54:00:12:fe:35'/> address='192.168.122.1' netmask='255.255.255.0'> > start='192.168.122.100' end='192.168.122.254'/> > > >The DHCP range is between 192.168.122.100 and 192.168.122.254.

How to configure static guest IP addresses on the VM host

First find out your guest VM’s MAC addresses, enter:
# virsh dumpxml {VM-NAME-HERE} | grep -i ' # virsh dumpxml xenial | grep -i '
Sample outputs:

 address='52:54:00:4c:40:1c'/>
Please note down the MAC addresses of the xenial VM that you want to assign static IP addresses.

Edit the default network

Type the following command:
# virsh net-edit default
Find the following section:
 >
start='192.168.122.100' end='192.168.122.254'/>Append the static IP as follows after range:
      mac='52:54:00:4c:40:1c' name='xenial' ip='192.168.122.4'/>
Where,
  1. mac='52:54:00:4c:40:1c' – VMs mac address
  2. name='xenial' – VMs name.
  3. ip='192.168.122.4' – VMs static IP.
Here is my complete file with three static DHCP entries for three VMs:
>
>default> >e346291e-f86b-4f2f-a16e-654136441805> mode='nat'/> name='virbr0' stp='on' delay='0'/> address='52:54:00:12:fe:35'/> address='192.168.122.1' netmask='255.255.255.0'> > start='192.168.122.100' end='192.168.122.254'/> mac='52:54:00:a0:cc:19' name='centos7' ip='192.168.122.2'/> mac='52:54:00:f7:a1:c8' name='puffy' ip='192.168.122.3'/> mac='52:54:00:4c:40:1c' name='xenial' ip='192.168.122.4'/> > > >Restart DHCP service:
# virsh net-destroy default
# virsh net-start default

Sample outputs:
Network default destroyed
Network default started
If you are running the guest/VM called xenial shutdown it:
# virsh shutdown xenial
# /etc/init.d/libvirt-bin restart
# virsh start xenial
# ping -a 192.168.122.4

Sample outputs:
PING 192.168.122.4 (192.168.122.4) 56(84) bytes of data.
64 bytes from 192.168.122.4: icmp_seq=1 ttl=64 time=0.518 ms
64 bytes from 192.168.122.4: icmp_seq=2 ttl=64 time=0.202 ms
64 bytes from 192.168.122.4: icmp_seq=3 ttl=64 time=0.327 ms
^C
--- 192.168.122.4 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.202/0.349/0.518/0.129 ms
Each time the guest or VM called xenial comes online (or rebooted for the kernel update) it will get 192.168.122.4 as static IP address by dnsmasq DHCP server.

No comments:

Post a Comment