Skip to content

Commit 93108c3

Browse files
Fix restore/attach backup volume as RBD volume in Ceph storage pool
1 parent 2c0473d commit 93108c3

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

plugins/backup/nas/src/main/java/org/apache/cloudstack/backup/NASBackupProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.cloud.storage.DataStoreRole;
3030
import com.cloud.storage.ScopeType;
3131
import com.cloud.storage.Storage;
32-
import com.cloud.storage.StoragePoolHostVO;
3332
import com.cloud.storage.Volume;
3433
import com.cloud.storage.VolumeApiServiceImpl;
3534
import com.cloud.storage.VolumeVO;
@@ -312,9 +311,11 @@ private Pair<List<PrimaryDataStoreTO>, List<String>> getVolumePoolsAndPaths(List
312311
if (Objects.isNull(storagePool)) {
313312
throw new CloudRuntimeException("Unable to find storage pool associated to the volume");
314313
}
315-
String volumePathPrefix = getVolumePathPrefix(storagePool);
314+
316315
DataStore dataStore = dataStoreMgr.getDataStore(storagePool.getId(), DataStoreRole.Primary);
317316
volumePools.add(dataStore != null ? (PrimaryDataStoreTO)dataStore.getTO() : null);
317+
318+
String volumePathPrefix = getVolumePathPrefix(storagePool);
318319
volumePaths.add(String.format("%s/%s", volumePathPrefix, volume.getPath()));
319320
}
320321
return new Pair<>(volumePools, volumePaths);
@@ -339,7 +340,6 @@ public Pair<Boolean, String> restoreBackedUpVolume(Backup backup, Backup.VolumeI
339340
final DiskOffering diskOffering = diskOfferingDao.findByUuid(backupVolumeInfo.getDiskOfferingId());
340341
final StoragePoolVO pool = primaryDataStoreDao.findByUuid(dataStoreUuid);
341342
final HostVO hostVO = hostDao.findByIp(hostIp);
342-
final StoragePoolHostVO storagePoolHost = storagePoolHostDao.findByPoolHost(pool.getId(), hostVO.getId());
343343

344344
LOG.debug("Restoring vm volume {} from backup {} on the NAS Backup Provider", backupVolumeInfo, backup);
345345
BackupRepository backupRepository = getBackupRepository(backup);
@@ -371,7 +371,7 @@ public Pair<Boolean, String> restoreBackedUpVolume(Backup backup, Backup.VolumeI
371371
restoreCommand.setBackupRepoType(backupRepository.getType());
372372
restoreCommand.setBackupRepoAddress(backupRepository.getAddress());
373373
restoreCommand.setVmName(vmNameAndState.first());
374-
restoreCommand.setRestoreVolumePaths(Collections.singletonList(String.format("%s%s", storagePoolHost != null ? storagePoolHost.getLocalPath() + "/" : "", volumeUUID)));
374+
restoreCommand.setRestoreVolumePaths(Collections.singletonList(String.format("%s/%s", getVolumePathPrefix(pool), volumeUUID)));
375375
DataStore dataStore = dataStoreMgr.getDataStore(pool.getId(), DataStoreRole.Primary);
376376
restoreCommand.setRestoreVolumePools(Collections.singletonList(dataStore != null ? (PrimaryDataStoreTO)dataStore.getTO() : null));
377377
restoreCommand.setDiskType(backupVolumeInfo.getType().name().toLowerCase(Locale.ROOT));

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public class LibvirtRestoreBackupCommandWrapper extends CommandWrapper<RestoreBa
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";
59-
private static final String ATTACH_RBD_DISK_COMMAND = " virsh attach-disk %s /dev/null %s --driver qemu --subdriver raw --type network --source-protocol rbd --source-name %s --cache none";
6059
private static final String ATTACH_RBD_DISK_XML_COMMAND = " virsh attach-device %s /dev/stdin <<EOF%sEOF";
6160
private static final String CURRRENT_DEVICE = "virsh domblklist --domain %s | tail -n 3 | head -n 1 | awk '{print $1}'";
6261
private static final String RSYNC_COMMAND = "rsync -az %s %s";
@@ -150,7 +149,7 @@ private void restoreVolume(KVMStoragePoolManager storagePoolMgr, String backupPa
150149
Pair<String, String> bkpPathAndVolUuid;
151150
try {
152151
bkpPathAndVolUuid = getBackupPath(mountDirectory, volumePath, backupPath, diskType, volumeUUID);
153-
if (!replaceVolumeWithBackup(storagePoolMgr, volumePool, volumePath, bkpPathAndVolUuid.first(), timeout)) {
152+
if (!replaceVolumeWithBackup(storagePoolMgr, volumePool, volumePath, bkpPathAndVolUuid.first(), timeout, true)) {
154153
throw new CloudRuntimeException(String.format("Unable to restore contents from the backup volume [%s].", bkpPathAndVolUuid.second()));
155154
}
156155
if (VirtualMachine.State.Running.equals(vmNameAndState.second())) {
@@ -215,28 +214,34 @@ private Pair<String, String> getBackupPath(String mountDirectory, String volumeP
215214
}
216215

217216
private boolean replaceVolumeWithBackup(KVMStoragePoolManager storagePoolMgr, PrimaryDataStoreTO volumePool, String volumePath, String backupPath, int timeout) {
217+
return replaceVolumeWithBackup(storagePoolMgr, volumePool, volumePath, backupPath, timeout, false);
218+
}
219+
220+
private boolean replaceVolumeWithBackup(KVMStoragePoolManager storagePoolMgr, PrimaryDataStoreTO volumePool, String volumePath, String backupPath, int timeout, boolean createTargetVolume) {
218221
if (volumePool.getPoolType() != Storage.StoragePoolType.RBD) {
219222
int exitValue = Script.runSimpleBashScriptForExitValue(String.format(RSYNC_COMMAND, backupPath, volumePath));
220223
return exitValue == 0;
221224
}
222225

223-
return replaceRbdVolumeWithBackup(storagePoolMgr, volumePool, volumePath, backupPath, timeout);
226+
return replaceRbdVolumeWithBackup(storagePoolMgr, volumePool, volumePath, backupPath, timeout, createTargetVolume);
224227
}
225228

226-
private boolean replaceRbdVolumeWithBackup(KVMStoragePoolManager storagePoolMgr, PrimaryDataStoreTO volumePool, String volumePath, String backupPath, int timeout) {
229+
private boolean replaceRbdVolumeWithBackup(KVMStoragePoolManager storagePoolMgr, PrimaryDataStoreTO volumePool, String volumePath, String backupPath, int timeout, boolean createTargetVolume) {
227230
KVMStoragePool volumeStoragePool = storagePoolMgr.getStoragePool(volumePool.getPoolType(), volumePool.getUuid());
228-
KVMPhysicalDisk rdbDisk = volumeStoragePool.getPhysicalDisk(volumePath);
229-
logger.debug("RBD volume: {}", rdbDisk.toString());
230231
QemuImg qemu;
231232
try {
232233
qemu = new QemuImg(timeout * 1000, true, false);
233-
qemu.setSkipTargetVolumeCreation(true);
234+
if (!createTargetVolume) {
235+
KVMPhysicalDisk rdbDisk = volumeStoragePool.getPhysicalDisk(volumePath);
236+
logger.debug("RBD volume: {}", rdbDisk.toString());
237+
qemu.setSkipTargetVolumeCreation(true);
238+
}
234239
} catch (LibvirtException ex) {
235240
throw new CloudRuntimeException("Failed to create qemu-img command to replace RBD volume with backup", ex);
236241
}
242+
237243
QemuImgFile srcBackupFile = null;
238244
QemuImgFile destVolumeFile = null;
239-
240245
try {
241246
srcBackupFile = new QemuImgFile(backupPath, QemuImg.PhysicalDiskFormat.QCOW2);
242247
String rbdDestVolumeFile = KVMPhysicalDisk.RBDStringBuilder(volumeStoragePool, volumePath);
@@ -261,7 +266,6 @@ private boolean attachVolumeToVm(KVMStoragePoolManager storagePoolMgr, String vm
261266
if (volumePool.getPoolType() != Storage.StoragePoolType.RBD) {
262267
exitValue = Script.runSimpleBashScriptForExitValue(String.format(ATTACH_QCOW2_DISK_COMMAND, vmName, volumePath, deviceToAttachDiskTo));
263268
} else {
264-
// exitValue = Script.runSimpleBashScriptForExitValue(String.format(ATTACH_RBD_DISK_COMMAND, vmName, deviceToAttachDiskTo, volumePath));
265269
String xmlForRbdDisk = getXmlForRbdDisk(storagePoolMgr, volumePool, volumePath, deviceToAttachDiskTo);
266270
logger.debug("RBD disk xml to attach: {}", xmlForRbdDisk);
267271
exitValue = Script.runSimpleBashScriptForExitValue(String.format(ATTACH_RBD_DISK_XML_COMMAND, vmName, xmlForRbdDisk));

0 commit comments

Comments
 (0)