@@ -497,7 +497,7 @@ private Long findAgentId(VirtualMachineProfile profile, DeployDestination dest,
497497 agentId = dest .getHost ().getId ();
498498 }
499499 if (!VirtualMachineManager .VmConfigDriveOnPrimaryPool .valueIn (dest .getDataCenter ().getId ()) &&
500- !VirtualMachineManager .VmConfigDriveForceHostCacheUse .valueIn (dest .getDataCenter ().getId ())) {
500+ !VirtualMachineManager .VmConfigDriveForceHostCacheUse .valueIn (dest .getDataCenter ().getId ()) && dataStore != null ) {
501501 agentId = findAgentIdForImageStore (dataStore );
502502 }
503503 return agentId ;
@@ -630,6 +630,10 @@ private DataStore getDatastoreForConfigDriveIso(DiskTO disk, VirtualMachineProfi
630630 private boolean deleteConfigDriveIso (final VirtualMachine vm ) throws ResourceUnavailableException {
631631 Long hostId = (vm .getHostId () != null ) ? vm .getHostId () : vm .getLastHostId ();
632632 Location location = getConfigDriveLocation (vm .getId ());
633+ if (hostId == null ) {
634+ LOG .info (String .format ("The VM was never booted; no config-drive ISO created for VM %s" , vm .getName ()));
635+ return true ;
636+ }
633637 if (location == Location .HOST ) {
634638 return deleteConfigDriveIsoOnHostCache (vm , hostId );
635639 }
@@ -639,7 +643,9 @@ private boolean deleteConfigDriveIso(final VirtualMachine vm) throws ResourceUna
639643
640644 if (location == Location .SECONDARY ) {
641645 dataStore = _dataStoreMgr .getImageStoreWithFreeCapacity (vm .getDataCenterId ());
642- agentId = findAgentIdForImageStore (dataStore );
646+ if (dataStore != null ) {
647+ agentId = findAgentIdForImageStore (dataStore );
648+ }
643649 } else if (location == Location .PRIMARY ) {
644650 List <VolumeVO > volumes = _volumeDao .findByInstanceAndType (vm .getId (), Volume .Type .ROOT );
645651 if (volumes != null && volumes .size () > 0 ) {
0 commit comments