Ubuntu cloud images with KVM

ยท 197 words ยท 1 minute read

Ubuntu are using cloud-init for their cloud images. This can be used in combination with libvirtd and kvm to pre-configure your virtual machine at boot.

Prepare cloud-init configuration ๐Ÿ”—

You can specify much more configuration than we’ve done in this example. This configuration sets the password ubuntu for the default user for the image. On Ubuntu images this user is ubuntu.

cat >user-data <<EOF
#cloud-config
password: ubuntu
chpasswd: { expire: False }
ssh_pwauth: True
EOF

Create a disk image out of the configuration.

cloud-localds user-data.img user-data

Virtual machine using cloud-init ๐Ÿ”—

Placing the files ๐Ÿ”—

  1. Download your prefered cloud image which ends with .img. They are qcow2 images.
  2. Copy the image to /var/lib/libvirit/images/your-vm-name.img
  3. Resize the disk to the size you want, example 16G.
    qemu-img resize /var/lib/libvirt/images/your-vm-name.img 16G
    
  4. Copy the configuration image to /var/lib/libvirt/images/user-data.img

Creating the VM ๐Ÿ”—

  • Create the VM definition. Use your-vm-name.img as your primary disk.
  • Then add a secondary disk with the configuration data, user-data.img.
  • Boot the virtual machine. Now it will run cloud-init on first boot.
  • After first boot you can remove the user-data.img disk. This can be done live. You need to do this step if you want to use snapshots from the graphical interface.