2626import com .cloud .hypervisor .Hypervisor ;
2727import com .cloud .offering .DiskOffering ;
2828import com .cloud .resource .ResourceManager ;
29+ import com .cloud .storage .DataStoreRole ;
2930import com .cloud .storage .ScopeType ;
3031import com .cloud .storage .Storage ;
31- import com .cloud .storage .StoragePoolHostVO ;
3232import com .cloud .storage .Volume ;
3333import com .cloud .storage .VolumeApiServiceImpl ;
3434import com .cloud .storage .VolumeVO ;
4949
5050import org .apache .cloudstack .backup .dao .BackupDao ;
5151import org .apache .cloudstack .backup .dao .BackupRepositoryDao ;
52+ import org .apache .cloudstack .engine .subsystem .api .storage .DataStore ;
53+ import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreManager ;
5254import org .apache .cloudstack .framework .config .ConfigKey ;
5355import org .apache .cloudstack .framework .config .Configurable ;
5456import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
5557import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
58+ import org .apache .cloudstack .storage .to .PrimaryDataStoreTO ;
5659import org .apache .logging .log4j .Logger ;
5760import org .apache .logging .log4j .LogManager ;
5861
@@ -106,6 +109,9 @@ public class NASBackupProvider extends AdapterBase implements BackupProvider, Co
106109 @ Inject
107110 private PrimaryDataStoreDao primaryDataStoreDao ;
108111
112+ @ Inject
113+ DataStoreManager dataStoreMgr ;
114+
109115 @ Inject
110116 private AgentManager agentManager ;
111117
@@ -203,8 +209,9 @@ public Pair<Boolean, Backup> takeBackup(final VirtualMachine vm, Boolean quiesce
203209 if (VirtualMachine .State .Stopped .equals (vm .getState ())) {
204210 List <VolumeVO > vmVolumes = volumeDao .findByInstance (vm .getId ());
205211 vmVolumes .sort (Comparator .comparing (Volume ::getDeviceId ));
206- List <String > volumePaths = getVolumePaths (vmVolumes );
207- command .setVolumePaths (volumePaths );
212+ Pair <List <PrimaryDataStoreTO >, List <String >> volumePoolsAndPaths = getVolumePoolsAndPaths (vmVolumes );
213+ command .setVolumePools (volumePoolsAndPaths .first ());
214+ command .setVolumePaths (volumePoolsAndPaths .second ());
208215 }
209216
210217 BackupAnswer answer ;
@@ -303,7 +310,9 @@ private Pair<Boolean, String> restoreVMBackup(VirtualMachine vm, Backup backup)
303310 restoreCommand .setMountOptions (backupRepository .getMountOptions ());
304311 restoreCommand .setVmName (vm .getName ());
305312 restoreCommand .setBackupVolumesUUIDs (backedVolumesUUIDs );
306- restoreCommand .setRestoreVolumePaths (getVolumePaths (restoreVolumes ));
313+ Pair <List <PrimaryDataStoreTO >, List <String >> volumePoolsAndPaths = getVolumePoolsAndPaths (restoreVolumes );
314+ restoreCommand .setRestoreVolumePools (volumePoolsAndPaths .first ());
315+ restoreCommand .setRestoreVolumePaths (volumePoolsAndPaths .second ());
307316 restoreCommand .setVmExists (vm .getRemoved () == null );
308317 restoreCommand .setVmState (vm .getState ());
309318 restoreCommand .setMountTimeout (NASBackupRestoreMountTimeout .value ());
@@ -319,31 +328,42 @@ private Pair<Boolean, String> restoreVMBackup(VirtualMachine vm, Backup backup)
319328 return new Pair <>(answer .getResult (), answer .getDetails ());
320329 }
321330
322- private List <String > getVolumePaths (List <VolumeVO > volumes ) {
331+ private Pair <List <PrimaryDataStoreTO >, List <String >> getVolumePoolsAndPaths (List <VolumeVO > volumes ) {
332+ List <PrimaryDataStoreTO > volumePools = new ArrayList <>();
323333 List <String > volumePaths = new ArrayList <>();
324334 for (VolumeVO volume : volumes ) {
325335 StoragePoolVO storagePool = primaryDataStoreDao .findById (volume .getPoolId ());
326336 if (Objects .isNull (storagePool )) {
327337 throw new CloudRuntimeException ("Unable to find storage pool associated to the volume" );
328338 }
329- String volumePathPrefix ;
330- if (ScopeType .HOST .equals (storagePool .getScope ())) {
331- volumePathPrefix = storagePool .getPath ();
332- } else if (Storage .StoragePoolType .SharedMountPoint .equals (storagePool .getPoolType ())) {
333- volumePathPrefix = storagePool .getPath ();
334- } else {
335- volumePathPrefix = String .format ("/mnt/%s" , storagePool .getUuid ());
336- }
339+
340+ DataStore dataStore = dataStoreMgr .getDataStore (storagePool .getId (), DataStoreRole .Primary );
341+ volumePools .add (dataStore != null ? (PrimaryDataStoreTO )dataStore .getTO () : null );
342+
343+ String volumePathPrefix = getVolumePathPrefix (storagePool );
337344 volumePaths .add (String .format ("%s/%s" , volumePathPrefix , volume .getPath ()));
338345 }
339- return volumePaths ;
346+ return new Pair <>(volumePools , volumePaths );
347+ }
348+
349+ private String getVolumePathPrefix (StoragePoolVO storagePool ) {
350+ String volumePathPrefix ;
351+ if (ScopeType .HOST .equals (storagePool .getScope ()) ||
352+ Storage .StoragePoolType .SharedMountPoint .equals (storagePool .getPoolType ()) ||
353+ Storage .StoragePoolType .RBD .equals (storagePool .getPoolType ())) {
354+ volumePathPrefix = storagePool .getPath ();
355+ } else {
356+ // Should be Storage.StoragePoolType.NetworkFilesystem
357+ volumePathPrefix = String .format ("/mnt/%s" , storagePool .getUuid ());
358+ }
359+ return volumePathPrefix ;
340360 }
341361
342362 @ Override
343363 public Pair <Boolean , String > restoreBackedUpVolume (Backup backup , Backup .VolumeInfo backupVolumeInfo , String hostIp , String dataStoreUuid , Pair <String , VirtualMachine .State > vmNameAndState ) {
344364 final VolumeVO volume = volumeDao .findByUuid (backupVolumeInfo .getUuid ());
345365 final DiskOffering diskOffering = diskOfferingDao .findByUuid (backupVolumeInfo .getDiskOfferingId ());
346- final StoragePoolHostVO dataStore = storagePoolHostDao .findByUuid (dataStoreUuid );
366+ final StoragePoolVO pool = primaryDataStoreDao .findByUuid (dataStoreUuid );
347367 final HostVO hostVO = hostDao .findByIp (hostIp );
348368
349369 LOG .debug ("Restoring vm volume {} from backup {} on the NAS Backup Provider" , backupVolumeInfo , backup );
@@ -360,19 +380,26 @@ public Pair<Boolean, String> restoreBackedUpVolume(Backup backup, Backup.VolumeI
360380 restoredVolume .setUuid (volumeUUID );
361381 restoredVolume .setRemoved (null );
362382 restoredVolume .setDisplayVolume (true );
363- restoredVolume .setPoolId (dataStore .getPoolId ());
383+ restoredVolume .setPoolId (pool .getId ());
384+ restoredVolume .setPoolType (pool .getPoolType ());
364385 restoredVolume .setPath (restoredVolume .getUuid ());
365386 restoredVolume .setState (Volume .State .Copying );
366- restoredVolume .setFormat (Storage .ImageFormat .QCOW2 );
367387 restoredVolume .setSize (backupVolumeInfo .getSize ());
368388 restoredVolume .setDiskOfferingId (diskOffering .getId ());
389+ if (pool .getPoolType () != Storage .StoragePoolType .RBD ) {
390+ restoredVolume .setFormat (Storage .ImageFormat .QCOW2 );
391+ } else {
392+ restoredVolume .setFormat (Storage .ImageFormat .RAW );
393+ }
369394
370395 RestoreBackupCommand restoreCommand = new RestoreBackupCommand ();
371396 restoreCommand .setBackupPath (backup .getExternalId ());
372397 restoreCommand .setBackupRepoType (backupRepository .getType ());
373398 restoreCommand .setBackupRepoAddress (backupRepository .getAddress ());
374399 restoreCommand .setVmName (vmNameAndState .first ());
375- restoreCommand .setRestoreVolumePaths (Collections .singletonList (String .format ("%s/%s" , dataStore .getLocalPath (), volumeUUID )));
400+ restoreCommand .setRestoreVolumePaths (Collections .singletonList (String .format ("%s/%s" , getVolumePathPrefix (pool ), volumeUUID )));
401+ DataStore dataStore = dataStoreMgr .getDataStore (pool .getId (), DataStoreRole .Primary );
402+ restoreCommand .setRestoreVolumePools (Collections .singletonList (dataStore != null ? (PrimaryDataStoreTO )dataStore .getTO () : null ));
376403 restoreCommand .setDiskType (backupVolumeInfo .getType ().name ().toLowerCase (Locale .ROOT ));
377404 restoreCommand .setMountOptions (backupRepository .getMountOptions ());
378405 restoreCommand .setVmExists (null );
0 commit comments