Copilot commented on code in PR #14725:
URL: https://github.com/apache/cloudstack/pull/14725#discussion_r4677909276


##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java:
##########
@@ -1587,20 +1736,35 @@ private KVMPhysicalDisk 
createEncryptedRootCoWClone(KVMPhysicalDisk template, KV
             ioContext = radosConnection.ioCtxCreate(destPool.getSourceDir());
             rbdClient = new Rbd(ioContext);
             templateImage = rbdClient.open(template.getName());
-            boolean luksSnapshotExists = false;
+            Long luksSnapshotSize = null;
             for (RbdSnapInfo snapshotInfo : templateImage.snapList()) {
                 if (luksReservedSnapshotName.equals(snapshotInfo.name)) {
-                    luksSnapshotExists = true;
+                    luksSnapshotSize = snapshotInfo.size;
                     break;
                 }
             }
-            if (!luksSnapshotExists) {
-                templateImage.resize(template.getVirtualSize() + 
LUKS2_HEADER_RESERVE_BYTES);
-                templateImage.snapCreate(luksReservedSnapshotName);
-                templateImage.snapProtect(luksReservedSnapshotName);
+            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.
+                RbdImageInfo templateInfo = templateImage.stat();
+                luksSnapshotSize = templateInfo.size + 
getRbdLuks2HeaderSize(templateInfo.obj_size);
+                templateImage.resize(luksSnapshotSize);
+                try {
+                    templateImage.snapCreate(luksReservedSnapshotName);
+                    templateImage.snapProtect(luksReservedSnapshotName);
+                } finally {
+                    templateImage.resize(templateInfo.size);

Review Comment:
   The check-then-resize/snapshot/shrink sequence is not serialized per 
template. Two concurrent deployments can both observe that this snapshot is 
absent; if the second observes the first deployment's temporary growth, it can 
calculate a larger snapshot size, lose `snapCreate` to the first deployment, 
and then its `finally` block restores the template to the temporarily observed 
size rather than the original size. That leaves the template permanently 
oversized and can make later unencrypted clones larger than the template. 
Protect this preparation with a per-template/distributed lock or re-read and 
restore the actual original size after a failed race.



##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java:
##########
@@ -1617,7 +1781,7 @@ private KVMPhysicalDisk 
createEncryptedRootCoWClone(KVMPhysicalDisk template, KV
             }
         }
         formatRbdImageEncryption(destPool, newUuid, passphrase);
-        if (disk.getVirtualSize() > template.getVirtualSize()) {
+        if (disk.getVirtualSize() > cloneSize) {

Review Comment:
   The reserved snapshot is sized from the template's object size, but the 
clone is created with the destination `rbdOrder` (currently 4 MiB objects). For 
example, a 32 MiB-object template produces a snapshot with `templateSize + 32 
MiB`, while the clone's LUKS2 header is only 16 MiB; `cloneSize` therefore 
becomes `templateSize + 16 MiB`, and this `>` check does not resize it back. 
The guest then sees a volume larger than requested. Resize whenever 
`disk.getVirtualSize() != cloneSize`, passing `allowShrink = 
disk.getVirtualSize() < cloneSize`, or otherwise make the snapshot reserve 
match the clone's object size.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to