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


##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java:
##########
@@ -1808,12 +1972,21 @@ to support snapshots(backuped) as qcow2 files. */
              * To do so it's mandatory that librbd on the system is at least 
0.67.7 (Ceph Dumpling)
              */
             logger.debug("The source image is not RBD, but the destination is. 
We will convert into RBD format 2");
+            String rbdDestPath = destPool.getSourceDir() + "/" + name;
+            // Only an image created by this copy may be removed again when 
the copy fails
+            boolean destCreatedByCopy = false;
             try {
                 srcFile = new QemuImgFile(sourcePath, sourceFormat);
-                String rbdDestPath = destPool.getSourceDir() + "/" + name;
                 String rbdDestFile = 
KVMPhysicalDisk.RBDStringBuilder(destPool, rbdDestPath);
                 destFile = new QemuImgFile(rbdDestFile, destFormat);
 
+                if (rbdImageExists(destPool, name)) {
+                    // qemu-img can't convert into an existing image, and it 
can't be told if it is complete, so don't touch it
+                    logger.error("RBD image {} already exists, it will not be 
overwritten by the copy of {}", rbdDestPath, srcFile.getFileName());
+                    return null;
+                }
+                destCreatedByCopy = true;

Review Comment:
   The existence check and `destCreatedByCopy = true` are not atomic with 
`qemu.convert`. If another agent creates this image after the check but before 
conversion, the conversion can fail and the cleanup path will remove that other 
operation's image, despite the flag claiming ownership. Use an atomic 
create/ownership mechanism (for example, a uniquely named staging image 
followed by an atomic handoff) before deleting on failure.



##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java:
##########
@@ -1268,6 +1356,64 @@ public boolean deletePhysicalDisk(String uuid, 
KVMStoragePool pool, Storage.Imag
         }
     }
 
+    static String maskRbdKey(String message) {
+        return message == null ? null : message.replaceAll("key=[^:\\s]*", 
"key=******");
+    }
+
+    private static Rados connectToCeph(KVMStoragePool pool) throws 
RadosException {
+        Rados r = new Rados(pool.getAuthUserName());
+        r.confSet("mon_host", pool.getSourceHost() + ":" + 
pool.getSourcePort());

Review Comment:
   This connection string always appends `:0` when the storage pool leaves the 
monitor port unspecified. That is not a valid way to express the default Ceph 
monitor port and makes the new stat/resize path fail for such pools; the 
existing RBD URI builder and `RbdEncryption.monSpec` omit the port when it is 
non-positive. Compose `mon_host` with the same omission rule here.



##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java:
##########
@@ -1587,20 +1735,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.

Review Comment:
   When the `cloudstack-base-snap-luks` snapshot already exists, this branch 
skips both the resize and the shrink-back. Images prepared by the previous 
implementation can therefore still have the template itself 16 MiB larger; 
after an upgrade, the first encrypted deployment takes this path and leaves 
subsequent unencrypted base snapshots/clones oversized. The existing reserved 
snapshot needs to be used to restore the template to its data size (or 
otherwise migrated) before proceeding.



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