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