From 9be635367e87a37671064936de964e90d736b052 Mon Sep 17 00:00:00 2001 From: anthony Date: Tue, 10 Jan 2012 16:45:01 -0800 Subject: [PATCH] bug 10363 : cleanup vhd in secondary storage if creating private template fails status 10363 : resolved fixed Conflicts: core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java --- .../xen/resource/CitrixResourceBase.java | 64 +++++++++++-------- .../create_privatetemplate_from_snapshot.sh | 13 +++- scripts/vm/hypervisor/xenserver/vmopspremium | 3 +- 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java index 4ad4f052784..bc850872e29 100755 --- a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java +++ b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java @@ -2256,22 +2256,25 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } String createTemplateFromSnapshot(Connection conn, String templatePath, String snapshotPath, int wait) { + String tmpltLocalDir = UUID.randomUUID().toString(); String results = callHostPluginAsync(conn, "vmopspremium", "create_privatetemplate_from_snapshot", - wait, "templatePath", templatePath, "snapshotPath", snapshotPath); - + wait, "templatePath", templatePath, "snapshotPath", snapshotPath, "tmpltLocalDir", tmpltLocalDir); + String errMsg = null; if (results == null || results.isEmpty()) { - String msg = "create_privatetemplate_from_snapshot return null"; - s_logger.warn(msg); - throw new CloudRuntimeException(msg); - } - String[] tmp = results.split("#"); - String status = tmp[0]; - if (status.equals("0")) { - return results; + errMsg = "create_privatetemplate_from_snapshot return null"; } else { - s_logger.warn(results); - throw new CloudRuntimeException(results); + String[] tmp = results.split("#"); + String status = tmp[0]; + if (status.equals("0")) { + return results; + } else { + errMsg = "create_privatetemplate_from_snapshot failed due to " + tmp[1]; + } } + String source = "cloud_mount/" + tmpltLocalDir; + killCopyProcess(conn, source); + s_logger.warn(errMsg); + throw new CloudRuntimeException(errMsg); } boolean killCopyProcess(Connection conn, String nameLabel) { @@ -3143,7 +3146,6 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } finally { if (task != null) { try { - task.cancel(conn); task.destroy(conn); } catch (Exception e1) { s_logger.warn("unable to destroy task(" + task.toString() + ") on host(" + _host.uuid + ") due to ", e1); @@ -5844,11 +5846,12 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe String details = null; SR tmpltSR = null; boolean result = false; + String secondaryStorageMountPath = null; + String installPath = null; try { - URI uri = new URI(secondaryStoragePoolURL); - String secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath(); - String installPath = "template/tmpl/" + accountId + "/" + templateId; + secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath(); + installPath = "template/tmpl/" + accountId + "/" + templateId; if( !createSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath)) { details = " Filed to create folder " + installPath + " in secondary storage"; s_logger.warn(details); @@ -5879,16 +5882,18 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir: " + tmpltURI); } installPath = installPath + "/" + tmpltFilename; - return new CreatePrivateTemplateAnswer(cmd, true, null, installPath, virtualSize, physicalSize, tmpltUUID, ImageFormat.VHD); - } catch (XenAPIException e) { - details = "Creating template from volume " + volumeUUID + " failed due to " + e.getMessage(); - s_logger.error(details, e); - } catch (Exception e) { - details = "Creating template from volume " + volumeUUID + " failed due to " + e.getMessage(); - s_logger.error(details, e); - } finally { - // Remove the secondary storage SR removeSR(conn, tmpltSR); + tmpltSR = null; + return new CreatePrivateTemplateAnswer(cmd, true, null, installPath, virtualSize, physicalSize, tmpltUUID, ImageFormat.VHD); + } catch (Exception e) { + if (tmpltSR != null) { + removeSR(conn, tmpltSR); + } + if ( secondaryStorageMountPath != null) { + deleteSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath); + } + details = "Creating template from volume " + volumeUUID + " failed due to " + e.toString(); + s_logger.error(details, e); } return new CreatePrivateTemplateAnswer(cmd, result, details); } @@ -5934,10 +5939,12 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe // By default, assume failure String details = null; boolean result = false; + String secondaryStorageMountPath = null; + String installPath = null; try { URI uri = new URI(secondaryStorageUrl); - String secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath(); - String installPath = "template/tmpl/" + accountId + "/" + newTemplateId; + secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath(); + installPath = "template/tmpl/" + accountId + "/" + newTemplateId; if( !createSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath)) { details = " Filed to create folder " + installPath + " in secondary storage"; s_logger.warn(details); @@ -5965,6 +5972,9 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe installPath = installPath + "/" + tmpltFilename; return new CreatePrivateTemplateAnswer(cmd, true, null, installPath, virtualSize, physicalSize, tmpltUuid, ImageFormat.VHD); } catch (Exception e) { + if (secondaryStorageMountPath != null) { + deleteSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath); + } details = "Creating template from snapshot " + backedUpSnapshotUuid + " failed due to " + e.toString(); s_logger.error(details, e); } diff --git a/scripts/vm/hypervisor/xenserver/create_privatetemplate_from_snapshot.sh b/scripts/vm/hypervisor/xenserver/create_privatetemplate_from_snapshot.sh index fc21909feaf..c09f4b003db 100755 --- a/scripts/vm/hypervisor/xenserver/create_privatetemplate_from_snapshot.sh +++ b/scripts/vm/hypervisor/xenserver/create_privatetemplate_from_snapshot.sh @@ -4,7 +4,7 @@ #set -x usage() { - printf "Usage: %s [vhd file in secondary storage] [template directory in secondary storage] \n" $(basename $0) + printf "Usage: %s [vhd file in secondary storage] [template directory in secondary storage] [template local dir] \n" $(basename $0) } options='tcp,soft,timeo=133,retrans=1' cleanup() @@ -40,6 +40,15 @@ else templateurl=$2 fi +if [ -z $3 ]; then + usage + echo "3#no template local dir" + exit 0 +else + tmpltLocalDir=$3 +fi + + snapshotdir=/var/run/cloud_mount/$(uuidgen -r) mkdir -p $snapshotdir if [ $? -ne 0 ]; then @@ -54,7 +63,7 @@ if [ $? -ne 0 ]; then exit 0 fi -templatedir=/var/run/cloud_mount/$(uuidgen -r) +templatedir=/var/run/cloud_mount/$tmpltLocalDir mkdir -p $templatedir if [ $? -ne 0 ]; then templatedir="" diff --git a/scripts/vm/hypervisor/xenserver/vmopspremium b/scripts/vm/hypervisor/xenserver/vmopspremium index f305a5a27d5..2f43d8c4f01 100755 --- a/scripts/vm/hypervisor/xenserver/vmopspremium +++ b/scripts/vm/hypervisor/xenserver/vmopspremium @@ -33,8 +33,9 @@ def forceShutdownVM(session, args): def create_privatetemplate_from_snapshot(session, args): templatePath = args['templatePath'] snapshotPath = args['snapshotPath'] + tmpltLocalDir = args['tmpltLocalDir'] try: - cmd = ["bash", "/opt/xensource/bin/create_privatetemplate_from_snapshot.sh",snapshotPath, templatePath] + cmd = ["bash", "/opt/xensource/bin/create_privatetemplate_from_snapshot.sh",snapshotPath, templatePath, tmpltLocalDir] txt = util.pread2(cmd) except: txt = '10#failed'