Skip to content

Commit f952e76

Browse files
committed
Log the original VM name instead of the cloned VM in case of cloning
1 parent 03ed3cf commit f952e76

File tree

4 files changed

+38
-24
lines changed

4 files changed

+38
-24
lines changed

core/src/main/java/com/cloud/agent/api/ConvertInstanceCommand.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
public class ConvertInstanceCommand extends Command {
2424

2525
private RemoteInstanceTO sourceInstance;
26+
private String originalVMName;
2627
private Hypervisor.HypervisorType destinationHypervisorType;
2728
private DataStoreTO conversionTemporaryLocation;
2829
private String templateDirOnConversionLocation;
@@ -35,19 +36,24 @@ public ConvertInstanceCommand() {
3536
}
3637

3738
public ConvertInstanceCommand(RemoteInstanceTO sourceInstance, Hypervisor.HypervisorType destinationHypervisorType, DataStoreTO conversionTemporaryLocation,
38-
String templateDirOnConversionLocation, boolean checkConversionSupport, boolean exportOvfToConversionLocation) {
39+
String templateDirOnConversionLocation, boolean checkConversionSupport, boolean exportOvfToConversionLocation, String sourceVMName) {
3940
this.sourceInstance = sourceInstance;
4041
this.destinationHypervisorType = destinationHypervisorType;
4142
this.conversionTemporaryLocation = conversionTemporaryLocation;
4243
this.templateDirOnConversionLocation = templateDirOnConversionLocation;
4344
this.checkConversionSupport = checkConversionSupport;
4445
this.exportOvfToConversionLocation = exportOvfToConversionLocation;
46+
this.originalVMName = sourceVMName;
4547
}
4648

4749
public RemoteInstanceTO getSourceInstance() {
4850
return sourceInstance;
4951
}
5052

53+
public String getOriginalVMName() {
54+
return originalVMName;
55+
}
56+
5157
public Hypervisor.HypervisorType getDestinationHypervisorType() {
5258
return destinationHypervisorType;
5359
}

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConvertInstanceCommandWrapper.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,38 @@ public Answer execute(ConvertInstanceCommand cmd, LibvirtComputingResource serve
5858
DataStoreTO conversionTemporaryLocation = cmd.getConversionTemporaryLocation();
5959
long timeout = (long) cmd.getWait() * 1000;
6060
String extraParams = cmd.getExtraParams();
61+
String originalVMName = cmd.getOriginalVMName(); // For logging purposes, as the sourceInstance may have been cloned
6162

6263
if (cmd.getCheckConversionSupport() && !serverResource.hostSupportsInstanceConversion()) {
6364
String msg = String.format("Cannot convert the instance %s from VMware as the virt-v2v binary is not found. " +
6465
"Please install virt-v2v%s on the host before attempting the instance conversion.", sourceInstanceName, serverResource.isUbuntuOrDebianHost()? ", nbdkit" : "");
65-
logger.info(String.format("(%s) %s", sourceInstanceName, msg));
66+
logger.info(String.format("(%s) %s", originalVMName, msg));
6667
return new Answer(cmd, false, msg);
6768
}
6869

6970
if (!areSourceAndDestinationHypervisorsSupported(sourceHypervisorType, destinationHypervisorType)) {
7071
String err = destinationHypervisorType != Hypervisor.HypervisorType.KVM ?
7172
String.format("The destination hypervisor type is %s, KVM was expected, cannot handle it", destinationHypervisorType) :
7273
String.format("The source hypervisor type %s is not supported for KVM conversion", sourceHypervisorType);
73-
logger.error(String.format("(%s) %s", sourceInstanceName, err));
74+
logger.error(String.format("(%s) %s", originalVMName, err));
7475
return new Answer(cmd, false, err);
7576
}
7677

7778
final KVMStoragePoolManager storagePoolMgr = serverResource.getStoragePoolMgr();
7879
KVMStoragePool temporaryStoragePool = getTemporaryStoragePool(conversionTemporaryLocation, storagePoolMgr);
7980

8081
logger.info(String.format("(%s) Attempting to convert the instance %s from %s to KVM",
81-
sourceInstanceName, sourceInstanceName, sourceHypervisorType));
82+
originalVMName, sourceInstanceName, sourceHypervisorType));
8283
final String temporaryConvertPath = temporaryStoragePool.getLocalPath();
8384

8485
String ovfTemplateDirOnConversionLocation;
8586
String sourceOVFDirPath;
8687
boolean ovfExported = false;
8788
if (cmd.getExportOvfToConversionLocation()) {
88-
String exportInstanceOVAUrl = getExportInstanceOVAUrl(sourceInstance);
89+
String exportInstanceOVAUrl = getExportInstanceOVAUrl(sourceInstance, originalVMName);
8990
if (StringUtils.isBlank(exportInstanceOVAUrl)) {
9091
String err = String.format("Couldn't export OVA for the VM %s, due to empty url", sourceInstanceName);
91-
logger.error(String.format("(%s) %s", sourceInstanceName, err));
92+
logger.error(String.format("(%s) %s", originalVMName, err));
9293
return new Answer(cmd, false, err);
9394
}
9495

@@ -99,10 +100,10 @@ public Answer execute(ConvertInstanceCommand cmd, LibvirtComputingResource serve
99100
ovfTemplateDirOnConversionLocation = UUID.randomUUID().toString();
100101
temporaryStoragePool.createFolder(ovfTemplateDirOnConversionLocation);
101102
sourceOVFDirPath = String.format("%s/%s/", temporaryConvertPath, ovfTemplateDirOnConversionLocation);
102-
ovfExported = exportOVAFromVMOnVcenter(exportInstanceOVAUrl, sourceOVFDirPath, noOfThreads, sourceInstanceName, timeout);
103+
ovfExported = exportOVAFromVMOnVcenter(exportInstanceOVAUrl, sourceOVFDirPath, noOfThreads, originalVMName, timeout);
103104
if (!ovfExported) {
104105
String err = String.format("Export OVA for the VM %s failed", sourceInstanceName);
105-
logger.error(String.format("(%s) %s", sourceInstanceName, err));
106+
logger.error(String.format("(%s) %s", originalVMName, err));
106107
return new Answer(cmd, false, err);
107108
}
108109
sourceOVFDirPath = String.format("%s%s/", sourceOVFDirPath, sourceInstanceName);
@@ -112,39 +113,39 @@ public Answer execute(ConvertInstanceCommand cmd, LibvirtComputingResource serve
112113
}
113114

114115
logger.info(String.format("(%s) Attempting to convert the OVF %s of the instance %s from %s to KVM",
115-
sourceInstanceName, ovfTemplateDirOnConversionLocation, sourceInstanceName, sourceHypervisorType));
116+
originalVMName, ovfTemplateDirOnConversionLocation, sourceInstanceName, sourceHypervisorType));
116117

117118
final String temporaryConvertUuid = UUID.randomUUID().toString();
118119
boolean verboseModeEnabled = serverResource.isConvertInstanceVerboseModeEnabled();
119120

120121
boolean cleanupSecondaryStorage = false;
121122
try {
122-
boolean result = performInstanceConversion(sourceInstanceName, sourceOVFDirPath, temporaryConvertPath, temporaryConvertUuid,
123+
boolean result = performInstanceConversion(originalVMName, sourceOVFDirPath, temporaryConvertPath, temporaryConvertUuid,
123124
timeout, verboseModeEnabled, extraParams, serverResource);
124125
if (!result) {
125126
String err = String.format(
126127
"The virt-v2v conversion for the OVF %s failed. Please check the agent logs " +
127128
"for the virt-v2v output. Please try on a different kvm host which " +
128129
"has a different virt-v2v version.",
129130
ovfTemplateDirOnConversionLocation);
130-
logger.error(String.format("(%s) %s", sourceInstanceName, err));
131+
logger.error(String.format("(%s) %s", originalVMName, err));
131132
return new Answer(cmd, false, err);
132133
}
133134
return new ConvertInstanceAnswer(cmd, temporaryConvertUuid);
134135
} catch (Exception e) {
135136
String error = String.format("Error converting instance %s from %s, due to: %s",
136137
sourceInstanceName, sourceHypervisorType, e.getMessage());
137-
logger.error(String.format("(%s) %s", sourceInstanceName, error), e);
138+
logger.error(String.format("(%s) %s", originalVMName, error), e);
138139
cleanupSecondaryStorage = true;
139140
return new Answer(cmd, false, error);
140141
} finally {
141142
if (ovfExported && StringUtils.isNotBlank(ovfTemplateDirOnConversionLocation)) {
142143
String sourceOVFDir = String.format("%s/%s", temporaryConvertPath, ovfTemplateDirOnConversionLocation);
143-
logger.debug("({}) Cleaning up exported OVA at dir: {}", sourceInstanceName, sourceOVFDir);
144+
logger.debug("({}) Cleaning up exported OVA at dir: {}", originalVMName, sourceOVFDir);
144145
FileUtil.deletePath(sourceOVFDir);
145146
}
146147
if (cleanupSecondaryStorage && conversionTemporaryLocation instanceof NfsTO) {
147-
logger.debug("({}) Cleaning up secondary storage temporary location", sourceInstanceName);
148+
logger.debug("({}) Cleaning up secondary storage temporary location", originalVMName);
148149
storagePoolMgr.deleteStoragePool(temporaryStoragePool.getType(), temporaryStoragePool.getUuid());
149150
}
150151
}
@@ -166,15 +167,15 @@ protected boolean areSourceAndDestinationHypervisorsSupported(Hypervisor.Hypervi
166167
supportedInstanceConvertSourceHypervisors.contains(sourceHypervisorType);
167168
}
168169

169-
private String getExportInstanceOVAUrl(RemoteInstanceTO sourceInstance) {
170+
private String getExportInstanceOVAUrl(RemoteInstanceTO sourceInstance, String originalVMName) {
170171
String url = null;
171172
if (sourceInstance.getHypervisorType() == Hypervisor.HypervisorType.VMware) {
172-
url = getExportOVAUrlFromRemoteInstance(sourceInstance);
173+
url = getExportOVAUrlFromRemoteInstance(sourceInstance, originalVMName);
173174
}
174175
return url;
175176
}
176177

177-
private String getExportOVAUrlFromRemoteInstance(RemoteInstanceTO vmwareInstance) {
178+
private String getExportOVAUrlFromRemoteInstance(RemoteInstanceTO vmwareInstance, String originalVMName) {
178179
String vcenter = vmwareInstance.getVcenterHost();
179180
String username = vmwareInstance.getVcenterUsername();
180181
String password = vmwareInstance.getVcenterPassword();
@@ -185,7 +186,7 @@ private String getExportOVAUrlFromRemoteInstance(RemoteInstanceTO vmwareInstance
185186
String encodedUsername = encodeUsername(username);
186187
String encodedPassword = encodeUsername(password);
187188
if (StringUtils.isNotBlank(path)) {
188-
logger.info("({}) VM path: {}", vm, path);
189+
logger.info("({}) VM path: {}", originalVMName, path);
189190
return String.format("vi://%s:%s@%s/%s/%s/%s",
190191
encodedUsername, encodedPassword, vcenter, datacenter, path, vm);
191192
}
@@ -204,7 +205,7 @@ protected void sanitizeDisksPath(List<LibvirtVMDef.DiskDef> disks) {
204205
private boolean exportOVAFromVMOnVcenter(String vmExportUrl,
205206
String targetOvfDir,
206207
int noOfThreads,
207-
String sourceInstanceName, long timeout) {
208+
String originalVMName, long timeout) {
208209
Script script = new Script("ovftool", timeout, logger);
209210
script.add("--noSSLVerify");
210211
if (noOfThreads > 1) {
@@ -213,14 +214,14 @@ private boolean exportOVAFromVMOnVcenter(String vmExportUrl,
213214
script.add(vmExportUrl);
214215
script.add(targetOvfDir);
215216

216-
String logPrefix = String.format("(%s) export ovf", sourceInstanceName);
217+
String logPrefix = String.format("(%s) export ovf", originalVMName);
217218
OutputInterpreter.LineByLineOutputLogger outputLogger = new OutputInterpreter.LineByLineOutputLogger(logger, logPrefix);
218219
script.execute(outputLogger);
219220
int exitValue = script.getExitValue();
220221
return exitValue == 0;
221222
}
222223

223-
protected boolean performInstanceConversion(String sourceInstanceName, String sourceOVFDirPath,
224+
protected boolean performInstanceConversion(String originalVMName, String sourceOVFDirPath,
224225
String temporaryConvertFolder,
225226
String temporaryConvertUuid,
226227
long timeout, boolean verboseModeEnabled, String extraParams,
@@ -240,7 +241,7 @@ protected boolean performInstanceConversion(String sourceInstanceName, String so
240241
script.add(extraParams);
241242
}
242243

243-
String logPrefix = String.format("(%s) virt-v2v ovf source: %s progress", sourceInstanceName, sourceOVFDirPath);
244+
String logPrefix = String.format("(%s) virt-v2v ovf source: %s progress", originalVMName, sourceOVFDirPath);
244245
OutputInterpreter.LineByLineOutputLogger outputLogger = new OutputInterpreter.LineByLineOutputLogger(logger, logPrefix);
245246
script.execute(outputLogger, serverResource.getConvertInstanceEnv());
246247
int exitValue = script.getExitValue();

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConvertInstanceCommandWrapperTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ private ConvertInstanceCommand getConvertInstanceCommand(RemoteInstanceTO remote
128128
Mockito.when(cmd.getWait()).thenReturn(14400);
129129
Mockito.when(cmd.getConversionTemporaryLocation()).thenReturn(secondaryDataStore);
130130
Mockito.when(cmd.getCheckConversionSupport()).thenReturn(checkConversionSupport);
131+
Mockito.when(cmd.getOriginalVMName()).thenReturn(vmName);
131132
return cmd;
132133
}
133134

server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,9 +1686,14 @@ protected UserVm importUnmanagedInstanceFromVmwareToKvm(DataCenter zone, Cluster
16861686
importVMTask = importVmTasksManager.createImportVMTaskRecord(zone, owner, userId, displayName, vcenter, datacenterName, sourceVMName,
16871687
convertHost, importHost);
16881688
importVmTasksManager.updateImportVMTaskStep(importVMTask, zone, owner, convertHost, importHost, null, CloningInstance);
1689+
1690+
// sourceVMwareInstance could be a cloned instance from sourceVMName, of the sourceVMName itself if its powered off.
1691+
// isClonedInstance indicates if the VM is a clone of sourceVMName
1692+
16891693
Pair<UnmanagedInstanceTO, Boolean> sourceInstanceDetails = getSourceVmwareUnmanagedInstance(vcenter, datacenterName, username, password, clusterName, sourceHostName, sourceVMName);
16901694
sourceVMwareInstance = sourceInstanceDetails.first();
16911695
isClonedInstance = sourceInstanceDetails.second();
1696+
16921697
boolean isWindowsVm = sourceVMwareInstance.getOperatingSystem().toLowerCase().contains("windows");
16931698
if (isWindowsVm) {
16941699
checkConversionSupportOnHost(convertHost, sourceVMName, true);
@@ -1994,7 +1999,8 @@ private UnmanagedInstanceTO convertVmwareInstanceToKVMWithOVFOnConvertLocation(
19941999
RemoteInstanceTO remoteInstanceTO = new RemoteInstanceTO(sourceVM);
19952000
List<String> destinationStoragePools = selectInstanceConversionStoragePools(convertStoragePools, sourceVMwareInstance.getDisks(), serviceOffering, dataDiskOfferingMap);
19962001
ConvertInstanceCommand cmd = new ConvertInstanceCommand(remoteInstanceTO,
1997-
Hypervisor.HypervisorType.KVM, temporaryConvertLocation, ovfTemplateDirConvertLocation, false, false);
2002+
Hypervisor.HypervisorType.KVM, temporaryConvertLocation,
2003+
ovfTemplateDirConvertLocation, false, false, sourceVM);
19982004
if (StringUtils.isNotBlank(extraParams)) {
19992005
cmd.setExtraParams(extraParams);
20002006
}
@@ -2016,7 +2022,7 @@ private UnmanagedInstanceTO convertVmwareInstanceToKVMAfterExportingOVFToConvert
20162022
RemoteInstanceTO remoteInstanceTO = new RemoteInstanceTO(sourceVMwareInstance.getName(), sourceVMwareInstance.getPath(), vcenterHost, vcenterUsername, vcenterPassword, datacenterName);
20172023
List<String> destinationStoragePools = selectInstanceConversionStoragePools(convertStoragePools, sourceVMwareInstance.getDisks(), serviceOffering, dataDiskOfferingMap);
20182024
ConvertInstanceCommand cmd = new ConvertInstanceCommand(remoteInstanceTO,
2019-
Hypervisor.HypervisorType.KVM, temporaryConvertLocation, null, false, true);
2025+
Hypervisor.HypervisorType.KVM, temporaryConvertLocation, null, false, true, sourceVM);
20202026
int timeoutSeconds = UnmanagedVMsManager.ConvertVmwareInstanceToKvmTimeout.value() * 60 * 60;
20212027
cmd.setWait(timeoutSeconds);
20222028
int noOfThreads = UnmanagedVMsManager.ThreadsOnKVMHostToImportVMwareVMFiles.value();

0 commit comments

Comments
 (0)