Skip to content

Commit 2b82b03

Browse files
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.
1 parent a289bb0 commit 2b82b03

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@
4545
import java.nio.file.Files;
4646
import java.nio.file.Path;
4747
import java.nio.file.Paths;
48+
import java.util.ArrayList;
4849
import java.util.List;
4950
import java.util.Locale;
5051
import java.util.Objects;
5152

5253
@ResourceWrapper(handles = RestoreBackupCommand.class)
5354
public class LibvirtRestoreBackupCommandWrapper extends CommandWrapper<RestoreBackupCommand, Answer, LibvirtComputingResource> {
5455
private static final String BACKUP_TEMP_FILE_PREFIX = "csbackup";
55-
private static final String MOUNT_COMMAND = "sudo mount -t %s %s %s";
5656
private static final String UMOUNT_COMMAND = "sudo umount %s";
5757
private static final String FILE_PATH_PLACEHOLDER = "%s/%s";
5858
private static final String ATTACH_QCOW2_DISK_COMMAND = " virsh attach-disk %s %s %s --driver qemu --subdriver qcow2 --cache none";
@@ -197,7 +197,7 @@ private void restoreVolume(KVMStoragePoolManager storagePoolMgr, String backupPa
197197

198198
private String mountBackupDirectory(String backupRepoAddress, String backupRepoType, String mountOptions, Integer mountTimeout) {
199199
String randomChars = RandomStringUtils.random(5, true, false);
200-
String mountDirectory = String.format("%s.%s",BACKUP_TEMP_FILE_PREFIX , randomChars);
200+
String mountDirectory = String.format("%s.%s", BACKUP_TEMP_FILE_PREFIX, randomChars);
201201

202202
try {
203203
mountDirectory = Files.createTempDirectory(mountDirectory).toString();
@@ -206,23 +206,35 @@ private String mountBackupDirectory(String backupRepoAddress, String backupRepoT
206206
throw new CloudRuntimeException("Failed to create the tmp mount directory for restore on the KVM host");
207207
}
208208

209-
String mount = String.format(MOUNT_COMMAND, backupRepoType, backupRepoAddress, mountDirectory);
210-
if ("cifs".equals(backupRepoType)) {
209+
if ("cifs".equalsIgnoreCase(backupRepoType)) {
211210
if (Objects.isNull(mountOptions) || mountOptions.trim().isEmpty()) {
212211
mountOptions = "nobrl";
213212
} else {
214213
mountOptions += ",nobrl";
215214
}
216215
}
216+
217+
List<String[]> commands = new ArrayList<>();
218+
List<String> cmd = new ArrayList<>();
219+
cmd.add("sudo");
220+
cmd.add("mount");
221+
cmd.add("-t");
222+
cmd.add(backupRepoType);
223+
cmd.add(backupRepoAddress);
224+
cmd.add(mountDirectory);
217225
if (Objects.nonNull(mountOptions) && !mountOptions.trim().isEmpty()) {
218-
mount += " -o " + mountOptions;
226+
cmd.add("-o");
227+
cmd.add(mountOptions);
219228
}
229+
commands.add(cmd.toArray(new String[0]));
220230

221-
int exitValue = Script.runSimpleBashScriptForExitValue(mount, mountTimeout, false);
222-
if (exitValue != 0) {
223-
logger.error("Failed to mount repository {} of type {} to the directory {}", backupRepoAddress, backupRepoType, mountDirectory);
231+
Pair<Integer, String> result = Script.executePipedCommands(commands, mountTimeout);
232+
if (result.first() != 0) {
233+
logger.error("Failed to mount repository {} of type {} to the directory {}. Error: {}",
234+
backupRepoAddress, backupRepoType, mountDirectory, result.second());
224235
throw new CloudRuntimeException("Failed to mount the backup repository on the KVM host");
225236
}
237+
226238
return mountDirectory;
227239
}
228240

0 commit comments

Comments
 (0)