Skip to content

KVM: create an encrypted RBD volume with room for its LUKS2 header - #14725

Closed
weizhouapache wants to merge 4 commits into
apache:mainfrom
weizhouapache:24-rbd-encrypted-volume-luks2-header
Closed

weizhouapache wants to merge 4 commits into
apache:mainfrom
weizhouapache:24-rbd-encrypted-volume-luks2-header

Conversation

@weizhouapache

Copy link
Copy Markdown
Member

Description

KVM: create an encrypted RBD volume with room for its LUKS2 header

librbd keeps the LUKS2 header in the RBD image, in front of the data, so an
encrypted RBD volume created with the size of its volume showed the Instance
16 MiB less, while a qemu LUKS volume on NFS, SharedMountPoint or local
storage shows the Instance the size of its volume. Create the image of an
encrypted RBD volume with room for the header instead, so that the Instance
sees the size of the volume on every pool type, and the size of the volume
in the database is what the Instance sees.

The size of the header is the larger of 16 MiB and the object size of the
image (measured: 16 MiB with objects of 1 to 16 MiB, 32 MiB with objects of
32 MiB), so the image is grown when its objects are larger.

- LibvirtStorageAdaptor.createPhysicalDisk() creates an encrypted RBD
  image with the header on top of the size it is given.
- LibvirtResizeVolumeCommandWrapper answers with the size the Instance
  sees for an encrypted RBD volume, so the database no longer gets the
  size of the image (the size of the volume + 16 MiB) after a resize.
- The CoW clone of a template for an encrypted ROOT volume is made from a
  snapshot of the template grown by the header, and the template is shrunk
  back right after the snapshot. The template used to stay 16 MiB larger, so
  the next unencrypted clone of it, if its base snapshot did not exist yet,
  was 16 MiB larger than its template. The clone is grown to the requested
  size from its own size, and the full copy of a template makes room for a
  header larger than 16 MiB as well.

Tests:
- unit test for the size of the header
- the TestVolumeEncryption smoke tests already expect the Instance to see
  the size of the volume

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • Build/CI
  • Test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Feature/Enhancement Scale

  • Major
  • Minor

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

Screenshots (if appropriate):

How Has This Been Tested?

How did you try to break this feature and the system with this change?

When qemu-img convert into an RBD pool failed or timed out, the half-written
image was left behind. Every later copy of the same template then failed with
"rbd create: File exists", so deployments from that template broke on the pool
until the image was removed by hand.

- check whether the destination RBD image exists before converting, and refuse
  to overwrite it, as it may be a complete image used by clones
- remove the image again if the copy created it and then failed
- do not print the Ceph key in the "Failed to convert" error
librbd keeps the LUKS2 header in the RBD image, in front of the data, so an
encrypted RBD volume created with the size of its volume showed the Instance
16 MiB less, while a qemu LUKS volume on NFS, SharedMountPoint or local
storage shows the Instance the size of its volume. Create the image of an
encrypted RBD volume with room for the header instead, so that the Instance
sees the size of the volume on every pool type, and the size of the volume
in the database is what the Instance sees.

The size of the header is the larger of 16 MiB and the object size of the
image (measured: 16 MiB with objects of 1 to 16 MiB, 32 MiB with objects of
32 MiB), so the image is grown when its objects are larger.

- LibvirtStorageAdaptor.createPhysicalDisk() creates an encrypted RBD
  image with the header on top of the size it is given.
- LibvirtResizeVolumeCommandWrapper answers with the size the Instance
  sees for an encrypted RBD volume, so the database no longer gets the
  size of the image (the size of the volume + 16 MiB) after a resize.
- The CoW clone of a template for an encrypted ROOT volume is made from a
  snapshot of the template grown by the header, and the template is shrunk
  back right after the snapshot. The template used to stay 16 MiB larger, so
  the next unencrypted clone of it, if its base snapshot did not exist yet,
  was 16 MiB larger than its template. The clone is grown to the requested
  size from its own size, and the full copy of a template makes room for a
  header larger than 16 MiB as well.

Tests:
- unit test for the size of the header
- the TestVolumeEncryption smoke tests already expect the Instance to see
  the size of the volume
@weizhouapache

Copy link
Copy Markdown
Member Author

@calvix

Thanks for your work on encrypted RBD volumes. Great work!

I have created this PR to allocate an additional 16 MiB or 32 MiB for the LUKS2 header of encrypted volumes on Ceph RBD. This ensures that the guest VM sees the exact volume size reported by CloudStack, keeping the behaviour consistent with encrypted volumes on NFS, Local and StorPool.

With the current implementation, the size visible inside the guest OS is the RBD volume size minus the 16 MiB LUKS2 header.

Please note that this change might affect your PR #14656.

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 4.08163% with 141 lines in your changes missing coverage. Please review.
✅ Project coverage is 20.81%. Comparing base (c5bd11a) to head (d3521a7).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
.../hypervisor/kvm/storage/LibvirtStorageAdaptor.java 4.13% 139 Missing ⚠️
...rce/wrapper/LibvirtResizeVolumeCommandWrapper.java 0.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main   #14725      +/-   ##
============================================
+ Coverage     20.77%   20.81%   +0.03%     
- Complexity    20855    20932      +77     
============================================
  Files          6053     6053              
  Lines        548230   548626     +396     
  Branches      73237    73303      +66     
============================================
+ Hits         113903   114188     +285     
- Misses       420988   421074      +86     
- Partials      13339    13364      +25     
Flag Coverage Δ
uitests 3.86% <ø> (-0.01%) ⬇️
unittests 22.25% <4.08%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wido wido self-assigned this Nov 20, 2026
@wido

wido commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

This is very nice! Looking at the code, I see no objections

@weizhouapache weizhouapache added this to the 24.0 milestone Nov 23, 2026
@weizhouapache
weizhouapache marked this pull request as ready for review November 23, 2026 11:53
Copilot AI lite review requested due to automatic review settings November 23, 2026 11:53
@weizhouapache

Copy link
Copy Markdown
Member Author

@blueorangutan package

@weizhouapache

Copy link
Copy Markdown
Member Author

@nvazquez
I added this to 24.0.
I think it would be better to address the issue before a major release, otherwise it might be difficult to update existing volumes created before this fix

@blueorangutan

Copy link
Copy Markdown

@weizhouapache a [SL] Jenkins job has been kicked to build packages. It will be bundled with no SystemVM templates. I'll keep you posted as I make progress.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Active moderate and critical findings remain in LibvirtStorageAdaptor, including invalid Ceph monitor-port handling, incomplete legacy snapshot migration, and a race that can delete another operation’s image.

3 open issues
What changed in this PR

This PR updates KVM RBD volume handling so librbd LUKS2 encryption reserves space for the encryption header while instances and CloudStack consistently see the requested usable volume size.

Changes:

  • Adds object-size-aware LUKS2 header reservation for encrypted RBD images.
  • Adjusts encrypted RBD resize reporting and template clone/full-copy sizing.
  • Adds RBD key masking, copy cleanup handling, and unit tests for header sizing and error sanitization.
File Description
plugins/​hypervisors/​kvm/​src/​test/​java/​com/​cloud/​hypervisor/​kvm/​storage/​LibvirtStorageAdaptorTest.java Adds unit tests for LUKS2 header-size calculations and Ceph key masking.
plugins/​hypervisors/​kvm/​src/​main/​java/​com/​cloud/​hypervisor/​kvm/​storage/​LibvirtStorageAdaptor.java Implements encrypted RBD header sizing, creation, resize reporting, cloning, copying, cleanup, and error-key masking.
plugins/​hypervisors/​kvm/​src/​main/​java/​com/​cloud/​hypervisor/​kvm/​resource/​wrapper/​LibvirtResizeVolumeCommandWrapper.java Reports the usable size of encrypted RBD volumes after resizing.

Review effort: Lite

Comment on lines +1745 to +1748
if (luksSnapshotSize == null) {
// The template is grown by the size of the header only for the snapshot the encrypted clones are made of,
// and shrunk back right after: its unencrypted clones are made of its base snapshot, which may not exist
// yet, and must keep the size of the template.
@blueorangutan

Copy link
Copy Markdown

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 20128

- leave the monitor port out of mon_host when the pool has none
- copy into a staging RBD image and rename it, so that an image created by
  another operation is neither overwritten nor removed when the copy fails
Copilot AI lite review requested due to automatic review settings November 24, 2026 09:19
@weizhouapache

Copy link
Copy Markdown
Member Author

@blueorangutan package

@blueorangutan

Copy link
Copy Markdown

@weizhouapache a [SL] Jenkins job has been kicked to build packages. It will be bundled with no SystemVM templates. I'll keep you posted as I make progress.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical unresolved race and size-mismatch issues can leave templates oversized and produce clones larger than requested, while the review also lacks the requested smoke-test evidence.

3 open issues
2 previously missed issues

In code that hasn't changed since last review

Low severity Provide evidence of an independent test run

plugins/​hypervisors/​kvm/​src/​main/​java/​com/​cloud/​hypervisor/​kvm/​resource/​wrapper/​LibvirtResizeVolumeCommandWrapper.java:193

The PR description lists tests and notes that the smoke suite expects this behavior, but it does not include evidence of an independent test run. Please add a test-run link and result showing this change was exercised by someone other than the PR author; the tester may also be an approver.

Low severity Link the successful TestVolumeEncryption smoke run

plugins/​hypervisors/​kvm/​src/​main/​java/​com/​cloud/​hypervisor/​kvm/​storage/​LibvirtStorageAdaptor.java:1174

The PR description identifies TestVolumeEncryption as the relevant smoke suite but provides no run result. Please add a link to the successful smoke run and its TestVolumeEncryption result to the PR description.

2 resolved since last review

Review effort: Lite

…he requested size

- when another deployment prepares the reserved snapshot first, restore the template to the size its snapshot tells
  instead of the size this deployment saw
- resize the encrypted clone when its size differs from the requested one, shrinking it if the header of the clone
  is smaller than the one reserved in the snapshot
Copilot AI lite review requested due to automatic review settings November 24, 2026 09:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@weizhouapache
weizhouapache requested a lite review from Copilot November 24, 2026 09:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@blueorangutan

Copy link
Copy Markdown

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✖️ suse15. SL-JID 20149

@weizhouapache

Copy link
Copy Markdown
Member Author

closing this

I have merged this PR into #14426 , and handled volume migration between RBD and other storage pools to ensure that the volume size remains consistent inside the guest OS.

cc @calvix @wido @kiranchavala @harikrishna-patnala

weizhouapache added a commit to weizhouapache/cloudstack that referenced this pull request Nov 25, 2026
…tate

Record the connectToCeph unification, the radosConnect removal, and
the checkstyle fixup cherry-picked onto 4.23.0-ceph as commits 112
to 114, all owned by apache#14426. Also reflect that apache#14725 was closed on
GitHub with its content folded into apache#14426, rather than merged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants