Whilst building a KVM-based virtualisation server on CentOS 6.4, I noticed that the default network created during the installation of the KVM/QEMU packages (192.168.122.0/24) allocated the entire address space to DHCP, as can be seen below in virt-manager (click to enlarge):
All of the configuration fields are read-only, and I wanted to reduce the DHCP scope so that I could build statically-addressed virtual servers without the hassle of creating static DHCP reservations for them. This scope can also be confirmed by looking at the current running dnsmasq configuration (dnsmasq is used by KVM/QEMU to manage DHCP):
|
1 2 3 |
$ ps -ef | grep '[d]nsmasq.*\.122\.' |\ > sed 's/^.*\(--dhcp-range.*254\).*/\1/' --dhcp-range 192.168.122.2,192.168.122.254 |
The powerful virsh shell can be used to perform this modification and directly alter the XML configuration that backs the virtualisation platform.
To start, run virtsh net-edit <network-name> to fire up your $EDITOR:
|
1 |
# virsh net-edit default |
Prior to making any modifications, my configuration was as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<network> <name>default</name> <uuid>9d015fbd-768e-4d95-b500-5537b51ae820</uuid> <forward mode='nat'/> <bridge name='virbr0' stp='on' delay='0' /> <mac address='52:54:00:FF:D4:30'/> <ip address='192.168.122.1' netmask='255.255.255.0'> <dhcp> <range start='192.168.122.2' end='192.168.122.254' /> </dhcp> </ip> </network> |
Next, reduce the DHCP range as required - my modified configuration is as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<network> <name>default</name> <uuid>9d015fbd-768e-4d95-b500-5537b51ae820</uuid> <forward mode='nat'/> <bridge name='virbr0' stp='on' delay='0' /> <mac address='52:54:00:FF:D4:30'/> <ip address='192.168.122.1' netmask='255.255.255.0'> <dhcp> <range start='192.168.122.128' end='192.168.122.254' /> </dhcp> </ip> </network> |
Once you quit your editor, you’ll see a message as follows, indicating that the change has been made:
|
1 |
Network default XML configuration edited. |
If at this stage you dump the configuration with virsh net-dumpxml <network-name> you will see that the changes have NOT yet been applied:
|
1 2 |
# virsh net-dumpxml default | grep range <range start='192.168.122.2' end='192.168.122.254' /> |
To apply the changes, restart the network with virsh net-destroy <network-name> and virsh net-start <network-name>:
|
1 2 3 4 5 |
# virsh net-destroy default Network default destroyed # virsh net-start default Network default started |
Now, a dump of the XML shows that the changes have been applied
|
1 2 |
# virsh net-dumpxml default | grep range <range start='192.168.122.128' end='192.168.122.254' /> |
as does another ps on the dnsmasq process:
|
1 2 3 |
$ ps -ef | grep '[d]nsmasq.*\.122\.' |\ > sed 's/^.*\(--dhcp-range.*254\).*/\1/' --dhcp-range 192.168.122.128,192.168.122.254 |
Now, launching virt-manager shows that the desired DHCP scope is in effect:
Another case of command line winning over the GUI. virsh is a VERY powerful command, and there will be more articles on its usage as I dabble more with KVM-based virtualisation.

