You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ha...@apache.org on 2026/05/18 06:48:27 UTC

(cloudstack) branch FixMountFormatBackupAndRestore created (now 7af5fc0c5ac)

This is an automated email from the ASF dual-hosted git repository.

harikrishna-patnala pushed a change to branch FixMountFormatBackupAndRestore
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


      at 7af5fc0c5ac Unit test fixes

This branch includes the following new commits:

     new 2b82b03002d Fix repository mount failure during restore due to path format handling
     new 7af5fc0c5ac Unit test fixes

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(cloudstack) 01/02: Fix repository mount failure during restore due to path format handling

Posted by ha...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

harikrishna-patnala pushed a commit to branch FixMountFormatBackupAndRestore
in repository https://gitbox.apache.org/repos/asf/cloudstack.git

commit 2b82b03002d2f191fefff8530f3877896c447f16
Author: Harikrishna Patnala <ha...@gmail.com>
AuthorDate: Tue Mar 10 10:39:04 2026 +0530

    Fix repository mount failure during restore due to path format handling
    
    Restore could fail when the backup repository address was specified in
    formats such as \\server\share. The restore logic built a raw shell
    command which caused backslashes to be interpreted as escape characters,
    resulting in an invalid mount path.
    
    Execute the mount command using Script.executePipedCommands() so the
    repository path is passed as an argument instead of being embedded in a
    shell command string.
---
 .../LibvirtRestoreBackupCommandWrapper.java        | 28 +++++++++++++++-------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java
index 22dbfbdd67a..2b2e09df690 100644
--- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java
+++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java
@@ -45,6 +45,7 @@ import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
 import java.util.Objects;
@@ -52,7 +53,6 @@ import java.util.Objects;
 @ResourceWrapper(handles = RestoreBackupCommand.class)
 public class LibvirtRestoreBackupCommandWrapper extends CommandWrapper<RestoreBackupCommand, Answer, LibvirtComputingResource> {
     private static final String BACKUP_TEMP_FILE_PREFIX = "csbackup";
-    private static final String MOUNT_COMMAND = "sudo mount -t %s %s %s";
     private static final String UMOUNT_COMMAND = "sudo umount %s";
     private static final String FILE_PATH_PLACEHOLDER = "%s/%s";
     private static final String ATTACH_QCOW2_DISK_COMMAND = " virsh attach-disk %s %s %s --driver qemu --subdriver qcow2 --cache none";
@@ -197,7 +197,7 @@ public class LibvirtRestoreBackupCommandWrapper extends CommandWrapper<RestoreBa
 
     private String mountBackupDirectory(String backupRepoAddress, String backupRepoType, String mountOptions, Integer mountTimeout) {
         String randomChars = RandomStringUtils.random(5, true, false);
-        String mountDirectory = String.format("%s.%s",BACKUP_TEMP_FILE_PREFIX , randomChars);
+        String mountDirectory = String.format("%s.%s", BACKUP_TEMP_FILE_PREFIX, randomChars);
 
         try {
             mountDirectory = Files.createTempDirectory(mountDirectory).toString();
@@ -206,23 +206,35 @@ public class LibvirtRestoreBackupCommandWrapper extends CommandWrapper<RestoreBa
             throw new CloudRuntimeException("Failed to create the tmp mount directory for restore on the KVM host");
         }
 
-        String mount = String.format(MOUNT_COMMAND, backupRepoType, backupRepoAddress, mountDirectory);
-        if ("cifs".equals(backupRepoType)) {
+        if ("cifs".equalsIgnoreCase(backupRepoType)) {
             if (Objects.isNull(mountOptions) || mountOptions.trim().isEmpty()) {
                 mountOptions = "nobrl";
             } else {
                 mountOptions += ",nobrl";
             }
         }
+
+        List<String[]> commands = new ArrayList<>();
+        List<String> cmd = new ArrayList<>();
+        cmd.add("sudo");
+        cmd.add("mount");
+        cmd.add("-t");
+        cmd.add(backupRepoType);
+        cmd.add(backupRepoAddress);
+        cmd.add(mountDirectory);
         if (Objects.nonNull(mountOptions) && !mountOptions.trim().isEmpty()) {
-            mount += " -o " + mountOptions;
+            cmd.add("-o");
+            cmd.add(mountOptions);
         }
+        commands.add(cmd.toArray(new String[0]));
 
-        int exitValue = Script.runSimpleBashScriptForExitValue(mount, mountTimeout, false);
-        if (exitValue != 0) {
-            logger.error("Failed to mount repository {} of type {} to the directory {}", backupRepoAddress, backupRepoType, mountDirectory);
+        Pair<Integer, String> result = Script.executePipedCommands(commands, mountTimeout);
+        if (result.first() != 0) {
+            logger.error("Failed to mount repository {} of type {} to the directory {}. Error: {}",
+                    backupRepoAddress, backupRepoType, mountDirectory, result.second());
             throw new CloudRuntimeException("Failed to mount the backup repository on the KVM host");
         }
+
         return mountDirectory;
     }
 


(cloudstack) 02/02: Unit test fixes

Posted by ha...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

harikrishna-patnala pushed a commit to branch FixMountFormatBackupAndRestore
in repository https://gitbox.apache.org/repos/asf/cloudstack.git

commit 7af5fc0c5ac25371f477cf6cafeb8a2cfd3f2d83
Author: Harikrishna Patnala <ha...@gmail.com>
AuthorDate: Tue Mar 10 14:26:24 2026 +0530

    Unit test fixes
---
 .../LibvirtRestoreBackupCommandWrapper.java        |  9 ++++++--
 .../LibvirtRestoreBackupCommandWrapperTest.java    | 27 ++++++++++++++++++----
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java
index 2b2e09df690..ca85535a89c 100644
--- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java
+++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java
@@ -214,6 +214,13 @@ public class LibvirtRestoreBackupCommandWrapper extends CommandWrapper<RestoreBa
             }
         }
 
+        executeMount(backupRepoAddress, backupRepoType, mountOptions, mountDirectory, mountTimeout);
+
+        return mountDirectory;
+    }
+
+    private void executeMount(String backupRepoAddress, String backupRepoType, String mountOptions,
+                              String mountDirectory, Integer mountTimeout) {
         List<String[]> commands = new ArrayList<>();
         List<String> cmd = new ArrayList<>();
         cmd.add("sudo");
@@ -234,8 +241,6 @@ public class LibvirtRestoreBackupCommandWrapper extends CommandWrapper<RestoreBa
                     backupRepoAddress, backupRepoType, mountDirectory, result.second());
             throw new CloudRuntimeException("Failed to mount the backup repository on the KVM host");
         }
-
-        return mountDirectory;
     }
 
     private void unmountBackupDirectory(String backupDirectory) {
diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapperTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapperTest.java
index ef6b5c08189..b956d3d3730 100644
--- a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapperTest.java
+++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapperTest.java
@@ -18,6 +18,7 @@ package com.cloud.hypervisor.kvm.resource.wrapper;
 
 import com.cloud.agent.api.Answer;
 import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
+import com.cloud.utils.Pair;
 import com.cloud.storage.Storage;
 import com.cloud.utils.script.Script;
 import com.cloud.vm.VirtualMachine;
@@ -83,6 +84,8 @@ public class LibvirtRestoreBackupCommandWrapperTest {
             filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
 
             try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
+                Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
+                        .thenReturn(new Pair<>(0, "success"));
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
                         .thenReturn(0); // Mount success
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString()))
@@ -126,6 +129,8 @@ public class LibvirtRestoreBackupCommandWrapperTest {
             filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
 
             try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
+                Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
+                        .thenReturn(new Pair<>(0, "success"));
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
                         .thenReturn(0); // Mount success
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString()))
@@ -165,6 +170,8 @@ public class LibvirtRestoreBackupCommandWrapperTest {
             filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
 
             try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
+                Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
+                        .thenReturn(new Pair<>(0, "success"));
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
                         .thenReturn(0); // Mount success
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString()))
@@ -207,6 +214,8 @@ public class LibvirtRestoreBackupCommandWrapperTest {
             filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
 
             try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
+                Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
+                        .thenReturn(new Pair<>(0, "success"));
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
                         .thenReturn(0); // Mount success
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString()))
@@ -251,8 +260,8 @@ public class LibvirtRestoreBackupCommandWrapperTest {
             filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
 
             try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
-                scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
-                        .thenReturn(1); // Mount failure
+                Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
+                        .thenReturn(new Pair<>(1, "Failed"));
 
                 Answer result = wrapper.execute(command, libvirtComputingResource);
 
@@ -290,6 +299,8 @@ public class LibvirtRestoreBackupCommandWrapperTest {
             filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
 
             try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
+                Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
+                        .thenReturn(new Pair<>(0, "success"));
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
                         .thenReturn(0); // Mount success
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString()))
@@ -339,8 +350,8 @@ public class LibvirtRestoreBackupCommandWrapperTest {
             filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
 
             try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
-                scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
-                        .thenReturn(0); // Mount success
+                Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
+                        .thenReturn(new Pair<>(0, "success"));
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString()))
                         .thenAnswer(invocation -> {
                             String command = invocation.getArgument(0);
@@ -390,6 +401,8 @@ public class LibvirtRestoreBackupCommandWrapperTest {
             filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
 
             try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
+                Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
+                        .thenReturn(new Pair<>(0, "success"));
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
                         .thenAnswer(invocation -> {
                             String command = invocation.getArgument(0);
@@ -449,6 +462,8 @@ public class LibvirtRestoreBackupCommandWrapperTest {
             filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
 
             try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
+                Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
+                        .thenReturn(new Pair<>(0, "success"));
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
                         .thenAnswer(invocation -> {
                             String command = invocation.getArgument(0);
@@ -551,6 +566,10 @@ public class LibvirtRestoreBackupCommandWrapperTest {
             filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath);
 
             try (MockedStatic<Script> scriptMock = mockStatic(Script.class)) {
+                Mockito.when(Script.executePipedCommands(Mockito.anyList(), Mockito.anyLong()))
+                        .thenReturn(new Pair<>(0, "success"));
+                scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))
+                        .thenReturn(0); // Mount success
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString()))
                         .thenReturn(0); // All commands success
                 scriptMock.when(() -> Script.runSimpleBashScriptForExitValue(anyString(), anyInt(), any(Boolean.class)))