Conversation
An encrypted root is a librbd CoW clone of a plaintext template carrying a LUKS2 header. librbd serves the ranges the clone's own objects do not hold from that parent, as plaintext, which is what makes the inherited filesystem readable. That stops the moment an object exists in the clone: one 4 KiB guest write copies up the whole 4 MiB object, and the ranges inside it that the parent never materialised are from then on read through the crypto layer - where zeros decrypted with AES-XTS are not zeros. Before taking the snapshot encrypted roots are cloned from, rewrite the template image from its own cloudstack-base-snap with qemu-img convert -S 0, so the snapshot has no holes left. The snapshot gets a new name, -luks2, so templates prepared before get a dense one too; roots already cloned from the old -luks snapshot keep it. If another host prepares the same template at the same time, its protected snapshot is used instead of failing the deploy. QemuImg grows setWriteZeroRanges() for the -S 0 this needs, since qemu-img skips the source's zero ranges by default.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
An encrypted root disk on RBD is a librbd CoW clone of the (plaintext) template with a LUKS2 header applied to the clone. librbd serves the ranges the clone's own objects don't hold from that parent, as plaintext, which is what makes the inherited filesystem readable at all.
That stops as soon as an object exists in the clone. One 4 KiB guest write copies up the whole 4 MiB object; librbd re-encrypts the parent data it copies, but the ranges the parent never materialised (a sparse template) aren't written at all. The object now exists, so reads of those ranges no longer go to the parent - they go through the crypto layer, and zeros decrypted with AES-XTS are not zeros. The guest gets garbage where it wrote nothing and the template held nothing.
So the corruption is latent and spreads with ordinary writes: the VM boots and runs, and breaks later in whatever file happens to sit in a copied-up object. A mostly empty separate
/bootshows it first, andext4lazyinitwalks the whole disk.The fix
The template is already prepared once for encrypted clones: it gets grown by the LUKS2 header reserve and a protected snapshot is taken to clone from. The only thing wrong is that this snapshot has the template's holes. So right before taking it, the template image is rewritten from its own
cloudstack-base-snapwithqemu-img convert -n -S 0, which writes out every zero range. The snapshot taken after that has nothing left to materialise, and every range of an encrypted clone stays readable. Clones stay thin.cloudstack-base-snap-luks2instead of-luks, so templates that were prepared before (and carry a sparse-lukssnapshot) get a dense one too. Roots already cloned from the old snapshot keep it.cloudstack-base-snap, which is only read.QemuImggetssetWriteZeroRanges()for the-S 0, sinceqemu-img convertskips the source's zero ranges by default.The cost is that a template used for encrypted roots becomes fully allocated in the pool, once per template per pool.
Types of changes
Feature/Enhancement Scale or Bug Severity
Bug Severity
How Has This Been Tested?
3-node KVM cluster (4.23.0.0), RBD primary storage.
Unit tests:
RbdEncryptionTest(10) andLibvirtStorageAdaptorTest(23) pass.RbdEncryptionTestchecks that the copy asks for-S 0, reads from the template's snapshot and writes into the template image itself, unencrypted.QemuImgTestcovers what-S 0does to a local file, but only runs where qemu-img and libvirt are available.On the cluster, with an Ubuntu 24.04 template that already had the old sparse
-lukssnapshot (2.2 of 4 GiB used):cloudstack-base-snap-luks2(~31 s for the 4 GiB template), fully allocated (4.0 of 4.0 GiB). The old-lukssnapshot was left alone.@cloudstack-base-snap-luks2and the VM booted from it.@cloudstack-base-snapand runs.To check the actual bug I made two clones of that template, one from the old
-lukssnapshot and one from the new-luks2, both with a LUKS2 header. In 10 objects that hold data but also have holes, I read a hole sector, wrote 4 KiB of zeros elsewhere in the same object (forcing the copy-up), and read the sector again:Not tested: two hosts preparing the same template at the same time, and templates bigger than 4 GiB.