libvirt - AmpereComputing/ampere-lts-kernel---DEPRECATED GitHub Wiki

Install:

dnf -y install bridge-utils libvirt virt-install qemu-kvm virt-top libguestfs-tools libguestfs-tools-c virt-manager 
sudo systemctl enable libvirtd
sudo systemctl start libvirtd

Build

git clone https://github.com/libvirt/libvirt.git
meson setup build -Dsystem=true -Ddriver_qemu=enabled
ninja -C build
ninja -C build install

Run

Command:

virsh define debian12.xml
virsh edit debian12
virsh start debian12
virsh list --all
virsh shutdown debian12
virsh destory debian12
virsh dumpxml debian12
virsh undefine debian12
virsh suspend debian12
virsh resume debian12
virsh qemu-monitor-command --hmp debian12 info block
virsh qemu-monitor-command debian12 '{"execute":"query-block"}'

virsh snapshot

virsh snapshot-create debian12
virsh snapshot-current debian12
virsh snapshot-list debian12
virsh snapshot-revert debian12 1620616838
virsh snapshot-delete debian12 1620616838

Connect VM with virsh console

virsh console debian12

CPU affinity

Reference:Setting KVM processor affinities

virsh vcpupin guest1 0 4
virsh vcpupin guest1 1 5
virsh vcpupin guest1 2 6
virsh vcpupin guest1 3 7

# virsh vcpuinfo guest1
VCPU:           0
CPU:            4
State:          running
CPU time:       32.2s
CPU Affinity:   ----y---
VCPU:           1
CPU:            5
State:          running
CPU time:       16.9s
CPU Affinity:   -----y--
VCPU:           2
CPU:            6
State:          running
CPU time:       11.9s
CPU Affinity:   ------y-
VCPU:           3
CPU:            7
State:          running
CPU time:       14.6s
CPU Affinity:   -------y

Check the effect with htop

debian12.xml

<domain type='kvm'>
  <name>debian12</name>
  <uuid>00364662-13ee-4871-99e8-d3fbdf5d96d9</uuid>
  <metadata>
    <libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
      <libosinfo:os id="http://centos.org/centos/8"/>
    </libosinfo:libosinfo>
  </metadata>
  <memory unit='KiB'>20971520</memory>
  <currentMemory unit='KiB'>20971520</currentMemory>
  <vcpu placement='static'>4</vcpu>
  <os>
    <type arch='aarch64' machine='virt-4.2'>hvm</type>
    <loader readonly='yes' type='pflash'>/usr/share/AAVMF/AAVMF_CODE.fd</loader>
    <nvram>/var/lib/libvirt/qemu/nvram/centos8_perf_00001_VARS.fd</nvram>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <gic version='3'/>
  </features>
  <cpu mode='host-passthrough' check='none'/>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
    <emulator>/usr/bin/qemu-system-aarch64</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/home/dom/Qemu/bin/debian-12-generic-arm64-20230711-1438.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x05' slot='0x00' function='0x0'/>
    </disk>
    <controller type='usb' index='0' model='qemu-xhci' ports='15'>
      <address type='pci' domain='0x0000' bus='0x02' slot='0x00' function='0x0'/>
    </controller>
    <controller type='scsi' index='0' model='virtio-scsi'>
      <address type='pci' domain='0x0000' bus='0x03' slot='0x00' function='0x0'/>
    </controller>
    <controller type='pci' index='0' model='pcie-root'/>
    <controller type='pci' index='1' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='1' port='0x8'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x0' multifunction='on'/>
    </controller>
    <controller type='pci' index='2' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='2' port='0x9'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='pci' index='3' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='3' port='0xa'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <controller type='pci' index='4' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='4' port='0xb'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x3'/>
    </controller>
    <controller type='pci' index='5' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='5' port='0xc'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x4'/>
    </controller>
    <controller type='pci' index='6' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='6' port='0xd'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x5'/>
    </controller>
    <controller type='pci' index='7' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='7' port='0xe'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x6'/>
    </controller>
    <controller type='pci' index='8' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='8' port='0xf'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x7'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
    </controller>
    <interface type='network'>
      <mac address='52:54:00:9d:34:c1'/>
      <source network='default'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target type='system-serial' port='0'>
        <model name='pl011'/>
      </target>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <channel type='unix'>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <audio id='1' type='none'/>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x06' slot='0x00' function='0x0'/>
    </memballoon>
    <rng model='virtio'>
      <backend model='random'>/dev/urandom</backend>
      <address type='pci' domain='0x0000' bus='0x07' slot='0x00' function='0x0'/>
    </rng>
  </devices>
</domain>

Python code

Example:

import libvirt
import time
from xml.etree import ElementTree

conn=libvirt.open("qemu:///system")
for id in conn.listDomainsID():
    domain=conn.lookupByID(id)
    print("name = " + domain.name())
    print("UUID = " + domain.UUIDString())
    print("info = " + str(domain.info()))

    t1=time.time()
    c1=int(domain.info()[4])
    time.sleep(1);
    t2=time.time();
    c2=int(domain.info()[4])
    c_nums=int(domain.info()[3])
    usage=(c2-c1)*100/((t2-t1)*c_nums*1e9)
    print("%s Cpu usage:%f" %(domain.name(),usage))

    domain.setMemoryStatsPeriod(10)
    meminfo=domain.memoryStats()
    print("meminfo = " + str(meminfo))

    tree=ElementTree.fromstring(domain.XMLDesc())
    devices=tree.findall('devices/disk/target')
    for d in devices:
        device=d.get('dev')
        try:
            devinfo=domain.blockInfo(device)
        except libvirt.libvirtError:pass
    print("device = %s,devinfo = %s"%(str(device),str(devinfo)))

conn.close()

Errors:

Build Error:

meson.build:1602:6: ERROR: Problem encountered: YAJL 2 is required to build QEMU driver

Solution:

apt install libyajl-dev

Build Error:

docs/meson.build:176:2: ERROR: Program 'rst2html5 rst2html5.py rst2html5-3' not found or not executable

Solution:

apt-get install python3-docutils

Start Error:

virsh define fedora38.xml
error: failed to connect to the hypervisor
error: Operation not supported: Cannot use direct socket mode if no URI is set

Solution

libvirtd -d
virsh version

Permission Deny Error:

virt-customize -v -a Fedora-Cloud-Base-38-1.6.aarch64.qcow2 --root-password password:123456
virt-customize: error: libguestfs error: could not create appliance through
libvirt.

Try running qemu directly without libvirt using this environment variable:
export LIBGUESTFS_BACKEND=direct

Original error from libvirt: Cannot access storage file
'/root/images/Fedora-Cloud-Base-38-1.6.aarch64.qcow2' (as uid:107,
gid:107): Permission denied [code=38 int1=13]

Solution:

/etc/libvirt/qemu.conf
user = "root"
grout = "root"
systemctl restart libvirtd

libvirtd.service error:

systemctl restart libvirtd                                                                                                                      
Job for libvirtd.service failed because the control process exited with error code.

Solution:

kill the pid of libvirtd

undefine error:

virsh undefine debian12
error: Failed to undefine domain 'debian12'
error: Requested operation is not valid: cannot undefine domain with nvram

Solution:

virsh undefine debian12 --nvram
⚠️ **GitHub.com Fallback** ⚠️