libvirt template for a x86_64 VM on Ubuntu 24.04

XML config

<domain type='kvm'>
  <name>myvm</name>
  <memory unit='MiB'>4096</memory>
  <vcpu placement='static'>4</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-jammy'>hvm</type>
    <boot dev='hd'/>
    <boot dev='cdrom'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <vmport state='off'/>
  </features>
  <cpu mode='host-passthrough'/>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/bin/qemu-system-x86_64</emulator>
    <controller type='pci' model='pci-root'/>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/opt/myvm.qcow2'/>
      <target dev='vda' bus='virtio'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/tmp/ubuntu.iso'/>
      <target dev='hda' bus='ide'/>
      <readonly/>
    </disk>
    <interface type='network'>
      <mac address='4c:68:29:d5:aa:55'/>
      <source network='default'/>
      <model type='virtio'/>
    </interface>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <input type='keyboard' bus='virtio'/>
    <input type='mouse' bus='virtio'/>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <graphics type='vnc' port='-1' listen='0.0.0.0' autoport='yes'>
       <listen type='address' address='0.0.0.0'/>
    </graphics>
    <filesystem type='mount' accessmode='mapped'>
      <source dir='/home/uli/vm-data'/>
      <target dir='data'/>
    </filesystem>
    <memballoon model='virtio'/>
  </devices>
</domain>

What to modify

How to create the disk image

qemu-img create -f qcow2 /opt/myvm.qcow2 100G

How to start the VM

Ensure that a DVD image to start from is available at /tmp/ubuntu.iso and run:

virsh define /path/to/your/xml/file.xml
virsh start myvm

In order to view the VM’s desktop over VNC, run

virsh vncdisplay myvm