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 ๐
- Download your prefered cloud image which ends with
.img. They are qcow2 images. - Copy the image to
/var/lib/libvirit/images/your-vm-name.img - Resize the disk to the size you want, example 16G.
qemu-img resize /var/lib/libvirt/images/your-vm-name.img 16G - Copy the configuration image to
/var/lib/libvirt/images/user-data.img
Creating the VM ๐
- Create the VM definition. Use
your-vm-name.imgas your primary disk. - Then add a secondary disk with the configuration data,
user-data.img. - Boot the virtual machine. Now it will run
cloud-initon first boot. - After first boot you can remove the
user-data.imgdisk. This can be done live. You need to do this step if you want to use snapshots from the graphical interface.