DEV Community

Leon Nunes
Leon Nunes

Posted on

7 2 1

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 ✌️

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

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

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay