Run cloud-init locally with QEMU

QEMU is a general purpose computer hardware emulator, able to run virtual machines with hardware acceleration, and to emulate the instruction sets of different architectures than the host you are running on.

The NoCloud datasource allows you to provide your own user data, metadata, or network configuration directly to an instance without running a network service. This is helpful for launching local cloud images with QEMU.

Create your configuration

In this example we will create empty network-config and meta-data files, and create a user-data file with a basic cloud-init configuration.

If you have network configuration or metadata to provide, edit the network-config and meta-data files accordingly.

To create the files, run the following commands:

$ touch network-config
$ touch meta-data
$ cat >user-data <<EOF
#cloud-config
password: password
chpasswd:
  expire: False
ssh_pwauth: True
EOF

Create an ISO disk

This disk is used to pass the configuration files to cloud-init. Create it with the genisoimage command:

genisoimage \
    -output seed.img \
    -volid cidata -rational-rock -joliet \
    user-data meta-data network-config

Download a cloud image

Download an Ubuntu image to run:

wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img

Note

This example uses emulated CPU instructions on non-x86 hosts, so it may be slow. To make it faster on non-x86 architectures, one can change the image type and qemu-system-<arch> command name to match the architecture of your host machine.

Boot the image with the ISO attached

Boot the cloud image with our configuration, seed.img, to QEMU:

$ qemu-system-x86_64 -m 1024 -net nic -net user \
    -drive file=jammy-server-cloudimg-amd64.img,index=0,format=qcow2,media=disk \
    -drive file=seed.img,index=1,media=cdrom \
    -machine accel=kvm:tcg

The now-booted image will allow for login using the password provided above.

For additional configuration, you can provide much more detailed configuration in the empty network-config and meta-data files.

Note

See the Networking config Version 2 page for details on the format and config of network configuration. To learn more about the possible values for metadata, check out the NoCloud page.