@@ -341,10 +341,10 @@ public boolean prepareMigration(NicProfile nic, Network network, VirtualMachineP
341341 try {
342342 if (isConfigDriveIsoOnHostCache (vm .getId ())) {
343343 vm .setConfigDriveLocation (Location .HOST );
344- configureConfigDriveData (vm , nic , dest );
345-
346- // Create the config drive on dest host cache
347- createConfigDriveIsoOnHostCache ( vm , dest . getHost (). getId ());
344+ if ( configureConfigDriveData (vm , nic , dest )) {
345+ // Create the config drive on dest host cache
346+ createConfigDriveIsoOnHostCache ( vm , dest . getHost (). getId ());
347+ }
348348 } else {
349349 vm .setConfigDriveLocation (getConfigDriveLocation (vm .getId ()));
350350 addPasswordAndUserdata (network , nic , vm , dest , context );
@@ -373,7 +373,7 @@ public void rollbackMigration(NicProfile nic, Network network, VirtualMachinePro
373373 @ Override
374374 public void commitMigration (NicProfile nic , Network network , VirtualMachineProfile vm , ReservationContext src , ReservationContext dst ) {
375375 try {
376- if (isConfigDriveIsoOnHostCache (vm .getId ())) {
376+ if (isLastConfigDriveIsoOnHostCache (vm .getId ())) {
377377 vm .setConfigDriveLocation (Location .HOST );
378378 // Delete the config drive on src host cache
379379 deleteConfigDriveIsoOnHostCache (vm .getVirtualMachine (), vm .getHostId ());
@@ -530,6 +530,17 @@ private boolean isConfigDriveIsoOnHostCache(long vmId) {
530530 return false ;
531531 }
532532
533+ private boolean isLastConfigDriveIsoOnHostCache (long vmId ) {
534+ final UserVmDetailVO vmDetailLastConfigDriveLocation = _userVmDetailsDao .findDetail (vmId , VmDetailConstants .LAST_CONFIG_DRIVE_LOCATION );
535+ if (vmDetailLastConfigDriveLocation == null ) {
536+ return isConfigDriveIsoOnHostCache (vmId );
537+ }
538+ if (Location .HOST .toString ().equalsIgnoreCase (vmDetailLastConfigDriveLocation .getValue ())) {
539+ return true ;
540+ }
541+ return false ;
542+ }
543+
533544 private boolean createConfigDriveIsoOnHostCache (VirtualMachineProfile profile , Long hostId ) throws ResourceUnavailableException {
534545 if (hostId == null ) {
535546 throw new ResourceUnavailableException ("Config drive iso creation failed, dest host not available" ,
@@ -556,7 +567,7 @@ private boolean createConfigDriveIsoOnHostCache(VirtualMachineProfile profile, L
556567 }
557568
558569 profile .setConfigDriveLocation (answer .getConfigDriveLocation ());
559- _userVmDetailsDao . addDetail (profile .getId (), VmDetailConstants . CONFIG_DRIVE_LOCATION , answer .getConfigDriveLocation (). toString (), false );
570+ updateConfigDriveLocationInVMDetails (profile .getId (), answer .getConfigDriveLocation ());
560571 addConfigDriveDisk (profile , null );
561572 return true ;
562573 }
@@ -618,11 +629,23 @@ private boolean createConfigDriveIso(VirtualMachineProfile profile, DeployDestin
618629 answer .getDetails ()), ConfigDriveNetworkElement .class , 0L );
619630 }
620631 profile .setConfigDriveLocation (answer .getConfigDriveLocation ());
621- _userVmDetailsDao . addDetail (profile .getId (), VmDetailConstants . CONFIG_DRIVE_LOCATION , answer .getConfigDriveLocation (). toString (), false );
632+ updateConfigDriveLocationInVMDetails (profile .getId (), answer .getConfigDriveLocation ());
622633 addConfigDriveDisk (profile , dataStore );
623634 return true ;
624635 }
625636
637+ private void updateConfigDriveLocationInVMDetails (long vmId , NetworkElement .Location configDriveLocation ) {
638+ final UserVmDetailVO vmDetailConfigDriveLocation = _userVmDetailsDao .findDetail (vmId , VmDetailConstants .CONFIG_DRIVE_LOCATION );
639+ if (vmDetailConfigDriveLocation != null ) {
640+ if (!configDriveLocation .toString ().equalsIgnoreCase (vmDetailConfigDriveLocation .getValue ())) {
641+ _userVmDetailsDao .addDetail (vmId , VmDetailConstants .LAST_CONFIG_DRIVE_LOCATION , vmDetailConfigDriveLocation .getValue (), false );
642+ } else {
643+ _userVmDetailsDao .removeDetail (vmId , VmDetailConstants .LAST_CONFIG_DRIVE_LOCATION );
644+ }
645+ }
646+ _userVmDetailsDao .addDetail (vmId , VmDetailConstants .CONFIG_DRIVE_LOCATION , configDriveLocation .toString (), false );
647+ }
648+
626649 private Map <String , String > getVMCustomUserdataParamMap (long vmId ) {
627650 UserVmVO userVm = _userVmDao .findById (vmId );
628651 String userDataDetails = userVm .getUserDataDetails ();
@@ -737,7 +760,7 @@ private void addConfigDriveDisk(final VirtualMachineProfile profile, final DataS
737760
738761 private boolean configureConfigDriveData (final VirtualMachineProfile profile , final NicProfile nic , final DeployDestination dest ) {
739762 final UserVmVO vm = _userVmDao .findById (profile .getId ());
740- if (vm .getType () != VirtualMachine .Type .User ) {
763+ if (vm == null || vm .getType () != VirtualMachine .Type .User ) {
741764 return false ;
742765 }
743766 final Nic defaultNic = _networkModel .getDefaultNic (vm .getId ());
0 commit comments