@@ -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