Kjetil's Information Center: A Blog About My Projects

Libvirt for KVM Guest

Referring to the Buildroot-based KVM Guest earlier, here is a way to set it up using libvirt: The virtualization API. Using libvirt instead of just QEMU gives additional flexibility, like easier pinning of physical CPUs to VCPUs and so on.

This setup also assumes that the root filesystem is already laid out in /tmp/kvm_guest, to be shared using the 9P protocol. Due to the fact that many files in the root filesystem are owned by root, the whole virtualization also needs to be run as root, or else the host will not allow access to the shared files.

Here is the XML configuration file for libvirt, some paths and the UUID may need to be changed. I named it "kvm_guest.xml":

<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
  <name>kvm_guest</name>
  <uuid>00000000-0000-0000-0000-000000000000</uuid>
  <memory unit='KiB'>131072</memory>
  <currentMemory unit='KiB'>131072</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-2.6'>hvm</type>
    <kernel>/tmp/buildroot/kvmguest/output/images/bzImage</kernel>
    <cmdline>root=/dev/root rw rootfstype=9p rootflags=trans=virtio console=hvc0</cmdline>
  </os>
  <features>
    <acpi/>
  </features>
  <cpu mode='custom' match='exact'>
    <model fallback='allow'>kvm64</model>
  </cpu>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
    <emulator>/usr/bin/qemu-system-x86_64</emulator>
    <controller type='pci' index='0' model='pci-root'/>
    <memballoon model='none'/>
    <console type='pty'>
      <target type='virtio' port='0'/>
    </console>
    <interface type='ethernet'>
      <mac address='00:00:de:ad:be:ef'/>
      <target dev='tap0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </interface>
  </devices>
  <qemu:commandline>
    <qemu:arg value='-chardev'/>
    <qemu:arg value='stdio,id=char0'/>
    <qemu:arg value='-device'/>
    <qemu:arg value='virtio-serial'/>
    <qemu:arg value='-device'/>
    <qemu:arg value='virtconsole,chardev=char0'/>
    <qemu:arg value='-fsdev'/>
    <qemu:arg value='local,id=fs0,path=/tmp/kvm_guest,security_model=none'/>
    <qemu:arg value='-device'/>
    <qemu:arg value='virtio-9p-pci,fsdev=fs0,mount_tag=/dev/root'/>
  </qemu:commandline>
</domain>
          


It can then be started and accessed like so:

sudo /etc/rc.d/rc.libvirt start
sudo virsh create kvm_guest.xml
sudo virsh console kvm_guest
          


The rc.libvirt script is possibly Slackware specific, your system may have a different way to start the libvirt daemons.

Topic: Configuration, by Kjetil @ 04/03-2017, Article Link