@@ -982,6 +982,7 @@ private Pair<String, String> copyVolumeToSecStorage(VmwareHostService hostServic
982982 throw new Exception (msg );
983983 }
984984
985+ boolean clonedWorkerVMNeeded = true ;
985986 vmMo = hyperHost .findVmOnHyperHost (vmName );
986987 if (vmMo == null || VmwareResource .getVmState (vmMo ) == PowerState .PowerOff ) {
987988 // create a dummy worker vm for attaching the volume
@@ -998,15 +999,18 @@ private Pair<String, String> copyVolumeToSecStorage(VmwareHostService hostServic
998999 String datastoreVolumePath = getVolumePathInDatastore (dsMo , volumePath + ".vmdk" , searchExcludedFolders );
9991000 workerVm .attachDisk (new String [] {datastoreVolumePath }, morDs );
10001001 vmMo = workerVm ;
1002+ clonedWorkerVMNeeded = false ;
1003+ } else {
1004+ vmMo .createSnapshot (exportName , "Temporary snapshot for copy-volume command" , false , false );
10011005 }
10021006
1003- vmMo .createSnapshot (exportName , "Temporary snapshot for copy-volume command" , false , false );
1004-
1005- exportVolumeToSecondaryStroage (vmMo , volumePath , secStorageUrl , destVolumePath , exportName , hostService .getWorkerName (hyperHost .getContext (), cmd , 1 ), _nfsVersion );
1007+ exportVolumeToSecondaryStorage (vmMo , volumePath , secStorageUrl , destVolumePath , exportName , hostService .getWorkerName (hyperHost .getContext (), cmd , 1 ), _nfsVersion , clonedWorkerVMNeeded );
10061008 return new Pair <>(destVolumePath , exportName );
10071009
10081010 } finally {
1009- vmMo .removeSnapshot (exportName , false );
1011+ if (vmMo != null && vmMo .getSnapshotMor (exportName ) != null ) {
1012+ vmMo .removeSnapshot (exportName , false );
1013+ }
10101014 if (workerVm != null ) {
10111015 //detach volume and destroy worker vm
10121016 workerVm .detachAllDisks ();
@@ -1656,8 +1660,8 @@ public Answer createTemplateFromSnapshot(CopyCommand cmd) {
16561660 }
16571661
16581662 // return Pair<String(divice bus name), String[](disk chain)>
1659- private Pair <String , String []> exportVolumeToSecondaryStroage (VirtualMachineMO vmMo , String volumePath , String secStorageUrl , String secStorageDir ,
1660- String exportName , String workerVmName , Integer nfsVersion ) throws Exception {
1663+ private Pair <String , String []> exportVolumeToSecondaryStorage (VirtualMachineMO vmMo , String volumePath , String secStorageUrl , String secStorageDir ,
1664+ String exportName , String workerVmName , Integer nfsVersion , boolean clonedWorkerVMNeeded ) throws Exception {
16611665
16621666 String secondaryMountPoint = mountService .getMountPoint (secStorageUrl , nfsVersion );
16631667 String exportPath = secondaryMountPoint + "/" + secStorageDir + "/" + exportName ;
@@ -1683,14 +1687,18 @@ private Pair<String, String[]> exportVolumeToSecondaryStroage(VirtualMachineMO v
16831687 throw new Exception (msg );
16841688 }
16851689
1686- // 4 MB is the minimum requirement for VM memory in VMware
1687- Pair <VirtualMachineMO , String []> cloneResult =
1688- vmMo .cloneFromCurrentSnapshot (workerVmName , 0 , 4 , volumeDeviceInfo .second (), VmwareHelper .getDiskDeviceDatastore (volumeDeviceInfo .first ()));
1689- clonedVm = cloneResult .first ();
1690- String disks [] = cloneResult .second ();
1691-
1692- clonedVm .exportVm (exportPath , exportName , false , false );
1693- return new Pair <>(volumeDeviceInfo .second (), disks );
1690+ String diskDevice = volumeDeviceInfo .second ();
1691+ String disks [] = vmMo .getCurrentSnapshotDiskChainDatastorePaths (diskDevice );
1692+ if (clonedWorkerVMNeeded ) {
1693+ // 4 MB is the minimum requirement for VM memory in VMware
1694+ Pair <VirtualMachineMO , String []> cloneResult =
1695+ vmMo .cloneFromCurrentSnapshot (workerVmName , 0 , 4 , diskDevice , VmwareHelper .getDiskDeviceDatastore (volumeDeviceInfo .first ()));
1696+ clonedVm = cloneResult .first ();
1697+ clonedVm .exportVm (exportPath , exportName , false , false );
1698+ } else {
1699+ vmMo .exportVm (exportPath , exportName , false , false );
1700+ }
1701+ return new Pair <>(diskDevice , disks );
16941702 } finally {
16951703 if (clonedVm != null ) {
16961704 clonedVm .detachAllDisks ();
@@ -1705,7 +1713,7 @@ private Ternary<String, String, String[]> backupSnapshotToSecondaryStorage(Virtu
17051713 Integer nfsVersion ) throws Exception {
17061714
17071715 String backupUuid = UUID .randomUUID ().toString ();
1708- Pair <String , String []> snapshotInfo = exportVolumeToSecondaryStroage (vmMo , volumePath , secStorageUrl , installPath , backupUuid , workerVmName , nfsVersion );
1716+ Pair <String , String []> snapshotInfo = exportVolumeToSecondaryStorage (vmMo , volumePath , secStorageUrl , installPath , backupUuid , workerVmName , nfsVersion , true );
17091717 return new Ternary <>(backupUuid , snapshotInfo .first (), snapshotInfo .second ());
17101718 }
17111719
0 commit comments