2424import com .linbit .linstor .api .model .ApiCallRcList ;
2525import com .linbit .linstor .api .model .ResourceDefinition ;
2626import com .linbit .linstor .api .model .ResourceDefinitionModify ;
27+ import com .linbit .linstor .api .model .ResourceMakeAvailable ;
2728
2829import javax .inject .Inject ;
2930
@@ -169,11 +170,36 @@ private VolumeVO createNewVolumeVO(Volume volume, StoragePoolVO storagePoolVO) {
169170 return _volumeDao .persist (newVol );
170171 }
171172
173+ private DevelopersApi getLinstorAPI (StoragePoolVO storagePool ) {
174+ return LinstorUtil .getLinstorAPI (storagePool .getHostAddress (),
175+ LinstorConfigurationManager .ApiToken .valueIn (storagePool .getId ()),
176+ Boolean .TRUE .equals (LinstorConfigurationManager .InsecureSsl .valueIn (storagePool .getId ())));
177+ }
178+
179+ /**
180+ * Makes the new resource available on the migration target host and returns its device path there.
181+ *
182+ * <p>The resource is spawned by LINSTOR's auto-placement, which does not know about the target host.
183+ * libvirt can only precreate file disks on the target, so if the block device is missing there the
184+ * migration fails with "cannot precreate storage for disk type 'block'". The source disk is not on
185+ * LINSTOR, so a plain (diskless for DRBD) make-available is enough, no dual-primary needed.</p>
186+ */
187+ private String makeAvailableOnHost (StoragePoolVO storagePool , String rscName , Host host ) {
188+ DevelopersApi api = getLinstorAPI (storagePool );
189+ try {
190+ logger .info ("Linstor: make resource {} available on migration target {}" , rscName , host .getName ());
191+ ApiCallRcList answers = api .resourceMakeAvailableOnNode (rscName , host .getName (), new ResourceMakeAvailable ());
192+ LinstorUtil .checkLinstorAnswersThrow (answers );
193+ return LinstorUtil .getDevicePath (api , rscName );
194+ } catch (ApiException apiEx ) {
195+ logger .error ("Linstor: ApiEx - {}" , apiEx .getMessage ());
196+ throw new CloudRuntimeException (apiEx .getBestMessage (), apiEx );
197+ }
198+ }
199+
172200 private void removeExactSizeProperty (VolumeInfo volumeInfo ) {
173201 StoragePoolVO destStoragePool = _storagePool .findById (volumeInfo .getDataStore ().getId ());
174- DevelopersApi api = LinstorUtil .getLinstorAPI (destStoragePool .getHostAddress (),
175- LinstorConfigurationManager .ApiToken .valueIn (destStoragePool .getId ()),
176- Boolean .TRUE .equals (LinstorConfigurationManager .InsecureSsl .valueIn (destStoragePool .getId ())));
202+ DevelopersApi api = getLinstorAPI (destStoragePool );
177203
178204 ResourceDefinitionModify rdm = new ResourceDefinitionModify ();
179205 rdm .setDeleteProps (Collections .singletonList (LinstorUtil .LIN_PROP_DRBDOPT_EXACT_SIZE ));
@@ -266,10 +292,10 @@ private void handlePostMigration(boolean success, Map<VolumeInfo, VolumeInfo> sr
266292 _volumeService .expungeVolumeAsync (destVolumeInfo );
267293
268294 if (destroyFuture .get ().isFailed ()) {
269- logger .debug ("Failed to clean up dest volume on storage" );
295+ logger .warn ("Failed to clean up dest volume {} on storage" , destVolumeInfo );
270296 }
271297 } catch (Exception e ) {
272- logger .debug ("Failed to clean up dest volume on storage" , e );
298+ logger .warn ("Failed to clean up dest volume {} on storage" , destVolumeInfo , e );
273299 }
274300 }
275301 }
@@ -293,9 +319,7 @@ private void handlePostMigration(boolean success, Map<VolumeInfo, VolumeInfo> sr
293319 private boolean needsExactSizeProp (VolumeInfo srcVolumeInfo ) {
294320 StoragePoolVO srcStoragePool = _storagePool .findById (srcVolumeInfo .getDataStore ().getId ());
295321 if (srcStoragePool .getPoolType () == Storage .StoragePoolType .Linstor ) {
296- DevelopersApi api = LinstorUtil .getLinstorAPI (srcStoragePool .getHostAddress (),
297- LinstorConfigurationManager .ApiToken .valueIn (srcStoragePool .getId ()),
298- Boolean .TRUE .equals (LinstorConfigurationManager .InsecureSsl .valueIn (srcStoragePool .getId ())));
322+ DevelopersApi api = getLinstorAPI (srcStoragePool );
299323
300324 String rscName = LinstorUtil .RSC_PREFIX + srcVolumeInfo .getPath ();
301325 try {
@@ -335,6 +359,7 @@ public void copyAsync(Map<VolumeInfo, DataStore> volumeDataStoreMap, VirtualMach
335359
336360 Map <String , MigrateCommand .MigrateDiskInfo > migrateStorage = new HashMap <>();
337361 Map <VolumeInfo , VolumeInfo > srcVolumeInfoToDestVolumeInfo = new HashMap <>();
362+ boolean postMigrationHandled = false ;
338363
339364 try {
340365 for (Map .Entry <VolumeInfo , DataStore > entry : volumeDataStoreMap .entrySet ()) {
@@ -358,13 +383,17 @@ public void copyAsync(Map<VolumeInfo, DataStore> volumeDataStoreMap, VirtualMach
358383
359384 boolean exactSize = needsExactSizeProp (srcVolumeInfo );
360385
361- String devPath = LinstorUtil .createResource (
362- destVolumeInfo , destStoragePool , _storagePoolDao , exactSize );
386+ LinstorUtil .createResource (destVolumeInfo , destStoragePool , _storagePoolDao , exactSize );
363387
364388 _volumeDao .update (destVolume .getId (), destVolume );
365389 destVolume = _volumeDao .findById (destVolume .getId ());
366390
367391 destVolumeInfo = _volumeDataFactory .getVolume (destVolume .getId (), destDataStore );
392+ // registered right away, so a failure from here on cleans the new resource up again
393+ srcVolumeInfoToDestVolumeInfo .put (srcVolumeInfo , destVolumeInfo );
394+
395+ String devPath = makeAvailableOnHost (
396+ destStoragePool , LinstorUtil .RSC_PREFIX + destVolumeInfo .getUuid (), destHost );
368397
369398 MigrateCommand .MigrateDiskInfo migrateDiskInfo = new MigrateCommand .MigrateDiskInfo (
370399 srcVolumeInfo .getPath (),
@@ -375,8 +404,6 @@ public void copyAsync(Map<VolumeInfo, DataStore> volumeDataStoreMap, VirtualMach
375404 migrateDiskInfoList .add (migrateDiskInfo );
376405
377406 migrateStorage .put (srcVolumeInfo .getPath (), migrateDiskInfo );
378-
379- srcVolumeInfoToDestVolumeInfo .put (srcVolumeInfo , destVolumeInfo );
380407 }
381408
382409 PrepareForMigrationCommand pfmc = new PrepareForMigrationCommand (vmTO );
@@ -413,6 +440,7 @@ public void copyAsync(Map<VolumeInfo, DataStore> volumeDataStoreMap, VirtualMach
413440 MigrateAnswer migrateAnswer = (MigrateAnswer ) _agentManager .send (srcHost .getId (), migrateCommand );
414441 boolean success = migrateAnswer != null && migrateAnswer .getResult ();
415442
443+ postMigrationHandled = true ;
416444 handlePostMigration (success , srcVolumeInfoToDestVolumeInfo , vmTO , destHost );
417445
418446 if (migrateAnswer == null ) {
@@ -427,9 +455,18 @@ public void copyAsync(Map<VolumeInfo, DataStore> volumeDataStoreMap, VirtualMach
427455 } catch (AgentUnavailableException | OperationTimedoutException | CloudRuntimeException ex ) {
428456 errMsg = String .format (
429457 "Copy volume(s) of VM [%s] to storage(s) [%s] and VM to host [%s] failed in LinstorDataMotionStrategy.copyAsync. Error message: [%s]." ,
430- vmTO , srcHost , destHost , ex .getMessage ());
458+ vmTO , volumeDataStoreMap . values () , destHost , ex .getMessage ());
431459 logger .error (errMsg , ex );
432460
461+ if (!postMigrationHandled && !srcVolumeInfoToDestVolumeInfo .isEmpty ()) {
462+ // failed before the migration was started: remove the already created destination volumes
463+ try {
464+ handlePostMigration (false , srcVolumeInfoToDestVolumeInfo , vmTO , destHost );
465+ } catch (Exception e ) {
466+ logger .warn ("Failed to clean up the destination volume(s) of VM [{}]" , vmTO , e );
467+ }
468+ }
469+
433470 throw new CloudRuntimeException (errMsg );
434471 } finally {
435472 CopyCmdAnswer copyCmdAnswer = new CopyCmdAnswer (errMsg );
0 commit comments