From a00adaa66cb5bcaff4573f4326b5283091b7488e Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Wed, 11 Mar 2026 16:16:37 +0100 Subject: [PATCH 1/2] server: copy template details to vm instance details when import VM --- .../main/java/com/cloud/vm/UserVmManagerImpl.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java index 49761905f004..5630813e58f5 100644 --- a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java @@ -9509,6 +9509,13 @@ public UserVm importVM(final DataCenter zone, final Host host, final VirtualMach throw new InvalidParameterValueException("Unable to import virtual machine with invalid host"); } + // Ensure template details are loaded so that commitUserVm can copy them into the VM's details map + VMTemplateVO vmTemplateVO = null; + if (template != null) { + vmTemplateVO = _templateDao.findById(template.getId()); + _templateDao.loadDetails(vmTemplateVO); + } + final long id = _vmDao.getNextInSequence(Long.class, "id"); String instanceName = StringUtils.isBlank(instanceNameInternal) ? getInternalName(owner.getAccountId(), id) : @@ -9521,10 +9528,10 @@ public UserVm importVM(final DataCenter zone, final Host host, final VirtualMach final String uuidName = _uuidMgr.generateUuid(UserVm.class, null); final Host lastHost = powerState != VirtualMachine.PowerState.PowerOn ? host : null; - final Boolean dynamicScalingEnabled = checkIfDynamicScalingCanBeEnabled(null, serviceOffering, template, zone.getId()); - return commitUserVm(true, zone, host, lastHost, template, hostName, displayName, owner, + final Boolean dynamicScalingEnabled = checkIfDynamicScalingCanBeEnabled(null, serviceOffering, vmTemplateVO, zone.getId()); + return commitUserVm(true, zone, host, lastHost, vmTemplateVO, hostName, displayName, owner, null, null, userData, null, null, isDisplayVm, keyboard, - accountId, userId, serviceOffering, template.getFormat().equals(ImageFormat.ISO), sshPublicKeys, networkNicMap, + accountId, userId, serviceOffering, vmTemplateVO.getFormat().equals(ImageFormat.ISO), sshPublicKeys, networkNicMap, id, instanceName, uuidName, hypervisorType, customParameters, null, null, null, powerState, dynamicScalingEnabled, null, serviceOffering.getDiskOfferingId(), null, null, null, null); }); From 0ed525a744e52794679ff3a6c416eb76ed54a18b Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Mon, 11 May 2026 11:43:48 +0200 Subject: [PATCH 2/2] apply suggestions --- .../main/java/com/cloud/vm/UserVmManagerImpl.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java index 5630813e58f5..ef148580f017 100644 --- a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java @@ -9508,13 +9508,16 @@ public UserVm importVM(final DataCenter zone, final Host host, final VirtualMach if (host == null && hypervisorType == HypervisorType.VMware) { throw new InvalidParameterValueException("Unable to import virtual machine with invalid host"); } + if (template == null) { + throw new InvalidParameterValueException("Unable to import virtual machine without a template"); + } // Ensure template details are loaded so that commitUserVm can copy them into the VM's details map - VMTemplateVO vmTemplateVO = null; - if (template != null) { - vmTemplateVO = _templateDao.findById(template.getId()); - _templateDao.loadDetails(vmTemplateVO); + VMTemplateVO vmTemplateVO = _templateDao.findById(template.getId()); + if (vmTemplateVO == null) { + throw new InvalidParameterValueException("Unable to find template with id " + template.getId() + " for virtual machine import"); } + _templateDao.loadDetails(vmTemplateVO); final long id = _vmDao.getNextInSequence(Long.class, "id"); String instanceName = StringUtils.isBlank(instanceNameInternal) ? @@ -9531,7 +9534,7 @@ public UserVm importVM(final DataCenter zone, final Host host, final VirtualMach final Boolean dynamicScalingEnabled = checkIfDynamicScalingCanBeEnabled(null, serviceOffering, vmTemplateVO, zone.getId()); return commitUserVm(true, zone, host, lastHost, vmTemplateVO, hostName, displayName, owner, null, null, userData, null, null, isDisplayVm, keyboard, - accountId, userId, serviceOffering, vmTemplateVO.getFormat().equals(ImageFormat.ISO), sshPublicKeys, networkNicMap, + accountId, userId, serviceOffering, template.getFormat().equals(ImageFormat.ISO), sshPublicKeys, networkNicMap, id, instanceName, uuidName, hypervisorType, customParameters, null, null, null, powerState, dynamicScalingEnabled, null, serviceOffering.getDiskOfferingId(), null, null, null, null); });