Libvirt setup

Cover Image for Libvirt setup

To take advantage of full networking features in libvirt, a connection to system driver is required. There are multiple ways to specify hypervisor uri when running virsh. Default uri can be changed by setting environment variable LIBVIRT_DEFAULT_URI=qemu:///system. Alternatively, virsh accepts --connect or -c parameter where default uri can be overridden. It's also possible to connect to different hypervisors including remote connections1.

On Fedora libvirt and supporting tools installed via dnf

$ sudo dnf install @virtualization python-libvirt python-lxml -y


Connection to system hypervisor requires root level access and authentication

$ LIBVIRT_DEFAULT_URI=qemu:///system virsh uri
==== AUTHENTICATING FOR org.libvirt.unix.manage ====
System policy prevents management of local virtualized systems
Authenticating as: ansible

Lbvirt supports interacting with access control framework using polkit driver2, libvirt-daemon package in fedora comes with polkit rule allowing members of libvirt group to manage libvirt

$ sudo cat /usr/share/polkit-1/rules.d/50-libvirt.rules
// Allow any user in the 'libvirt' group to connect to system libvirtd
// without entering a password.

polkit.addRule(function(action, subject) {
    if ( == "org.libvirt.unix.manage" &&
        subject.isInGroup("libvirt")) {
        return polkit.Result.YES;

Alternatively, rule can be setup implementing %wheel ALL=(ALL) NOPASSWD:ALL sudo functionality

$ sudo cat /usr/share/polkit-1/rules.d/49-nopasswd_global.rules
polkit.addRule(function(action, subject) {
    if (subject.isInGroup("wheel")) {
        return polkit.Result.YES;
$ sudo systemctl restart polkit

With polkit setup correctly, libvirt will not require password authentication

$ LIBVIRT_DEFAULT_URI=qemu:///system virsh uri

Hypervisor running as qemu user will need search access to the home dir. Permissions can be granted using setfacl

$ setfacl -m u:qemu:rx $HOME  


Network manager comes with dnsmasq plugin, when setup, dns queries are resolved by dnsmasq instance running locally. This is useful to resolve hosts in libvirt network3.

Setup network manager to use dnsmasq plugin

$ cat /etc/NetworkManager/conf.d/01-use-dnsmasq.conf

Setup dnsmasq plugin to resolve names in cosmos.local domain

$ cat /etc/NetworkManager/dnsmasq.d/01-libvirt_dnsmasq.conf
# Add name server for cosmos.local domain

NetworkManager should be restarted via systemctl restart NetworkManager for changes to take affect.

Setup libvirt network

$ cat /tmp/network.xml
  <forward mode='nat'>
      <port start='1024' end='65535'/>
  <bridge name='virbr1'/>
  <domain name='cosmos.local' localOnly='yes'/>
  <ip address='' netmask=''>
      <range start='' end=''/>
$ virsh -c qemu:///system net-define /tmp/network.xml
$ virsh -c qemu:///system net-autostart cosmos
$ virsh -c qemu:///system net-start cosmos
$ virsh -c qemu:///system net-list


Location is set to store virtual machine images and storage pool is defined in hypervisor

$ sudo mkdir -p /storage/images
$ sudo chown 1000:1000 /storage/images

$ cat storage.xml
<pool type='dir'>
$ virsh -c qemu:///system pool-define storage.xml
$ virsh -c qemu:///system pool-autostart images
$ virsh -c qemu:///system pool-start images
$ virsh -c qemu:///system pool-list

Creating virtual machines

Create 30G disk image in images storage pool backed by f32-base image which was built for base-image article

$ qemu-img create \
    -f qcow2 \
    -b /storage/images/f32-base.vda.x86_64.qcow2 \
    /storage/images/jupiter.vda.x86_64.qcow2 \

Setup static dhcp address on cosmos network for jupiter host

$ virsh -c qemu:///system net-update cosmos add-last ip-dhcp-host \
    "<host mac='52:54:00:da:70:35' name='jupiter' ip=''/>" \
    --live --config --parent-index 0

Import jupiter vm

$ virt-install \
    --name jupiter \
    --ram 1024 \
    --os-type linux \
    --os-variant fedora31 \
    --graphics none \
    --network network=cosmos,mac=52:54:00:da:70:35 \
    --disk=/storage/images/jupiter.vda.x86_64.qcow2,bus=virtio,format=qcow2 \

Now libvirt is setup to allow connections to system hypervisor by the members of the wheel group.

1 :
2 :
3 :