Creating QEMU guests from cloud images

2021-01-05 00:00:00 +0000 UTC

Setting up a test environment for KVM Conjurer

This documents the simple scripts I developed to enable easy initial testing of certain views in my KVM Conjurer project. It relies on existing libvirt-powered tools to get the qemu:// hypervisor to a certain state.

Step 1: Obtain (or detect cached) .qcow2 base image

Grab a cloud image from the Debian mirrors if we don’t have it on disk already:

test -f images/debian10.qcow2 || wget -O images/debian10.qcow2 https://cloud.debian.org/images/cloud/buster/20201214-484/debian-10-generic-amd64-20201214-484.qcow2

Step 2: Create .qcow2 image with base image as backing image

In my current test environment script, I create 2 virtual machines, so I need two of these overlay images. Documentation found on the libvirt knowledgebase 1.

qemu-img create -f qcow2 -F qcow2 -o backing_file=images/debian10.qcow2 test1.qcow2
qemu-img create -f qcow2 -F qcow2 -o backing_file=images/debian10.qcow2 test2.qcow2

Step 3: Create ISO with cloud-init configuration

There are two sub-steps here. First, create the configuration files. Second, package them to be read by the virtual CD-ROM drive.

Substep 2.1: Create cloud-init.cfg

You can see the file I created on GitHub here. Full cloud-init documentation can be found on readthedocs at 2.

Optional network configuration

I opted to not include a network configuration in my scripts, since I do not need network configuration for my current test suite. You can find more information about configuring networks in the cloud-init documentation 2 or at Fabian Lee’s blog 3.

Substep 2.2: run cloud-localds to produce .img

Again, I need 2 cloud-init seed images because I am creating two virtual machines for testing. This tool is provided by the package cloud-image-utils (the package name may vary; I am using Debian 10).

cloud-localds -v test-seed1.img cloud-init.cfg
cloud-localds -v test-seed2.img cloud-init.cfg

Step 4: Boot using virt-install

We boot from the virtual overlay harddrive using the --boot hd option, provide the cloud-init configuration through the --disk path=test-seed1.img,device=cdrom option, and avoid being tossed to an interactive console session with --noautoconsole. The other options are standard or discretionary depending on your context.

virt-install --name test1 \
    --memory 1000 --vcpus 1 \
    --boot hd,menu=on \
    --disk path=test-seed1.img,device=cdrom \
    --disk path=test1.qcow2,device=disk \
    --graphics none \
    --noautoconsole \
    --network network:default \
    --os-type=linux --os-variant=debian10

This command is ran twice modulus the seed and overlay image filenames.

Sources

Tags: cloud-init linux