Building a KVM guest from a graphical wizard is fine once; doing it twenty times is how you discover that the wizard skips half the production-grade options. virt-install is the scriptable equivalent — every choice the GUI hides is an argument. This article walks through the canonical recipes we keep in our build playbook: an Ubuntu cloud image with cloud-init seeding, an ISO-based install for an OS that has no cloud variant, and the flags that show up regardless.
How to verify
virt-install ships with the virtinst package on Ubuntu/Debian and virt-install on RHEL-family distributions. The libvirt daemon must already be running and you need write access to a storage pool.
which virt-install && virt-install --version
virsh -c qemu:///system pool-list --details
virsh -c qemu:///system net-list
osinfo-query os | head
ls /var/lib/libvirt/images/
osinfo-query lists the OS variants virt-install knows; passing the right --osinfo (formerly --os-variant) is what gets you the correct virtio defaults and clock source for the guest. A wrong or missing variant produces a guest that boots but performs poorly.
What’s happening
virt-install is a thin wrapper that asks you the four questions every libvirt domain needs answered — CPU/memory, disk, network, install source — translates them into XML, and hands them to libvirt’s domainCreateXML. Once the domain exists, libvirt owns the lifecycle; virt-install exits. The arguments map almost one-to-one to elements in the domain XML, so an operator who reads libvirt domain XML schema recognizes the structure.
The install source can be an ISO (boots the installer, manual or kickstart), a network URL (PXE-style for distros that publish a netboot tree), a disk image already containing an OS (a cloud image), or --import (use the disk as-is, skip any installer). Cloud images plus cloud-init is the production path because the disk is immutable; the per-host config is injected via a small seed ISO.
The procedure
-
Cloud image with cloud-init — the day-job recipe. Download the image, build a seed ISO with cloud-init user-data, then
--import(see libvirt + cloud-init combined for the deep version):IMG=/var/lib/libvirt/images/jammy-base.qcow2 wget -O "$IMG" https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img cp "$IMG" /var/lib/libvirt/images/web01.qcow2 qemu-img resize /var/lib/libvirt/images/web01.qcow2 20G cat > user-data <<'EOF' #cloud-config hostname: web01 users: - name: ops sudo: ALL=(ALL) NOPASSWD:ALL ssh_authorized_keys: - ssh-ed25519 AAAA... ops@bastion package_update: true EOF cloud-localds /var/lib/libvirt/images/web01-seed.iso user-data virt-install \ --name web01 --memory 2048 --vcpus 2 \ --disk /var/lib/libvirt/images/web01.qcow2,bus=virtio \ --disk /var/lib/libvirt/images/web01-seed.iso,device=cdrom \ --import --os-variant ubuntu22.04 \ --network network=br0,model=virtio \ --graphics none --console pty,target_type=serial \ --noautoconsole -
ISO install — Windows or any distro without a cloud image. Same shape, different source. Add
--cdromand let the installer do its thing. Use VNC for the console because the installer wants a framebuffer:virt-install \ --name win2022 --memory 8192 --vcpus 4 \ --disk size=80,bus=virtio --disk path=/iso/win2022.iso,device=cdrom \ --disk path=/iso/virtio-win.iso,device=cdrom \ --os-variant win2k22 --network network=br0,model=virtio \ --graphics vnc,listen=127.0.0.1 \ --noautoconsoleThe virtio-win ISO is a second CD-ROM so the Windows installer can pick up the storage and network drivers (see virtio drivers for Windows guest).
-
Network install from a netboot tree. When you want kickstart/preseed and no ISO:
virt-install \ --name db01 --memory 4096 --vcpus 2 \ --disk size=40,bus=virtio \ --location 'http://archive.ubuntu.com/ubuntu/dists/jammy/main/installer-amd64/' \ --extra-args 'console=ttyS0,115200n8 url=http://seed.example/preseed.cfg' \ --os-variant ubuntu22.04 --network network=br0,model=virtio \ --graphics none --console pty,target_type=serial -
The flags you change every time.
--name,--memory(MiB),--vcpus, the disk and network. Everything else has sane defaults until you have a specific reason to deviate. -
The flags you set once per host and forget.
--cpu host-passthroughfor max performance,--features kvm_hidden=onto prevent guests from detecting the hypervisor (sometimes needed for licensing),--clock offset=utc,timer.rtc_tickpolicy=catchupfor predictable timekeeping. -
The flags that bite when missing. Without
--osinfo(or--os-variant), virt-install picks an ancient default and the guest ends up with the slow IDE bus and a non-virtio NIC. Without--graphics none --console pty,target_type=serial, a headless cloud image hangs forever waiting for a framebuffer that doesn’t exist. -
Dry-run, print-XML, then commit. When you’re scripting, generate the XML first and inspect it:
virt-install ... --print-xml > web01.xml virsh define web01.xml virsh start web01
Common pitfalls
- Omitting
--osinfoproduces a guest with the IDE bus and no virtio NIC — boots, but I/O is awful and you’ll spend an hour wondering why. - Forgetting
--noautoconsoleon a remote provisioning run leaves the SSH session attached to the serial console; the script never returns. - Mixing
qemu-img resizewith cloud images requires the guest’s filesystem to grow on first boot — that is what cloud-init’sgrowpartandresize2fsmodules do, but only if cloud-init runs. - Using
--locationwithout--extra-args 'console=...'for a headless host means the kernel boots invisibly; you cannot tell why the install stalled. - The default storage pool path needs SELinux/AppArmor labels in good order — putting disks in
/home/$USER/on a RHEL host with SELinux enforcing fails with a permissions error that is actually a label mismatch.
Stack Harbor codifies its virt-install recipes as Ansible templates — one file per OS family, one inventory per environment, one cloud-init template per role. Operators can spin a guest in seconds and the result is identical across hosts and reproducible months later. Clients running KVM as a pet-VM zoo get this build automation when they move onto our platform; the operating model is Managed Operations.