Fix duplicate dummy templates, and update guest os for dummy template (#12780)

* Fix duplicate dummy template 'kvm-default-vm-import-dummy-template' entries

* Update guest os id of dummy template to 99 (Other Linux (64-bit)) from existing id: 1 (CentOS 4.5 (32-bit))

* update migration path to remove duplicate dummy templates
This commit is contained in:
Suresh Kumar Anaparti 2026-03-24 18:01:26 +05:30 committed by GitHub
parent 7107d28db8
commit 7c3637a2f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 64 additions and 8 deletions

View File

@ -26,6 +26,8 @@ import static com.cloud.hypervisor.Hypervisor.HypervisorType.VMware;
public interface UnmanagedVMsManager extends VmImportService, UnmanageVMService, PluggableService, Configurable {
String VM_IMPORT_DEFAULT_TEMPLATE_NAME = "system-default-vm-import-dummy-template.iso";
String KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME = "kvm-default-vm-import-dummy-template";
ConfigKey<Boolean> UnmanageVMPreserveNic = new ConfigKey<>("Advanced", Boolean.class, "unmanage.vm.preserve.nics", "false",
"If set to true, do not remove VM nics (and its MAC addresses) when unmanaging a VM, leaving them allocated but not reserved. " +
"If set to false, nics are removed and MAC addresses can be reassigned", true, ConfigKey.Scope.Zone);

View File

@ -16,6 +16,14 @@
// under the License.
package com.cloud.upgrade.dao;
import org.apache.cloudstack.vm.UnmanagedVMsManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class Upgrade42200to42210 extends DbUpgradeAbstractImpl implements DbUpgrade, DbUpgradeSystemVmTemplate {
@Override
@ -27,4 +35,47 @@ public class Upgrade42200to42210 extends DbUpgradeAbstractImpl implements DbUpgr
public String getUpgradedVersion() {
return "4.22.1.0";
}
@Override
public void performDataMigration(Connection conn) {
removeDuplicateKVMImportTemplates(conn);
}
private void removeDuplicateKVMImportTemplates(Connection conn) {
List<Long> templateIds = new ArrayList<>();
try (PreparedStatement selectStmt = conn.prepareStatement(String.format("SELECT id FROM cloud.vm_template WHERE name='%s' ORDER BY id ASC", UnmanagedVMsManager.KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME))) {
ResultSet rs = selectStmt.executeQuery();
while (rs.next()) {
templateIds.add(rs.getLong(1));
}
if (templateIds.size() <= 1) {
return;
}
logger.info("Removing duplicate template " + UnmanagedVMsManager.KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME + " entries");
Long firstTemplateId = templateIds.get(0);
String updateTemplateSql = "UPDATE cloud.vm_instance SET vm_template_id = ? WHERE vm_template_id = ?";
String deleteTemplateSql = "DELETE FROM cloud.vm_template WHERE id = ?";
try (PreparedStatement updateTemplateStmt = conn.prepareStatement(updateTemplateSql);
PreparedStatement deleteTemplateStmt = conn.prepareStatement(deleteTemplateSql)) {
for (int i = 1; i < templateIds.size(); i++) {
Long duplicateTemplateId = templateIds.get(i);
// Update VM references
updateTemplateStmt.setLong(1, firstTemplateId);
updateTemplateStmt.setLong(2, duplicateTemplateId);
updateTemplateStmt.executeUpdate();
// Delete duplicate dummy template
deleteTemplateStmt.setLong(1, duplicateTemplateId);
deleteTemplateStmt.executeUpdate();
}
}
} catch (Exception e) {
logger.warn("Failed to remove duplicate template " + UnmanagedVMsManager.KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME + " entries", e);
}
}
}

View File

@ -33,3 +33,5 @@ UPDATE `cloud`.`alert` SET type = 34 WHERE name = 'ALERT.VR.PRIVATE.IFACE.MTU';
-- Update configuration 'kvm.ssh.to.agent' description and is_dynamic fields
UPDATE `cloud`.`configuration` SET description = 'True if the management server will restart the agent service via SSH into the KVM hosts after or during maintenance operations', is_dynamic = 1 WHERE name = 'kvm.ssh.to.agent';
UPDATE `cloud`.`vm_template` SET guest_os_id = 99 WHERE name = 'kvm-default-vm-import-dummy-template';

View File

@ -148,8 +148,8 @@ import java.util.HashSet;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import static org.apache.cloudstack.vm.UnmanagedVMsManagerImpl.KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME;
import static org.apache.cloudstack.vm.UnmanagedVMsManagerImpl.VM_IMPORT_DEFAULT_TEMPLATE_NAME;
import static org.apache.cloudstack.vm.UnmanagedVMsManager.KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME;
import static org.apache.cloudstack.vm.UnmanagedVMsManager.VM_IMPORT_DEFAULT_TEMPLATE_NAME;
public class StorageSystemDataMotionStrategy implements DataMotionStrategy {
protected Logger logger = LogManager.getLogger(getClass());

View File

@ -200,9 +200,8 @@ import static org.apache.cloudstack.vm.ImportVmTask.Step.ConvertingInstance;
import static org.apache.cloudstack.vm.ImportVmTask.Step.Importing;
public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
public static final String VM_IMPORT_DEFAULT_TEMPLATE_NAME = "system-default-vm-import-dummy-template.iso";
public static final String KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME = "kvm-default-vm-import-dummy-template";
protected Logger logger = LogManager.getLogger(UnmanagedVMsManagerImpl.class);
private static final long OTHER_LINUX_64_GUEST_OS_ID = 99;
private static final List<Hypervisor.HypervisorType> importUnmanagedInstancesSupportedHypervisors =
Arrays.asList(Hypervisor.HypervisorType.VMware, Hypervisor.HypervisorType.KVM);
@ -325,7 +324,7 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
try {
template = VMTemplateVO.createSystemIso(templateDao.getNextInSequence(Long.class, "id"), templateName, templateName, true,
"", true, 64, Account.ACCOUNT_ID_SYSTEM, "",
"VM Import Default Template", false, 1);
"VM Import Default Template", false, OTHER_LINUX_64_GUEST_OS_ID);
template.setState(VirtualMachineTemplate.State.Inactive);
template = templateDao.persist(template);
if (template == null) {
@ -1487,11 +1486,13 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
protected VMTemplateVO getTemplateForImportInstance(Long templateId, Hypervisor.HypervisorType hypervisorType) {
VMTemplateVO template;
if (templateId == null) {
template = templateDao.findByName(VM_IMPORT_DEFAULT_TEMPLATE_NAME);
boolean isKVMHypervisor = Hypervisor.HypervisorType.KVM.equals(hypervisorType);
String templateName = (isKVMHypervisor) ? KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME : VM_IMPORT_DEFAULT_TEMPLATE_NAME;
template = templateDao.findByName(templateName);
if (template == null) {
template = createDefaultDummyVmImportTemplate(Hypervisor.HypervisorType.KVM == hypervisorType);
template = createDefaultDummyVmImportTemplate(isKVMHypervisor);
if (template == null) {
throw new InvalidParameterValueException(String.format("Default VM import template with unique name: %s for hypervisor: %s cannot be created. Please use templateid parameter for import", VM_IMPORT_DEFAULT_TEMPLATE_NAME, hypervisorType.toString()));
throw new InvalidParameterValueException(String.format("Default VM import template with unique name: %s for hypervisor: %s cannot be created. Please use templateid parameter for import", templateName, hypervisorType.toString()));
}
}
} else {