Skip to content

Commit 2e3f76e

Browse files
Improve error messaging / logs when listing VMs on the remote KVM host (for import) (#9204)
1 parent 6edcf32 commit 2e3f76e

2 files changed

Lines changed: 19 additions & 22 deletions

File tree

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ public final class LibvirtGetRemoteVmsCommandWrapper extends CommandWrapper<GetR
5050

5151
@Override
5252
public Answer execute(final GetRemoteVmsCommand command, final LibvirtComputingResource libvirtComputingResource) {
53-
String result = null;
54-
String hypervisorURI = "qemu+tcp://" + command.getRemoteIp() +
55-
"/system";
53+
String hypervisorURI = "qemu+tcp://" + command.getRemoteIp() + "/system";
5654
HashMap<String, UnmanagedInstanceTO> unmanagedInstances = new HashMap<>();
5755
try {
5856
Connect conn = LibvirtConnection.getConnection(hypervisorURI);
@@ -61,26 +59,27 @@ public Answer execute(final GetRemoteVmsCommand command, final LibvirtComputingR
6159
final Domain domain = libvirtComputingResource.getDomain(conn, name);
6260

6361
final DomainInfo.DomainState ps = domain.getInfo().state;
64-
6562
final VirtualMachine.PowerState state = libvirtComputingResource.convertToPowerState(ps);
66-
67-
s_logger.debug("VM " + domain.getName() + ": powerstate = " + ps + "; vm state=" + state.toString());
63+
s_logger.debug("VM " + domain.getName() + " - powerstate: " + ps + ", state: " + state.toString());
6864

6965
if (state == VirtualMachine.PowerState.PowerOff) {
7066
try {
7167
UnmanagedInstanceTO instance = getUnmanagedInstance(libvirtComputingResource, domain, conn);
7268
unmanagedInstances.put(instance.getName(), instance);
7369
} catch (Exception e) {
74-
s_logger.error("Error while fetching instance details", e);
70+
s_logger.error("Couldn't fetch VM " + domain.getName() + " details, due to: " + e.getMessage(), e);
7571
}
7672
}
7773
domain.free();
7874
}
79-
s_logger.debug("Found Vms: "+ unmanagedInstances.size());
80-
return new GetRemoteVmsAnswer(command, "", unmanagedInstances);
75+
s_logger.debug("Found " + unmanagedInstances.size() + " stopped VMs on host " + command.getRemoteIp());
76+
return new GetRemoteVmsAnswer(command, "", unmanagedInstances);
8177
} catch (final LibvirtException e) {
82-
s_logger.error("Error while listing stopped Vms on remote host: "+ e.getMessage());
83-
return new Answer(command, false, result);
78+
s_logger.error("Failed to list stopped VMs on remote host " + command.getRemoteIp() + ", due to: " + e.getMessage(), e);
79+
if (e.getMessage().toLowerCase().contains("connection refused")) {
80+
return new Answer(command, false, "Unable to connect to remote host " + command.getRemoteIp() + ", please check the libvirtd tcp connectivity and retry");
81+
}
82+
return new Answer(command, false, "Unable to list stopped VMs on remote host " + command.getRemoteIp() + ", due to: " + e.getMessage());
8483
}
8584
}
8685

@@ -106,8 +105,8 @@ private UnmanagedInstanceTO getUnmanagedInstance(LibvirtComputingResource libvir
106105

107106
return instance;
108107
} catch (Exception e) {
109-
s_logger.debug("Unable to retrieve unmanaged instance info. ", e);
110-
throw new CloudRuntimeException("Unable to retrieve unmanaged instance info. " + e.getMessage());
108+
s_logger.debug("Unable to retrieve unmanaged instance info, due to: " + e.getMessage(), e);
109+
throw new CloudRuntimeException("Unable to retrieve unmanaged instance info, due to: " + e.getMessage());
111110
}
112111
}
113112

@@ -119,7 +118,6 @@ private UnmanagedInstanceTO.PowerState getPowerState(VirtualMachine.PowerState v
119118
return UnmanagedInstanceTO.PowerState.PowerOff;
120119
default:
121120
return UnmanagedInstanceTO.PowerState.PowerUnknown;
122-
123121
}
124122
}
125123

@@ -166,7 +164,6 @@ private List<UnmanagedInstanceTO.Disk> getUnmanagedInstanceDisks(List<LibvirtVMD
166164
disk.setLabel(diskDef.getDiskLabel());
167165
disk.setController(diskDef.getBusType().toString());
168166

169-
170167
Pair<String, String> sourceHostPath = getSourceHostPath(libvirtComputingResource, diskDef.getSourcePath());
171168
if (sourceHostPath != null) {
172169
disk.setDatastoreHost(sourceHostPath.first());

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,10 +2046,10 @@ private UserVmResponse importKvmInstance(ImportVmCmd cmd) {
20462046
throw new InvalidParameterValueException("Username need to be provided.");
20472047
}
20482048

2049-
HashMap<String, UnmanagedInstanceTO> instancesMap = getRemoteVms(zoneId, remoteUrl, cmd.getUsername(), cmd.getPassword());
2049+
HashMap<String, UnmanagedInstanceTO> instancesMap = getRemoteVmsOnKVMHost(zoneId, remoteUrl, cmd.getUsername(), cmd.getPassword());
20502050
unmanagedInstanceTO = instancesMap.get(cmd.getName());
20512051
if (unmanagedInstanceTO == null) {
2052-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Vm with name: %s not found on remote host", instanceName));
2052+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("VM with name: %s not found on remote host %s", instanceName, remoteUrl));
20532053
}
20542054
}
20552055

@@ -2460,7 +2460,7 @@ public ListResponse<UnmanagedInstanceResponse> listVmsForImport(ListVmsForImport
24602460
}
24612461

24622462
List<UnmanagedInstanceResponse> responses = new ArrayList<>();
2463-
HashMap<String, UnmanagedInstanceTO> vmMap = getRemoteVms(zoneId, cmd.getHost(), cmd.getUsername(), cmd.getPassword());
2463+
HashMap<String, UnmanagedInstanceTO> vmMap = getRemoteVmsOnKVMHost(zoneId, cmd.getHost(), cmd.getUsername(), cmd.getPassword());
24642464
for (String key : vmMap.keySet()) {
24652465
UnmanagedInstanceTO instance = vmMap.get(key);
24662466
if (StringUtils.isNotEmpty(keyword) &&
@@ -2475,17 +2475,17 @@ public ListResponse<UnmanagedInstanceResponse> listVmsForImport(ListVmsForImport
24752475
return listResponses;
24762476
}
24772477

2478-
private HashMap<String, UnmanagedInstanceTO> getRemoteVms(long zoneId, String remoteUrl, String username, String password) {
2478+
private HashMap<String, UnmanagedInstanceTO> getRemoteVmsOnKVMHost(long zoneId, String remoteHostUrl, String username, String password) {
24792479
//ToDo: add option to list one Vm by name
24802480
List<HostVO> hosts = resourceManager.listAllUpAndEnabledHostsInOneZoneByHypervisor(Hypervisor.HypervisorType.KVM, zoneId);
24812481
if(hosts.size() < 1) {
2482-
throw new CloudRuntimeException("No hosts available for Vm Import");
2482+
throw new CloudRuntimeException("No hosts available for VM import");
24832483
}
24842484
HostVO host = hosts.get(0);
2485-
GetRemoteVmsCommand getRemoteVmsCommand = new GetRemoteVmsCommand(remoteUrl, username, password);
2485+
GetRemoteVmsCommand getRemoteVmsCommand = new GetRemoteVmsCommand(remoteHostUrl, username, password);
24862486
Answer answer = agentManager.easySend(host.getId(), getRemoteVmsCommand);
24872487
if (!(answer instanceof GetRemoteVmsAnswer)) {
2488-
throw new CloudRuntimeException("Error while listing remote Vms");
2488+
throw new CloudRuntimeException("Failed to list VMs, due to: " + answer.getDetails());
24892489
}
24902490
GetRemoteVmsAnswer getRemoteVmsAnswer = (GetRemoteVmsAnswer) answer;
24912491
return getRemoteVmsAnswer.getUnmanagedInstances();

0 commit comments

Comments
 (0)