Skip to content

Commit 792b80f

Browse files
Fix restore/attach backup volume as RBD volume in Ceph storage pool
1 parent 5397375 commit 792b80f

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;
@@ -337,9 +336,11 @@ private Pair<List<PrimaryDataStoreTO>, List<String>> getVolumePoolsAndPaths(List
337336
if (Objects.isNull(storagePool)) {
338337
throw new CloudRuntimeException("Unable to find storage pool associated to the volume");
339338
}
340-
String volumePathPrefix = getVolumePathPrefix(storagePool);
339+
341340
DataStore dataStore = dataStoreMgr.getDataStore(storagePool.getId(), DataStoreRole.Primary);
342341
volumePools.add(dataStore != null ? (PrimaryDataStoreTO)dataStore.getTO() : null);
342+
343+
String volumePathPrefix = getVolumePathPrefix(storagePool);
343344
volumePaths.add(String.format("%s/%s", volumePathPrefix, volume.getPath()));
344345
}
345346
return new Pair<>(volumePools, volumePaths);
@@ -364,7 +365,6 @@ public Pair<Boolean, String> restoreBackedUpVolume(Backup backup, Backup.VolumeI
364365
final DiskOffering diskOffering = diskOfferingDao.findByUuid(backupVolumeInfo.getDiskOfferingId());
365366
final StoragePoolVO pool = primaryDataStoreDao.findByUuid(dataStoreUuid);
366367
final HostVO hostVO = hostDao.findByIp(hostIp);
367-
final StoragePoolHostVO storagePoolHost = storagePoolHostDao.findByPoolHost(pool.getId(), hostVO.getId());
368368

369369
LOG.debug("Restoring vm volume {} from backup {} on the NAS Backup Provider", backupVolumeInfo, backup);
370370
BackupRepository backupRepository = getBackupRepository(backup);
@@ -396,7 +396,7 @@ public Pair<Boolean, String> restoreBackedUpVolume(Backup backup, Backup.VolumeI
396396
restoreCommand.setBackupRepoType(backupRepository.getType());
397397
restoreCommand.setBackupRepoAddress(backupRepository.getAddress());
398398
restoreCommand.setVmName(vmNameAndState.first());
399-
restoreCommand.setRestoreVolumePaths(Collections.singletonList(String.format("%s%s", storagePoolHost != null ? storagePoolHost.getLocalPath() + "/" : "", volumeUUID)));
399+
restoreCommand.setRestoreVolumePaths(Collections.singletonList(String.format("%s/%s", getVolumePathPrefix(pool), volumeUUID)));
400400
DataStore dataStore = dataStoreMgr.getDataStore(pool.getId(), DataStoreRole.Primary);
401401
restoreCommand.setRestoreVolumePools(Collections.singletonList(dataStore != null ? (PrimaryDataStoreTO)dataStore.getTO() : null));
402402
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";
@@ -156,7 +155,7 @@ private void restoreVolume(KVMStoragePoolManager storagePoolMgr, String backupPa
156155
try {
157156
bkpPathAndVolUuid = getBackupPath(mountDirectory, volumePath, backupPath, diskType, volumeUUID);
158157
verifyBackupFile(bkpPathAndVolUuid.first(), bkpPathAndVolUuid.second());
159-
if (!replaceVolumeWithBackup(storagePoolMgr, volumePool, volumePath, bkpPathAndVolUuid.first(), timeout)) {
158+
if (!replaceVolumeWithBackup(storagePoolMgr, volumePool, volumePath, bkpPathAndVolUuid.first(), timeout, true)) {
160159
throw new CloudRuntimeException(String.format("Unable to restore contents from the backup volume [%s].", bkpPathAndVolUuid.second()));
161160
}
162161
if (VirtualMachine.State.Running.equals(vmNameAndState.second())) {
@@ -239,28 +238,34 @@ private boolean checkBackupPathExists(String backupPath) {
239238
}
240239

241240
private boolean replaceVolumeWithBackup(KVMStoragePoolManager storagePoolMgr, PrimaryDataStoreTO volumePool, String volumePath, String backupPath, int timeout) {
241+
return replaceVolumeWithBackup(storagePoolMgr, volumePool, volumePath, backupPath, timeout, false);
242+
}
243+
244+
private boolean replaceVolumeWithBackup(KVMStoragePoolManager storagePoolMgr, PrimaryDataStoreTO volumePool, String volumePath, String backupPath, int timeout, boolean createTargetVolume) {
242245
if (volumePool.getPoolType() != Storage.StoragePoolType.RBD) {
243246
int exitValue = Script.runSimpleBashScriptForExitValue(String.format(RSYNC_COMMAND, backupPath, volumePath));
244247
return exitValue == 0;
245248
}
246249

247-
return replaceRbdVolumeWithBackup(storagePoolMgr, volumePool, volumePath, backupPath, timeout);
250+
return replaceRbdVolumeWithBackup(storagePoolMgr, volumePool, volumePath, backupPath, timeout, createTargetVolume);
248251
}
249252

250-
private boolean replaceRbdVolumeWithBackup(KVMStoragePoolManager storagePoolMgr, PrimaryDataStoreTO volumePool, String volumePath, String backupPath, int timeout) {
253+
private boolean replaceRbdVolumeWithBackup(KVMStoragePoolManager storagePoolMgr, PrimaryDataStoreTO volumePool, String volumePath, String backupPath, int timeout, boolean createTargetVolume) {
251254
KVMStoragePool volumeStoragePool = storagePoolMgr.getStoragePool(volumePool.getPoolType(), volumePool.getUuid());
252-
KVMPhysicalDisk rdbDisk = volumeStoragePool.getPhysicalDisk(volumePath);
253-
logger.debug("RBD volume: {}", rdbDisk.toString());
254255
QemuImg qemu;
255256
try {
256257
qemu = new QemuImg(timeout * 1000, true, false);
257-
qemu.setSkipTargetVolumeCreation(true);
258+
if (!createTargetVolume) {
259+
KVMPhysicalDisk rdbDisk = volumeStoragePool.getPhysicalDisk(volumePath);
260+
logger.debug("RBD volume: {}", rdbDisk.toString());
261+
qemu.setSkipTargetVolumeCreation(true);
262+
}
258263
} catch (LibvirtException ex) {
259264
throw new CloudRuntimeException("Failed to create qemu-img command to replace RBD volume with backup", ex);
260265
}
266+
261267
QemuImgFile srcBackupFile = null;
262268
QemuImgFile destVolumeFile = null;
263-
264269
try {
265270
srcBackupFile = new QemuImgFile(backupPath, QemuImg.PhysicalDiskFormat.QCOW2);
266271
String rbdDestVolumeFile = KVMPhysicalDisk.RBDStringBuilder(volumeStoragePool, volumePath);
@@ -285,7 +290,6 @@ private boolean attachVolumeToVm(KVMStoragePoolManager storagePoolMgr, String vm
285290
if (volumePool.getPoolType() != Storage.StoragePoolType.RBD) {
286291
exitValue = Script.runSimpleBashScriptForExitValue(String.format(ATTACH_QCOW2_DISK_COMMAND, vmName, volumePath, deviceToAttachDiskTo));
287292
} else {
288-
// exitValue = Script.runSimpleBashScriptForExitValue(String.format(ATTACH_RBD_DISK_COMMAND, vmName, deviceToAttachDiskTo, volumePath));
289293
String xmlForRbdDisk = getXmlForRbdDisk(storagePoolMgr, volumePool, volumePath, deviceToAttachDiskTo);
290294
logger.debug("RBD disk xml to attach: {}", xmlForRbdDisk);
291295
exitValue = Script.runSimpleBashScriptForExitValue(String.format(ATTACH_RBD_DISK_XML_COMMAND, vmName, xmlForRbdDisk));

0 commit comments

Comments
 (0)