sidshas03 commented on code in PR #14697:
URL: https://github.com/apache/cloudstack/pull/14697#discussion_r4607924118


##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java:
##########
@@ -3609,4 +3665,231 @@ public KVMStoragePool 
getTemplateSourcePoolUsingMigrationOptions(KVMStoragePool
 
         return 
storagePoolMgr.getStoragePool(migrationOptions.getSrcPoolType(), 
migrationOptions.getSrcPoolUuid());
     }
+
+    /**
+     * Write a marker file before creating a snapshot overlay.
+     * Marker format: 
poolUuid|volumePath|snapshotName|vmName|diskLabel|overlayPath|timestamp
+     */
+    protected void writeSnapshotMarker(KVMStoragePool pool, String volumePath, 
String snapshotName) {
+        writeSnapshotMarker(pool, volumePath, snapshotName, null, null, null);
+    }
+
+    protected void writeSnapshotMarker(KVMStoragePool pool, String volumePath, 
String snapshotName,
+                                       String vmName, String diskLabel, String 
overlayPath) {
+        try {
+            File markerDir = new File(SNAPSHOT_MARKER_DIR);
+            if (!markerDir.exists()) {
+                markerDir.mkdirs();
+            }
+            String markerName = SNAPSHOT_MARKER_PREFIX + pool.getUuid() + "_" 
+ volumePath.replace("/", "_") + "_" + snapshotName;
+            File markerFile = new File(markerDir, markerName);
+            String content = String.join("|",
+                    pool.getUuid(),
+                    volumePath,
+                    snapshotName,
+                    StringUtils.defaultString(vmName),
+                    StringUtils.defaultString(diskLabel),
+                    StringUtils.defaultString(overlayPath),
+                    String.valueOf(System.currentTimeMillis()));
+            try (FileOutputStream fos = new FileOutputStream(markerFile)) {
+                fos.write(content.getBytes());
+            }
+            logger.debug("Wrote snapshot marker: {}", 
markerFile.getAbsolutePath());
+        } catch (IOException e) {
+            logger.warn("Failed to write snapshot marker for volume {} 
snapshot {}: {}", volumePath, snapshotName, e.getMessage());
+        }
+    }
+
+    protected void updateSnapshotMarker(KVMStoragePool pool, String 
volumePath, String snapshotName,
+                                        String vmName, String diskLabel, 
String overlayPath) {
+        writeSnapshotMarker(pool, volumePath, snapshotName, vmName, diskLabel, 
overlayPath);
+    }
+
+    /**
+     * Remove the snapshot marker after successful merge.
+     */
+    protected void removeSnapshotMarker(KVMStoragePool pool, String 
volumePath, String snapshotName) {
+        try {
+            String markerName = SNAPSHOT_MARKER_PREFIX + pool.getUuid() + "_" 
+ volumePath.replace("/", "_") + "_" + snapshotName;
+            File markerFile = new File(SNAPSHOT_MARKER_DIR, markerName);
+            if (markerFile.exists()) {
+                markerFile.delete();
+                logger.debug("Removed snapshot marker: {}", 
markerFile.getAbsolutePath());
+            }
+        } catch (Exception e) {
+            logger.warn("Failed to remove snapshot marker for volume {} 
snapshot {}: {}", volumePath, snapshotName, e.getMessage());
+        }
+    }
+
+    /**
+     * Reconcile incomplete snapshots by checking for marker files on agent 
startup.
+     * If a marker exists, the agent died during a snapshot and the merge may 
not have completed.
+     *
+     * Strategy per marker:
+     * - Overlay gone: snapshot completed or was cleaned up, remove marker
+     * - VM still running on overlay: blockcommit --active --pivot back to base
+     * - VM already on base: overlay is orphaned, remove marker
+     * - Domain gone, overlay exists: offline qemu-img commit
+     * - Base modified since marker: cannot safely commit, alert and keep 
marker
+     */
+    protected void reconcileSnapshotMarkers() {
+        File markerDir = new File(SNAPSHOT_MARKER_DIR);
+        if (!markerDir.exists() || !markerDir.isDirectory()) {
+            return;
+        }
+
+        File[] markers = markerDir.listFiles((dir, name) -> 
name.startsWith(SNAPSHOT_MARKER_PREFIX));
+        if (markers == null || markers.length == 0) {
+            return;
+        }
+
+        logger.info("Found {} snapshot marker(s) to reconcile", 
markers.length);
+        for (File marker : markers) {
+            reconcileSingleMarker(marker);
+        }
+    }
+
+    protected void reconcileSingleMarker(File marker) {
+        try {
+            String content = new 
String(Files.readAllBytes(marker.toPath())).trim();
+            String[] parts = content.split("\\|", -1);
+            if (parts.length < 3) {
+                logger.error("Invalid snapshot marker format in {}: {}", 
marker.getName(), content);
+                return;
+            }
+
+            String poolUuid = parts[0];
+            String volumePath = parts[1];
+            String snapshotName = parts[2];
+            String vmName = parts.length > 3 ? parts[3] : "";
+            String diskLabel = parts.length > 4 ? parts[4] : "";
+            String overlayPath = parts.length > 5 ? parts[5] : "";
+
+            if (StringUtils.isEmpty(overlayPath)) {
+                overlayPath = resource.getSnapshotTemporaryPath(volumePath, 
snapshotName);
+            }
+
+            logger.warn("Reconciling incomplete snapshot: pool={}, volume={}, 
snapshot={}, vm={}, disk={}, overlay={}",
+                    poolUuid, volumePath, snapshotName, vmName, diskLabel, 
overlayPath);
+
+            File overlayFile = new File(overlayPath);
+            if (!overlayFile.exists()) {
+                logger.info("Overlay file {} no longer exists. Snapshot may 
have completed or been cleaned up. Removing marker.", overlayPath);
+                marker.delete();
+                return;
+            }
+
+            if (StringUtils.isNotEmpty(vmName)) {
+                if (reconcileWithRunningDomain(vmName, diskLabel, volumePath, 
overlayPath, marker)) {
+                    return;
+                }
+            }
+
+            reconcileOffline(volumePath, overlayPath, marker);
+
+        } catch (IOException e) {
+            logger.error("Failed to read snapshot marker {}: {}", 
marker.getName(), e.getMessage());
+        }
+    }
+
+    private boolean reconcileWithRunningDomain(String vmName, String 
diskLabel, String volumePath,
+                                               String overlayPath, File 
marker) {
+        try {
+            Connect conn = LibvirtConnection.getConnection();
+            Domain vm = conn.domainLookupByName(vmName);
+            if (vm == null) {
+                return false;
+            }
+
+            String xmlDesc = vm.getXMLDesc(0);
+            LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
+            parser.parseDomainXML(xmlDesc);
+
+            for (DiskDef disk : parser.getDisks()) {
+                String currentSource = disk.getDiskPath();
+                if (currentSource != null && 
currentSource.equals(overlayPath)) {
+                    logger.warn("VM [{}] disk [{}] is still on overlay [{}]. 
Attempting blockcommit to merge back to base [{}].",
+                            vmName, disk.getDiskLabel(), overlayPath, 
volumePath);
+
+                    String actualDiskLabel = StringUtils.isNotEmpty(diskLabel) 
? diskLabel : disk.getDiskLabel();
+                    try {
+                        resource.mergeDeltaIntoBaseFile(vm, actualDiskLabel, 
volumePath, null, true, "reconcile", null, conn);
+                        logger.info("Successfully reconciled overlay [{}] back 
to base [{}] for VM [{}].", overlayPath, volumePath, vmName);
+                        marker.delete();
+                        return true;
+                    } catch (Exception e) {
+                        logger.error("Failed to blockcommit overlay [{}] for 
VM [{}]: {}. Manual intervention required.",
+                                overlayPath, vmName, e.getMessage(), e);
+                        return true;
+                    }
+                }
+
+                if (currentSource != null && currentSource.equals(volumePath)) 
{
+                    logger.info("VM [{}] disk [{}] is already on base [{}]. 
Overlay [{}] is orphaned. Removing marker.",
+                            vmName, disk.getDiskLabel(), volumePath, 
overlayPath);
+                    marker.delete();

Review Comment:
   Yes, makes sense. If the VM has already pivoted back to the base, the 
overlay is just lying around wasting disk space. Added the cleanup! we check if 
the overlay exists and delete it before removing the marker.



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