Cloud-init installation — VM automation first step initialisation of Container on KVM

Kozanoglu Aysad
2 min readOct 29, 2022


Complete script code: if you need to see or educate only the automation script for all steps below then:

What does cloud-init do and what for is it ?

cloud-init is a software package that automates the initialization of cloud instances during system boot. You can configure cloud-init to perform a variety of tasks. Some sample tasks that cloud-init can perform include: Configuring a host name. Installing packages on an instance.

ok Lets go practice

first of all we need a cloud-init img file from the distribution what we want to install automated with cloud-init provisioning.

in this szenario i want to use ubuntu20.04-server cloud-init img

  1. create new subfolder for your needs

goto path default libvirt image path /var/lib/libvirt/images

mkdir /var/lib/libvirt/images/cloud-init-images
cd /var/lib/libvirt/images/cloud-init-images

2. download ubuntu-20.04-server-cloudimg-amd64


if you want to know which versions of cloud-init img’s for ubuntu exists, visit this release site:

3. Convert downloaded cloud-init to your needs

ubuntu-20.04-server-cloudimg-amd64.img file to your local vm storage. For this step we will create a subfolder named on your VMname and converting cloudimg to your newvm as storage and at the resizing space to our needs, in this case 10G (gigabyte)

VMNAME=vm-ubuntu2004-cloudcd /var/lib/libvirt/images/
mkdir $VMNAME
qemu-img convert /var/lib/libvirt/images/cloud-init-images /var/lib/libvirt/images/$VMNAME/root-disk.qcow2qemu-img resize /var/lib/libvirt/images/$VMNAME/root-disk.qcow2 10G

4. preparing cloud-init config to automation

create new file name cloud-init.cfg and inject to file the config on one line

# copy this lines all in one ! before copy change for your needs
# onyl change username, password
echo "#cloud-config
home: /home/$USERNAME
password: $PASSWORD
chpasswd: { expire: False }
hostname: $VMNAME
# configure sshd to allow users logging in using password
# rather than just keys
ssh_pwauth: True
" | sudo tee /var/lib/libvirt/images/$VMNAME/cloud-init.cfg

if you want to know about more parameters to set or change for your need than visit the following site for cloud-init parameters

5. Important step — create iso file named cloud-init.iso with the cloud-init.cfg file we created above

cloud-localds \
/var/lib/libvirt/images/$VMNAME/cloud-init.iso \

6. Last step — Preparing automated Cloud-init installation with KVM virt-install command

virt-install \
--name $VMNAME \
--memory 1024 \
--disk /var/lib/libvirt/images/$VMNAME/root-disk.qcow2,device=disk,bus=virtio \
--disk /var/lib/libvirt/images/$VMNAME/cloud-init.iso,device=cdrom \
--os-type linux \
--os-variant ubuntu19.04 \
--virt-type kvm \
--graphics none \
--network network=default \

Important: if you use KVM network in bridge mode than on line 12 you need to change to your needs like bridge=BRIDGENAME

Videocast for of all step above