Skip to content

Commit 7107d28

Browse files
authored
[VMware to KVM] Add guest OS for importing VM based on the source VM OS (#12802)
1 parent bce5594 commit 7107d28

File tree

20 files changed

+240
-106
lines changed

20 files changed

+240
-106
lines changed

api/src/main/java/com/cloud/vm/UserVmService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ UserVm upgradeVirtualMachine(ScaleVMCmd cmd) throws ResourceUnavailableException
524524
* @param userId user ID
525525
* @param serviceOffering service offering for the imported VM
526526
* @param sshPublicKey ssh key for the imported VM
527+
* @param guestOsId guest OS ID for the imported VM (if not passed, then the guest OS of the template will be used)
527528
* @param hostName the name for the imported VM
528529
* @param hypervisorType hypervisor type for the imported VM
529530
* @param customParameters details for the imported VM
@@ -533,7 +534,7 @@ UserVm upgradeVirtualMachine(ScaleVMCmd cmd) throws ResourceUnavailableException
533534
* @throws InsufficientCapacityException in case of errors
534535
*/
535536
UserVm importVM(final DataCenter zone, final Host host, final VirtualMachineTemplate template, final String instanceNameInternal, final String displayName, final Account owner, final String userData, final Account caller, final Boolean isDisplayVm, final String keyboard,
536-
final long accountId, final long userId, final ServiceOffering serviceOffering, final String sshPublicKey,
537+
final long accountId, final long userId, final ServiceOffering serviceOffering, final String sshPublicKey, final Long guestOsId,
537538
final String hostName, final HypervisorType hypervisorType, final Map<String, String> customParameters,
538539
final VirtualMachine.PowerState powerState, final LinkedHashMap<String, List<NicProfile>> networkNicMap) throws InsufficientCapacityException;
539540

api/src/main/java/org/apache/cloudstack/api/command/admin/vm/ImportVmCmd.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.cloudstack.api.Parameter;
3131
import org.apache.cloudstack.api.ResponseObject;
3232
import org.apache.cloudstack.api.ServerApiException;
33+
import org.apache.cloudstack.api.response.GuestOSResponse;
3334
import org.apache.cloudstack.api.response.HostResponse;
3435
import org.apache.cloudstack.api.response.NetworkResponse;
3536
import org.apache.cloudstack.api.response.StoragePoolResponse;
@@ -171,6 +172,13 @@ public class ImportVmCmd extends ImportUnmanagedInstanceCmd {
171172
description = "(only for importing VMs from VMware to KVM) optional - if true, forces virt-v2v conversions to write directly on the provided storage pool (avoid using temporary conversion pool).")
172173
private Boolean forceConvertToPool;
173174

175+
@Parameter(name = ApiConstants.OS_ID,
176+
type = CommandType.UUID,
177+
entityType = GuestOSResponse.class,
178+
since = "4.22.1",
179+
description = "(only for importing VMs from VMware to KVM) optional - the ID of the guest OS for the imported VM.")
180+
private Long guestOsId;
181+
174182
/////////////////////////////////////////////////////
175183
/////////////////// Accessors ///////////////////////
176184
/////////////////////////////////////////////////////
@@ -268,6 +276,10 @@ public boolean getForceConvertToPool() {
268276
return BooleanUtils.toBooleanDefaultIfNull(forceConvertToPool, false);
269277
}
270278

279+
public Long getGuestOsId() {
280+
return guestOsId;
281+
}
282+
271283
@Override
272284
public String getEventDescription() {
273285
String vmName = getName();

api/src/main/java/org/apache/cloudstack/api/command/user/guest/ListGuestOsCmd.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class ListGuestOsCmd extends BaseListCmd {
4545
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = GuestOSResponse.class, description = "List by OS type ID")
4646
private Long id;
4747

48+
@Parameter(name = ApiConstants.IDS, type = CommandType.LIST, collectionType = CommandType.UUID,
49+
entityType = GuestOSResponse.class, since = "4.22.1",
50+
description = "Comma separated list of OS types")
51+
private List<Long> ids;
52+
4853
@Parameter(name = ApiConstants.OS_CATEGORY_ID, type = CommandType.UUID, entityType = GuestOSCategoryResponse.class, description = "List by OS Category ID")
4954
private Long osCategoryId;
5055

@@ -63,6 +68,10 @@ public Long getId() {
6368
return id;
6469
}
6570

71+
public List<Long> getIds() {
72+
return ids;
73+
}
74+
6675
public Long getOsCategoryId() {
6776
return osCategoryId;
6877
}

api/src/main/java/org/apache/cloudstack/api/response/UnmanagedInstanceResponse.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public class UnmanagedInstanceResponse extends BaseResponse {
5151
@Param(description = "The name of the host to which Instance belongs")
5252
private String hostName;
5353

54+
@SerializedName(ApiConstants.HYPERVISOR)
55+
@Param(description = "The hypervisor to which Instance belongs")
56+
private String hypervisor;
57+
58+
@SerializedName(ApiConstants.HYPERVISOR_VERSION)
59+
@Param(description = "The hypervisor version of the host to which Instance belongs")
60+
private String hypervisorVersion;
61+
5462
@SerializedName(ApiConstants.POWER_STATE)
5563
@Param(description = "The power state of the Instance")
5664
private String powerState;
@@ -140,6 +148,22 @@ public void setHostName(String hostName) {
140148
this.hostName = hostName;
141149
}
142150

151+
public String getHypervisor() {
152+
return hypervisor;
153+
}
154+
155+
public void setHypervisor(String hypervisor) {
156+
this.hypervisor = hypervisor;
157+
}
158+
159+
public String getHypervisorVersion() {
160+
return hypervisorVersion;
161+
}
162+
163+
public void setHypervisorVersion(String hypervisorVersion) {
164+
this.hypervisorVersion = hypervisorVersion;
165+
}
166+
143167
public String getPowerState() {
144168
return powerState;
145169
}

api/src/main/java/org/apache/cloudstack/vm/UnmanagedInstanceTO.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public enum PowerState {
5555

5656
private String hostName;
5757

58+
private String hypervisorType;
59+
private String hostHypervisorVersion;
60+
5861
private List<Disk> disks;
5962

6063
private List<Nic> nics;
@@ -168,6 +171,22 @@ public void setHostName(String hostName) {
168171
this.hostName = hostName;
169172
}
170173

174+
public String getHypervisorType() {
175+
return hypervisorType;
176+
}
177+
178+
public void setHypervisorType(String hypervisorType) {
179+
this.hypervisorType = hypervisorType;
180+
}
181+
182+
public String getHostHypervisorVersion() {
183+
return hostHypervisorVersion;
184+
}
185+
186+
public void setHostHypervisorVersion(String hostHypervisorVersion) {
187+
this.hostHypervisorVersion = hostHypervisorVersion;
188+
}
189+
171190
public List<Disk> getDisks() {
172191
return disks;
173192
}

engine/schema/src/main/java/com/cloud/storage/dao/GuestOSDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public interface GuestOSDao extends GenericDao<GuestOSVO, Long> {
3535

3636
List<GuestOSVO> listByDisplayName(String displayName);
3737

38-
Pair<List<? extends GuestOS>, Integer> listGuestOSByCriteria(Long startIndex, Long pageSize, Long id, Long osCategoryId, String description, String keyword, Boolean forDisplay);
38+
Pair<List<? extends GuestOS>, Integer> listGuestOSByCriteria(Long startIndex, Long pageSize, List<Long> ids, Long osCategoryId, String description, String keyword, Boolean forDisplay);
3939

4040
List<Long> listIdsByCategoryId(final long categoryId);
4141
}

engine/schema/src/main/java/com/cloud/storage/dao/GuestOSDaoImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ public List<GuestOSVO> listByDisplayName(String displayName) {
125125
return listBy(sc);
126126
}
127127

128-
public Pair<List<? extends GuestOS>, Integer> listGuestOSByCriteria(Long startIndex, Long pageSize, Long id, Long osCategoryId, String description, String keyword, Boolean forDisplay) {
128+
public Pair<List<? extends GuestOS>, Integer> listGuestOSByCriteria(Long startIndex, Long pageSize, List<Long> ids, Long osCategoryId, String description, String keyword, Boolean forDisplay) {
129129
final Filter searchFilter = new Filter(GuestOSVO.class, "displayName", true, startIndex, pageSize);
130130
final SearchCriteria<GuestOSVO> sc = createSearchCriteria();
131131

132-
if (id != null) {
133-
sc.addAnd("id", SearchCriteria.Op.EQ, id);
132+
if (CollectionUtils.isNotEmpty(ids)) {
133+
sc.addAnd("id", SearchCriteria.Op.IN, ids.toArray());
134134
}
135135

136136
if (osCategoryId != null) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.cloud.agent.api.Answer;
2323
import com.cloud.agent.api.GetRemoteVmsAnswer;
2424
import com.cloud.agent.api.GetRemoteVmsCommand;
25+
import com.cloud.hypervisor.Hypervisor;
2526
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
2627
import com.cloud.hypervisor.kvm.resource.LibvirtConnection;
2728
import com.cloud.hypervisor.kvm.resource.LibvirtDomainXMLParser;
@@ -97,6 +98,7 @@ private UnmanagedInstanceTO getUnmanagedInstance(LibvirtComputingResource libvir
9798
if (parser.getCpuTuneDef() !=null) {
9899
instance.setCpuSpeed(parser.getCpuTuneDef().getShares());
99100
}
101+
instance.setHypervisorType(Hypervisor.HypervisorType.KVM.name());
100102
instance.setPowerState(getPowerState(libvirtComputingResource.getVmState(conn,domain.getName())));
101103
instance.setNics(getUnmanagedInstanceNics(parser.getInterfaces()));
102104
instance.setDisks(getUnmanagedInstanceDisks(parser.getDisks(),libvirtComputingResource, domain));

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.cloud.agent.api.GetUnmanagedInstancesAnswer;
2121
import com.cloud.agent.api.GetUnmanagedInstancesCommand;
22+
import com.cloud.hypervisor.Hypervisor;
2223
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
2324
import com.cloud.hypervisor.kvm.resource.LibvirtDomainXMLParser;
2425
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef;
@@ -130,6 +131,7 @@ private UnmanagedInstanceTO getUnmanagedInstance(LibvirtComputingResource libvir
130131
if (parser.getCpuModeDef() != null) {
131132
instance.setCpuCoresPerSocket(parser.getCpuModeDef().getCoresPerSocket());
132133
}
134+
instance.setHypervisorType(Hypervisor.HypervisorType.KVM.name());
133135
instance.setPowerState(getPowerState(libvirtComputingResource.getVmState(conn,domain.getName())));
134136
instance.setMemory((int) LibvirtComputingResource.getDomainMemory(domain) / 1024);
135137
instance.setNics(getUnmanagedInstanceNics(libvirtComputingResource, parser.getInterfaces()));

server/src/main/java/com/cloud/api/ApiResponseHelper.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5339,8 +5339,21 @@ public UnmanagedInstanceResponse createUnmanagedInstanceResponse(UnmanagedInstan
53395339
if (host != null) {
53405340
response.setHostId(host.getUuid());
53415341
response.setHostName(host.getName());
5342-
} else if (instance.getHostName() != null) {
5343-
response.setHostName(instance.getHostName());
5342+
if (host.getHypervisorType() != null) {
5343+
response.setHypervisor(host.getHypervisorType().name());
5344+
}
5345+
response.setHypervisorVersion(host.getHypervisorVersion());
5346+
} else {
5347+
// In case the unmanaged instance is on an external host
5348+
if (instance.getHostName() != null) {
5349+
response.setHostName(instance.getHostName());
5350+
}
5351+
if (instance.getHypervisorType() != null) {
5352+
response.setHypervisor(instance.getHypervisorType());
5353+
}
5354+
if (instance.getHostHypervisorVersion() != null) {
5355+
response.setHypervisorVersion(instance.getHostHypervisorVersion());
5356+
}
53445357
}
53455358
response.setPowerState((instance.getPowerState() != null)? instance.getPowerState().toString() : UnmanagedInstanceTO.PowerState.PowerUnknown.toString());
53465359
response.setCpuCores(instance.getCpuCores());

0 commit comments

Comments
 (0)