KVM with Ubuntu cloud image

some configs about create virtual machines with ubuntu cloud image on KVM

install KVM packages

sudo apt install qemu-kvm libvirt-bin virtinst bridge-utils cloud-image-utils

download cloud image

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

convert to qcow2 format and resize

qemu-img convert -O qcow2 xenial-server-cloudimg-amd64-disk1.img xenial-server-cloudimg-amd64-disk1.qcow2
qemu-img resize xenial-server-cloudimg-amd64-disk1.qcow2 +8G

create an image snapshot

qemu-img create -f qcow2 -b xenial-server-cloudimg-amd64-disk1.qcow2 ubuntu.img

cloud image config

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

create config image

cloud-localds config.img config

init

modify your own VMNAME

virt-install --connect=qemu:///system \
 --name VMNAME \
 --ram 2048 \
 --vcpus=2 \
 --os-type=linux \
 --os-variant=ubuntu16.04 \
 --disk ubuntu.img,device=disk,bus=virtio \
 --disk config.img,device=cdrom \
 --graphics none \
 --import

eject config image

eject and modify config do not load that image on next boot

virsh change-media VMNAME hda --eject --config