Skip to main content

Rethinking My VM Image Pipeline

Today, my pipeline regularly builds images for my disposable VMs. Here's the current process:

  • A dedicated builder VM reads Containerfiles for all VMs, including itself.

  • The builder VM uses podman build to create container images for all VMs.

  • The builder VM then uses bootc-image-builder to create disk images for all VMs.

This process works well, but it has a significant issue: the disk images aren't built efficiently. Unlike container images, which benefit from reusable, cacheable layers, disk images are always built from scratch. This leads to long build times and limited opportunities for data deduplication.

To address this, I've been exploring alternative options to improve the pipeline.

Disk Image Formats and Deduplication

My Current Format: QCOW2

I currently use QCOW2 with compression enabled. This format offers several features like snapshots, compression, and sparse files, which are useful when the underlying filesystem doesn't support them. However, if the filesystem does provide these features, QCOW2 doesn't offer many additional benefits over a simple raw disk image, at least for my use case.

Some notes:

  • Raw disk images are more transparent and widely supported by various tools. It's also much easier to deduplicate raw image files than compressed QCOW2 images. A QCOW2 image without compression should theoretically be similar to a raw image, but I haven't verified this.

  • The compression in QCOW2 is "read-only," meaning new writes aren't compressed. This isn't a problem for me because my VMs are immutable, so the images are rarely written to after creation.

  • bootc-image-builder actually builds the raw image first, before converting it into the QCOW2 format.

The Power of Deduplication

I expect deduplication to be highly effective in my setup because most of my disk images are very similar. There are a few ways to achieve this:

  • Filesystem Deduplication: This approach can be either online (e.g., ZFS) or offline (e.g., btrfs). The filesystem finds duplicate data blocks within files and removes redundant data from the disk. This is a general solution but doesn't necessarily speed up the initial build process.

  • Proactive Deduplication: This method is about building new images by applying small changes to an existing one. For example, you can "fork" an image using cp --reflink a.img b.img or qemu-img create -b a.qcow2 b.qcow2. Only the differences between the two images are stored on disk. This approach can significantly speed up the build process because you are not building from scratch, but it requires images to be built incrementally, not from a clean slate.

Exploring New Approaches

Bootc and In-Place Updates

I'm not currently using bootc images in their intended way. bootc is designed so you build a single disk image once and then update it in-place via a container registry.

I've considered two ways of leveraging this:

  1. I could trust the VMs to update themselves.

  2. I could maintain a "trusted base image" and follow this process:

    • Create a base disk image using bootc. This image is only used for building other images and never for running services.

    • To create the disk image for a specific VM, say VM X, I would first fork the base image using cp --reflink or qemu-img create -b to create X.img.

    • I would then boot a VM using X.img and have it upgrade itself using VM X's specific container image. This container image could either be served from the builder VM via a server or a mounted directory, or it could be built locally within the forked VM, potentially using shared layers from a mounted cache.

This process seems workable, but it's overly complex for my taste. It involves running VMs during the build process, which would require a significant amount of scripting.

Plain Disk Images and In-Place Updates

This is similar to the bootc approach but uses standard raw disk images. Again, I could set up temporary VMs for the build process, but instead of relying on bootc's update mechanism, I would need custom scripts. This starts to resemble tools like cloud-init or Ansible.

A key benefit here is that a VM isn't a strict dependency. I could use something like systemd-nspawn to directly modify the disk images in-place, which would simplify scripting and make the process more reliable. I did attempt this with bootc images, but they don't work well with systemd-nspawn out of the box because the partitions lack the UUIDs that systemd-nspawn requires.

Final Thoughts

Ultimately, I haven't found a truly satisfying improvement to my current build process. While some of these approaches could theoretically improve build times and reduce disk usage, they also make the build pipeline more complicated and less reliable. At this moment, I don't think the trade-off is worth it.

For now, I'll probably just experiment with deduplication on ZFS and reflink on XFS. I noted that ZFS doesn't support reflink (zfs_bclone_enabled) by default, so that's a small hurdle.

This exploration has been an interesting learning experience. I've revisited/discovered some relevant tools:

  • libvirt

  • incus

  • systemd-nspawn

  • cloud-init

  • ansible

  • systemd-volatile-root.service

Sometimes, when I'm writing my own scripts, I feel like I'm building a slimmed-down version of these tools myself. However, I'm not yet convinced that it's the right time to fully switch to them.

[UPDATE]: I learned that this process is called Golden Image and Phoenix Server.

Comments

Popular posts from this blog

A Rocky Migration: Moving from docker-compose to Podman and gVisor

I've been running a few containers for several years. They were all running under rootless Docker with a single user. Initially, I planned to  migrate the containers to VMs , but I couldn't get a stable workflow after about two months of effort. Later,  gVisor caught my attention , and I decided to migrate to Podman with gVisor instead. The new plan is to run each container with  --userns=auto  and use Quadlet for systemd integration. This approach provides better isolation and makes writing firewall rules easier. I'm now close to migrating all my containers. Here are a couple of rough edges I'd like to share. Network Layout I compared  various networking options  and spent a few hours trying the one-interface-per-group approach before giving up. I settled on a single macvlan network and decided to use static IP addresses for my containers. To prevent a randomly assigned IP address from conflicting with a predefined one, I allocated a large IP range for my ...

GameConqueror 0.09 -- Linux Game Hacking Tool

If you are a game hacker If you've been looking for a `CheatEngine for Linux` Then you can't miss this. ============================================== GameConqueror is a game hacking tool for linux, it's written in PyGTK and uses scanmem as its backend. It's supposed to be with most useful features of CheatEngine for Linux. Currently, I've implemented almost everything about scanning, involving variant data types and scan types: Data Types: int{8/16/32/64}, float{32/64}, unknown type(int or float) and unknown width(will try each of them), byte array and string Scan Types: equal, greater, less, changed, unchanged, increased(by), decreased(by) This should be enough for most cases, so I decided to release it at the current status. ============================================= Here's how you can get it PPA (for Ubuntu users) https://launchpad.net/~coolwanglu/+archive/scanmem (I've not test it in 32bit environments or Jaunty, do please inform me if it doe...

Fix Google Security Code

Google Security Code (http://g.co/sc) is one type of 2-step verification. This is particularly useful when security keys and passkeys are not available. I have been using it in my LXC containers, until today I found out that it stopped working. It just kept saying "The code is invalid". It is easy to rule out some factors: The code works on other browsers on my laptop. The code works on other devices that are directly connected to the router. So it appears that Google also checks IP addresses besides the security code. Recently I have IPv6 enabled, so most devices that are directly connected to the router have both IPv4 and IPv6 addresses. But  I only enabled IPv4 for my LXC containers. So I guess when a code is generated by device A and used by device B, Google should be able to check that device A and device B are closely located. But in my case, IPv6 address appears on device A but not on device B, which may look suspicious. To fix the problem, I just needed to disable IPv...