DEV Community

Leon Nunes
Leon Nunes

Posted on

Creating a compressed and minimal KVM template

Note❗: This is purely for self-learning, so that I can learn how to automate my Virtual Machines using Libvirt

Goal

                  virt-sysprep        virt-sysprep
                    (reset)      (add user, keys, logos)
                        |                   |
                dd      v          dd       v
original guest ----> template ---------> copied ------> custom
                                          template         guest 
Enter fullscreen mode Exit fullscreen mode

To start this you first need to install a virtual machine in KVM, either by using virt-install or virt-manager. Automating this is possible via kickstart or by downloading a cloud image, I did it manually.

This was the base image after installation and it was around 2.1 GB on disk and 10GB virtually.

qemu-img info "rocky-linux.qcow2"
file format: qcow2
virtual size: 10 GiB (10737418240 bytes)
disk size: 2.25 GiB
Enter fullscreen mode Exit fullscreen mode

I've used virt-sparsify instead of dd with compress which made the image size 1GB on disk and took around 5 minutes for it to complete

sudo virt-sparsify --tmp=./ --compress rocky-linux.qcow2 rocky-linux-template.qcow2
Enter fullscreen mode Exit fullscreen mode

Final file “rocky-linux-template.qcow2” which is 1GB 🔥.

sudo qemu-img info rocky-linux-template.qcow2
  virtual size: 10 GiB (10737418240 bytes)
  disk size: 0.993 GiB
Enter fullscreen mode Exit fullscreen mode

Now let’s virt-sysprep it

sudo virt-sysprep -a rocky-linux-template.qcow2
Enter fullscreen mode Exit fullscreen mode

Time to virt-sparsify it again

sudo virt-sparsify --tmp=./ --compress rocky-linux-template.qcow2 rocky-linux-g1.qcow2
Enter fullscreen mode Exit fullscreen mode

Time to add the ssh keys and relabel the system

sudo virt-sysprep --format=qcow2 -a rocky-linux-g1.qcow2 \
--ssh-inject root:file:.ssh/id_rsa.pub \
--selinux-relabel --hostname rk-g1-minimal
Enter fullscreen mode Exit fullscreen mode

Creating a guest out of this

  virt-install --connect qemu:///system \
      --name "${VM_NAME}" \
      --disk "${VM_DISK_PATH}",target.bus=virtio \
      --boot hd \
      --memory=1048 \
      --noautoconsole \
      --graphics none \
      --os-variant "${VM_OS_VARIANT}" \
      --vcpus 2 \
      --cpu host \
      --os-type linux \
      --network network=br0,model=virtio \
      --console pty,target_type=serial
Enter fullscreen mode Exit fullscreen mode

And it’s done ✌️

Top comments (2)

Collapse
 
noobyco profile image
Md Moinuddin • Edited

Nedda explore this, getting FOMO... how could I miss exploring this aggghhh!!!

Thanks for steps Leon :)))

Collapse
 
mediocredevops profile image
Leon Nunes

hahahaha you can still do it, at that time nobody used to look at KVM in this way :p