Skip to content

Commit f82e4e4

Browse files
committed
[VMware to KVM] Add guest OS for importing VM based on the source VM OS
1 parent 6516f7f commit f82e4e4

File tree

18 files changed

+177
-32
lines changed

18 files changed

+177
-32
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ public class ImportVmCmd extends ImportUnmanagedInstanceCmd {
171171
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).")
172172
private Boolean forceConvertToPool;
173173

174+
@Parameter(name = ApiConstants.OS_ID,
175+
type = CommandType.UUID,
176+
since = "4.22.1",
177+
description = "(only for importing VMs from VMware to KVM) optional - the ID of the guest OS for the imported VM.")
178+
private Long guestOsId;
179+
174180
/////////////////////////////////////////////////////
175181
/////////////////// Accessors ///////////////////////
176182
/////////////////////////////////////////////////////
@@ -268,6 +274,10 @@ public boolean getForceConvertToPool() {
268274
return BooleanUtils.toBooleanDefaultIfNull(forceConvertToPool, false);
269275
}
270276

277+
public Long getGuestOsId() {
278+
return guestOsId;
279+
}
280+
271281
@Override
272282
public String getEventDescription() {
273283
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ 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_VERSION)
55+
@Param(description = "The version of the host to which Instance belongs")
56+
private String hypervisorVersion;
57+
5458
@SerializedName(ApiConstants.POWER_STATE)
5559
@Param(description = "The power state of the Instance")
5660
private String powerState;
@@ -140,6 +144,14 @@ public void setHostName(String hostName) {
140144
this.hostName = hostName;
141145
}
142146

147+
public String getHypervisorVersion() {
148+
return hypervisorVersion;
149+
}
150+
151+
public void setHypervisorVersion(String hypervisorVersion) {
152+
this.hypervisorVersion = hypervisorVersion;
153+
}
154+
143155
public String getPowerState() {
144156
return powerState;
145157
}

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

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

5656
private String hostName;
5757

58+
private String hostHypervisorVersion;
59+
5860
private List<Disk> disks;
5961

6062
private List<Nic> nics;
@@ -168,6 +170,14 @@ public void setHostName(String hostName) {
168170
this.hostName = hostName;
169171
}
170172

173+
public String getHostHypervisorVersion() {
174+
return hostHypervisorVersion;
175+
}
176+
177+
public void setHostHypervisorVersion(String hostHypervisorVersion) {
178+
this.hostHypervisorVersion = hostHypervisorVersion;
179+
}
180+
171181
public List<Disk> getDisks() {
172182
return disks;
173183
}

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) {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5339,8 +5339,15 @@ 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+
response.setHypervisorVersion(host.getHypervisorVersion());
5343+
} else {
5344+
// In case the unmanaged instance is on an external host
5345+
if (instance.getHostName() != null) {
5346+
response.setHostName(instance.getHostName());
5347+
}
5348+
if (instance.getHostHypervisorVersion() != null) {
5349+
response.setHypervisorVersion(instance.getHostHypervisorVersion());
5350+
}
53445351
}
53455352
response.setPowerState((instance.getPowerState() != null)? instance.getPowerState().toString() : UnmanagedInstanceTO.PowerState.PowerUnknown.toString());
53465353
response.setCpuCores(instance.getCpuCores());

server/src/main/java/com/cloud/server/ManagementServerImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import javax.inject.Inject;
4444
import javax.naming.ConfigurationException;
4545

46+
import com.cloud.api.query.MutualExclusiveIdsManagerBase;
4647
import com.cloud.network.vpc.VpcVO;
4748
import org.apache.cloudstack.acl.ControlledEntity;
4849
import org.apache.cloudstack.acl.SecurityChecker;
@@ -832,7 +833,6 @@
832833
import com.cloud.utils.PasswordGenerator;
833834
import com.cloud.utils.Ternary;
834835
import com.cloud.utils.component.ComponentLifecycle;
835-
import com.cloud.utils.component.ManagerBase;
836836
import com.cloud.utils.concurrency.NamedThreadFactory;
837837
import com.cloud.utils.crypt.DBEncryptionUtil;
838838
import com.cloud.utils.db.DB;
@@ -875,7 +875,7 @@
875875
import com.cloud.vm.dao.VMInstanceDao;
876876
import com.cloud.vm.dao.VMInstanceDetailsDao;
877877

878-
public class ManagementServerImpl extends ManagerBase implements ManagementServer, Configurable {
878+
public class ManagementServerImpl extends MutualExclusiveIdsManagerBase implements ManagementServer, Configurable {
879879
protected StateMachine2<State, VirtualMachine.Event, VirtualMachine> _stateMachine;
880880

881881
static final String FOR_SYSTEMVMS = "forsystemvms";
@@ -2898,15 +2898,15 @@ protected void setParameters(SearchCriteria<IPAddressVO> sc, final ListPublicIpA
28982898

28992899
@Override
29002900
public Pair<List<? extends GuestOS>, Integer> listGuestOSByCriteria(final ListGuestOsCmd cmd) {
2901-
final Long id = cmd.getId();
2901+
List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds());
29022902
final Long osCategoryId = cmd.getOsCategoryId();
29032903
final String description = cmd.getDescription();
29042904
final String keyword = cmd.getKeyword();
29052905
final Long startIndex = cmd.getStartIndex();
29062906
final Long pageSize = cmd.getPageSizeVal();
29072907
Boolean forDisplay = cmd.getDisplay();
29082908

2909-
return _guestOSDao.listGuestOSByCriteria(startIndex, pageSize, id, osCategoryId, description, keyword, forDisplay);
2909+
return _guestOSDao.listGuestOSByCriteria(startIndex, pageSize, ids, osCategoryId, description, keyword, forDisplay);
29102910
}
29112911

29122912
@Override

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4803,12 +4803,13 @@ private String generateHostName(String uuidName) {
48034803

48044804
private UserVmVO commitUserVm(final boolean isImport, final DataCenter zone, final Host host, final Host lastHost, final VirtualMachineTemplate template, final String hostName, final String displayName, final Account owner,
48054805
final Long diskOfferingId, final Long diskSize, final String userData, Long userDataId, String userDataDetails, final Boolean isDisplayVm, final String keyboard,
4806-
final long accountId, final long userId, final ServiceOffering offering, final boolean isIso, final String sshPublicKeys, final LinkedHashMap<String, List<NicProfile>> networkNicMap,
4806+
final long accountId, final long userId, final ServiceOffering offering, final boolean isIso, final Long guestOsId, final String sshPublicKeys, final LinkedHashMap<String, List<NicProfile>> networkNicMap,
48074807
final long id, final String instanceName, final String uuidName, final HypervisorType hypervisorType, final Map<String, String> customParameters,
48084808
final Map<String, Map<Integer, String>> extraDhcpOptionMap, final Map<Long, DiskOffering> dataDiskTemplateToDiskOfferingMap,
48094809
final Map<String, String> userVmOVFPropertiesMap, final VirtualMachine.PowerState powerState, final boolean dynamicScalingEnabled, String vmType, final Long rootDiskOfferingId, String sshkeypairs,
48104810
List<VmDiskInfo> dataDiskInfoList, Volume volume, Snapshot snapshot) throws InsufficientCapacityException {
4811-
UserVmVO vm = new UserVmVO(id, instanceName, displayName, template.getId(), hypervisorType, template.getGuestOSId(), offering.isOfferHA(),
4811+
Long selectedGuestOsId = guestOsId != null ? guestOsId : template.getGuestOSId();
4812+
UserVmVO vm = new UserVmVO(id, instanceName, displayName, template.getId(), hypervisorType, selectedGuestOsId, offering.isOfferHA(),
48124813
offering.getLimitCpuUse(), owner.getDomainId(), owner.getId(), userId, offering.getId(), userData, userDataId, userDataDetails, hostName);
48134814
vm.setUuid(uuidName);
48144815
vm.setDynamicallyScalable(dynamicScalingEnabled);
@@ -4834,8 +4835,7 @@ private UserVmVO commitUserVm(final boolean isImport, final DataCenter zone, fin
48344835
vm.setIsoId(template.getId());
48354836
}
48364837

4837-
long guestOSId = template.getGuestOSId();
4838-
GuestOSVO guestOS = _guestOSDao.findById(guestOSId);
4838+
GuestOSVO guestOS = _guestOSDao.findById(selectedGuestOsId);
48394839
long guestOSCategoryId = guestOS.getCategoryId();
48404840
GuestOSCategoryVO guestOSCategory = _guestOSCategoryDao.findById(guestOSCategoryId);
48414841
if (hypervisorType.equals(HypervisorType.VMware)) {
@@ -5093,7 +5093,7 @@ private UserVmVO commitUserVm(final DataCenter zone, final VirtualMachineTemplat
50935093
List<VmDiskInfo> dataDiskInfoList, Volume volume, Snapshot snapshot) throws InsufficientCapacityException {
50945094
return commitUserVm(false, zone, null, null, template, hostName, displayName, owner,
50955095
diskOfferingId, diskSize, userData, userDataId, userDataDetails, isDisplayVm, keyboard,
5096-
accountId, userId, offering, isIso, sshPublicKeys, networkNicMap,
5096+
accountId, userId, offering, isIso, null, sshPublicKeys, networkNicMap,
50975097
id, instanceName, uuidName, hypervisorType, customParameters,
50985098
extraDhcpOptionMap, dataDiskTemplateToDiskOfferingMap,
50995099
userVmOVFPropertiesMap, null, dynamicScalingEnabled, vmType, rootDiskOfferingId, sshkeypairs, dataDiskInfoList, volume, snapshot);
@@ -9498,7 +9498,7 @@ private String getInternalName(long accountId, long vmId) {
94989498
@Override
94999499
public UserVm importVM(final DataCenter zone, final Host host, final VirtualMachineTemplate template, final String instanceNameInternal, final String displayName,
95009500
final Account owner, final String userData, final Account caller, final Boolean isDisplayVm, final String keyboard,
9501-
final long accountId, final long userId, final ServiceOffering serviceOffering, final String sshPublicKeys,
9501+
final long accountId, final long userId, final ServiceOffering serviceOffering, final String sshPublicKeys, final Long guestOsId,
95029502
final String hostName, final HypervisorType hypervisorType, final Map<String, String> customParameters,
95039503
final VirtualMachine.PowerState powerState, final LinkedHashMap<String, List<NicProfile>> networkNicMap) throws InsufficientCapacityException {
95049504
return Transaction.execute((TransactionCallbackWithException<UserVm, InsufficientCapacityException>) status -> {
@@ -9524,7 +9524,7 @@ public UserVm importVM(final DataCenter zone, final Host host, final VirtualMach
95249524
final Boolean dynamicScalingEnabled = checkIfDynamicScalingCanBeEnabled(null, serviceOffering, template, zone.getId());
95259525
return commitUserVm(true, zone, host, lastHost, template, hostName, displayName, owner,
95269526
null, null, userData, null, null, isDisplayVm, keyboard,
9527-
accountId, userId, serviceOffering, template.getFormat().equals(ImageFormat.ISO), sshPublicKeys, networkNicMap,
9527+
accountId, userId, serviceOffering, template.getFormat().equals(ImageFormat.ISO), guestOsId, sshPublicKeys, networkNicMap,
95289528
id, instanceName, uuidName, hypervisorType, customParameters,
95299529
null, null, null, powerState, dynamicScalingEnabled, null, serviceOffering.getDiskOfferingId(), null, null, null, null);
95309530
});

0 commit comments

Comments
 (0)