This is an automated email from the ASF dual-hosted git repository.
sureshanaparti pushed a commit to branch 4.22
in repository https://gitbox.apache.org/repos/asf/cloudstack.git
The following commit(s) were added to refs/heads/4.22 by this push:
new 7c3637a2f58 Fix duplicate dummy templates, and update guest os for
dummy template (#12780)
7c3637a2f58 is described below
commit 7c3637a2f5803f41ccc93e5316c03058d7b538a0
Author: Suresh Kumar Anaparti <[email protected]>
AuthorDate: Tue Mar 24 18:01:26 2026 +0530
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
---
.../apache/cloudstack/vm/UnmanagedVMsManager.java | 2 +
.../com/cloud/upgrade/dao/Upgrade42200to42210.java | 51 ++++++++++++++++++++++
.../resources/META-INF/db/schema-42200to42210.sql | 2 +
.../motion/StorageSystemDataMotionStrategy.java | 4 +-
.../cloudstack/vm/UnmanagedVMsManagerImpl.java | 13 +++---
5 files changed, 64 insertions(+), 8 deletions(-)
diff --git
a/api/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManager.java
b/api/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManager.java
index b6233b9c270..e1963313eb6 100644
--- a/api/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManager.java
+++ b/api/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManager.java
@@ -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);
diff --git
a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42200to42210.java
b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42200to42210.java
index c9610f7b9ff..813351d7534 100644
--- a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42200to42210.java
+++ b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42200to42210.java
@@ -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);
+ }
+ }
}
diff --git
a/engine/schema/src/main/resources/META-INF/db/schema-42200to42210.sql
b/engine/schema/src/main/resources/META-INF/db/schema-42200to42210.sql
index a8a3d3f7bd4..2326a855f32 100644
--- a/engine/schema/src/main/resources/META-INF/db/schema-42200to42210.sql
+++ b/engine/schema/src/main/resources/META-INF/db/schema-42200to42210.sql
@@ -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';
diff --git
a/engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategy.java
b/engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategy.java
index 275f41de43a..bcade3a371c 100644
---
a/engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategy.java
+++
b/engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategy.java
@@ -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());
diff --git
a/server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java
b/server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java
index acd40447ee4..fa97236aca4 100644
--- a/server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java
+++ b/server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java
@@ -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 {