From afbadbf2236102dcca0f35cb78737984114c77cc Mon Sep 17 00:00:00 2001 From: anthony Date: Fri, 22 Jul 2011 19:29:19 -0700 Subject: [PATCH] use the safe way to delete secondary storage directory --- .../xen/resource/CitrixResourceBase.java | 16 ++++---- scripts/vm/hypervisor/xenserver/vmopsSnapshot | 40 ++++--------------- 2 files changed, 14 insertions(+), 42 deletions(-) diff --git a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java index a513c281e51..aa0b4bdf8f9 100644 --- a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java +++ b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java @@ -6000,10 +6000,12 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (uri != null) { String secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath(); - details = deleteSnapshotsDir(conn, dcId, accountId, volumeId, secondaryStorageMountPath); + success = deleteSnapshotsDir(conn, dcId, accountId, volumeId, secondaryStorageMountPath); success = (details != null && details.equals("1")); if (success) { s_logger.debug("Successfully deleted snapshotsDir for volume: " + volumeId); + } else { + s_logger.debug("Failed to delete snapshotsDir for volume: " + volumeId); } } @@ -6163,8 +6165,8 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } protected boolean deleteSecondaryStorageFolder(Connection conn, String remoteMountPath, String folder) { - String result = callHostPlugin(conn, "vmopsSnapshot", "delete_secondary_storage_folder", "remoteMountPath", remoteMountPath, "folder", folder); - return (result != null); + String details = callHostPlugin(conn, "vmopsSnapshot", "delete_secondary_storage_folder", "remoteMountPath", remoteMountPath, "folder", folder); + return (details != null && details.equals("1")); } protected boolean postCreatePrivateTemplate(Connection conn, String templatePath, String tmpltFilename, String templateName, String templateDescription, String checksum, long size, long virtualSize, long templateId) { @@ -6237,12 +6239,8 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return result; } - protected String deleteSnapshotsDir(Connection conn, Long dcId, Long accountId, Long volumeId, String secondaryStorageMountPath) { - // If anybody modifies the formatting below again, I'll skin them - String result = callHostPlugin(conn, "vmopsSnapshot", "deleteSnapshotsDir", "dcId", dcId.toString(), "accountId", accountId.toString(), "volumeId", volumeId.toString(), - "secondaryStorageMountPath", secondaryStorageMountPath); - - return result; + protected boolean deleteSnapshotsDir(Connection conn, Long dcId, Long accountId, Long volumeId, String secondaryStorageMountPath) { + return deleteSecondaryStorageFolder(conn, secondaryStorageMountPath, dcId.toString() + "/" + accountId.toString() + "/" + volumeId.toString()); } diff --git a/scripts/vm/hypervisor/xenserver/vmopsSnapshot b/scripts/vm/hypervisor/xenserver/vmopsSnapshot index 081a7fa1b00..21c263e87dd 100755 --- a/scripts/vm/hypervisor/xenserver/vmopsSnapshot +++ b/scripts/vm/hypervisor/xenserver/vmopsSnapshot @@ -64,7 +64,7 @@ def create_secondary_storage_folder(session, args): # Unmount the local folder umount(local_mount_path) # Remove the local folder - os.system("rm -rf " + local_mount_path) + os.system("rmdir " + local_mount_path) return "1" @@ -84,7 +84,8 @@ def delete_secondary_storage_folder(session, args): # Delete the specified folder folder = local_mount_path + "/" + args["folder"] if os.path.isdir(folder): - os.system("rm -rf " + folder) + os.system("rm -f " + folder + "/*") + os.system("rmdir " + folder) except OSError, (errno, strerror): errMsg = "delete_secondary_storage_folder failed: errno: " + str(errno) + ", strerr: " + strerror util.SMlog(errMsg) @@ -98,7 +99,7 @@ def delete_secondary_storage_folder(session, args): # Unmount the local folder umount(local_mount_path) # Remove the local folder - os.system("rm -rf " + local_mount_path) + os.system("rmdir " + local_mount_path) return "1" @@ -154,7 +155,7 @@ def post_create_private_template(session, args): # Unmount the local folder umount(local_mount_path) # Remove the local folder - os.system("rm -rf " + local_mount_path) + os.system("rmdir " + local_mount_path) return "1" def isfile(path, isISCSI): @@ -303,7 +304,7 @@ def umount(localDir): except CommandException: errMsg = "CommandException raised while trying to umount " + localDir util.SMlog(errMsg) - return + raise xs_errors.XenError(errMsg) util.SMlog("Successfully unmounted " + localDir) return @@ -539,33 +540,6 @@ def deleteSnapshotBackup(session, args): return "1" -def rmtree(path): - if os.path.isdir(path): - try: - shutil.rmtree(path) - except OSError, (errno, strerror): - errMsg = "Error while deleting " + path + " on secondary storage with errno: " + str(errno) + " and strerr: " + strerror + ". Please delete it manually" - util.SMlog(errMsg) - util.SMlog("Successfully deleted " + path) - else: - util.SMlog("Could not find directory with path " + path) - return - -@echo -def deleteSnapshotsDir(session, args): - util.SMlog("Calling deleteSnapshotsDir with " + str(args)) - dcId = args['dcId'] - accountId = args['accountId'] - volumeId = args['volumeId'] - secondaryStorageMountPath = args['secondaryStorageMountPath'] - - backupsDir = mountSnapshotsDir(secondaryStorageMountPath, "snapshots", dcId, accountId, volumeId) - accountDir = os.path.dirname(backupsDir) - util.SMlog("accountDir is " + accountDir) - rmtree(accountDir) - - return "1" - if __name__ == "__main__": - XenAPIPlugin.dispatch({"getVhdParent":getVhdParent, "create_secondary_storage_folder":create_secondary_storage_folder, "delete_secondary_storage_folder":delete_secondary_storage_folder, "post_create_private_template":post_create_private_template, "backupSnapshot": backupSnapshot, "deleteSnapshotBackup": deleteSnapshotBackup, "unmountSnapshotsDir": unmountSnapshotsDir, "deleteSnapshotsDir": deleteSnapshotsDir}) + XenAPIPlugin.dispatch({"getVhdParent":getVhdParent, "create_secondary_storage_folder":create_secondary_storage_folder, "delete_secondary_storage_folder":delete_secondary_storage_folder, "post_create_private_template":post_create_private_template, "backupSnapshot": backupSnapshot, "deleteSnapshotBackup": deleteSnapshotBackup, "unmountSnapshotsDir": unmountSnapshotsDir})