Skip to content

libvirt + cloud-init combined — the end-to-end recipe

A complete libvirt + cloud-init workflow — base image, seed ISO, virt-install, and the post-boot checks an operator runs to confirm the guest configured cleanly.

This article is the end-to-end recipe we use to bring a Linux VM up on a libvirt hypervisor with cloud-init doing the first-boot configuration. It ties together the per-topic articles (cloud-init for VMs deep and virt-install CLI) into the single workflow an operator runs. The output is a guest that boots, picks up its keys and hostname, joins the right network, and is ready for config management — every time, with no manual SSH-in-and-configure step.

How to verify

Confirm you have the prerequisites — libvirt, qemu-kvm, virt-install, cloud-localds — and a cloud image of the OS you want.

which virsh virt-install cloud-localds genisoimage
virsh -c qemu:///system pool-list
ls /var/lib/libvirt/images/jammy-base.qcow2 2>/dev/null || \
  wget -O /var/lib/libvirt/images/jammy-base.qcow2 \
    https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img

qemu-img info /var/lib/libvirt/images/jammy-base.qcow2

cloud-localds (from cloud-image-utils on Debian/Ubuntu) is the helper that builds a properly-labelled NoCloud seed ISO. Without it you can build one by hand with genisoimage -output seed.iso -volid cidata -joliet -rock user-data meta-data.

What’s happening

A cloud image is a qcow2 disk that already contains an OS with cloud-init enabled and configured to look for a NoCloud datasource. When you boot it on libvirt with a seed ISO attached, cloud-init mounts the ISO, reads user-data and meta-data from it, and runs the configuration steps before the first login. The base image stays untouched; per-guest config lives in the seed ISO. You never SSH in to configure — the guest is configured when it finishes booting.

The workflow has three artifacts per guest: the base image (shared, immutable, often a Packer-built golden image), the per-guest disk (a copy or backing-file of the base, resized to the target size), and the per-guest seed ISO (the cloud-init user-data + meta-data). The seed ISO is small (a few KiB) and contains the only host-specific configuration.

The procedure

  1. Stage the base image. Copy or download the upstream cloud image to your libvirt pool. If you use it for many guests, treat it as immutable — link to it as a backing file:

    POOL=/var/lib/libvirt/images
    curl -L -o "$POOL/jammy-base.qcow2" \
      https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img
    qemu-img info "$POOL/jammy-base.qcow2"
  2. Create the per-guest disk as a backing-file qcow2. This is faster than copy and uses much less disk — the per-guest disk only stores the diff from the base:

    NAME=web01
    qemu-img create -f qcow2 -F qcow2 \
      -b "$POOL/jammy-base.qcow2" \
      "$POOL/${NAME}.qcow2" 20G
    qemu-img info "$POOL/${NAME}.qcow2"
  3. Write user-data and meta-data. Tailor these per guest:

    cat > "/tmp/${NAME}-user-data" <<EOF
    #cloud-config
    hostname: ${NAME}
    fqdn: ${NAME}.dc1.example.com
    manage_etc_hosts: true
    
    users:
      - name: ops
        sudo: ALL=(ALL) NOPASSWD:ALL
        shell: /bin/bash
        ssh_authorized_keys:
          - ssh-ed25519 AAAA... ops@bastion
    
    ssh_pwauth: false
    disable_root: true
    package_update: true
    packages:
      - qemu-guest-agent
    runcmd:
      - systemctl enable --now qemu-guest-agent
    EOF
    
    cat > "/tmp/${NAME}-meta-data" <<EOF
    instance-id: iid-${NAME}-$(date +%s)
    local-hostname: ${NAME}
    EOF

    The instance-id is what cloud-init uses to decide whether to rerun on this boot — a unique value per VM ensures it runs the first time and doesn’t on subsequent boots.

  4. Build the seed ISO. cloud-localds does it in one line:

    cloud-localds "$POOL/${NAME}-seed.iso" \
      "/tmp/${NAME}-user-data" \
      "/tmp/${NAME}-meta-data"
    ls -la "$POOL/${NAME}-seed.iso"
  5. Boot the guest with virt-install —import. No installer to run — the base image already has an OS:

    virt-install \
      --name "${NAME}" --memory 2048 --vcpus 2 \
      --disk "$POOL/${NAME}.qcow2",bus=virtio \
      --disk "$POOL/${NAME}-seed.iso",device=cdrom \
      --import --os-variant ubuntu22.04 \
      --network network=br0,model=virtio \
      --graphics none --console pty,target_type=serial \
      --noautoconsole
  6. Watch the first boot. Attach to the console and observe cloud-init:

    virsh console "${NAME}"
    # Ctrl+] to detach

    You’ll see cloud-init’s stage banners. When it gets to a login prompt and you can SSH in as ops, the configuration is complete.

  7. Verify cloud-init succeeded. Inside the guest:

    cloud-init status --long
    sudo cat /run/cloud-init/result.json   # errors: []
    sudo journalctl -u cloud-init-local -u cloud-init -u cloud-config -u cloud-final --no-pager | tail -40

    For pasture diagnostics, cloud-init analyze blame shows which modules took the most time.

  8. Clean up the seed ISO (optional). Once the guest is configured, the seed ISO is no longer needed. Some operators detach it to avoid cloud-init re-running on guest cloning:

    sudo virsh shutdown "${NAME}"
    sudo virsh detach-disk "${NAME}" hdc --config
    sudo virsh start "${NAME}"

Common pitfalls

  • Seed ISO with the wrong label (anything but cidata / CIDATA) means cloud-init’s NoCloud datasource never finds it; the guest boots with default config and no keys. Use cloud-localds, not a hand-rolled mkisofs.
  • A backing-file qcow2 that points at a moved-or-deleted base image refuses to boot with a cryptic error; if you reorganize your image pool, rebuild the per-guest disks.
  • disable_root: true plus ssh_pwauth: false without any ssh_authorized_keys is a guest you cannot log into. Always include a key.
  • The qemu-guest-agent package install is best done in cloud-init so it’s there on first boot — without it, virsh shutdown falls back to ACPI and virsh snapshot --quiesce can’t fsfreeze.
  • Cloning a configured guest without changing the instance-id in cloud-init’s state means the clone thinks it’s already done — either cloud-init clean before cloning or accept that clones inherit the original’s identity.

Stack Harbor runs this exact workflow as Ansible plays: one template generates the user-data per host, a play wraps cloud-localds + virt-install, and the per-guest disks land on the right storage pool with the right metadata. Clients moving from “boot ISO, install, SSH in, configure” to a templated workflow get this picked up as part of Managed Operations. The result is reproducible guests across hypervisors with the per-host config visible in version control.