Skip to content

Commit fef4c79

Browse files
author
Mike Tutkowski
committed
CLOUDSTACK-5873: [Automation] Failed to attach volume to VM, if the vm is created with option startvm=false
1 parent 032888c commit fef4c79

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

‎engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ public ChapInfo getChapInfo(VolumeInfo volumeInfo, DataStore dataStore) {
159159
return null;
160160
}
161161

162+
@Override
162163
public boolean connectVolumeToHost(VolumeInfo volumeInfo, Host host, DataStore dataStore) {
163-
DataStoreDriver dataStoreDriver = dataStore.getDriver();
164+
DataStoreDriver dataStoreDriver = dataStore != null ? dataStore.getDriver() : null;
164165

165166
if (dataStoreDriver instanceof PrimaryDataStoreDriver) {
166167
return ((PrimaryDataStoreDriver)dataStoreDriver).connectVolumeToHost(volumeInfo, host, dataStore);
@@ -169,8 +170,9 @@ public boolean connectVolumeToHost(VolumeInfo volumeInfo, Host host, DataStore d
169170
return false;
170171
}
171172

173+
@Override
172174
public void disconnectVolumeFromHost(VolumeInfo volumeInfo, Host host, DataStore dataStore) {
173-
DataStoreDriver dataStoreDriver = dataStore.getDriver();
175+
DataStoreDriver dataStoreDriver = dataStore != null ? dataStore.getDriver() : null;
174176

175177
if (dataStoreDriver instanceof PrimaryDataStoreDriver) {
176178
((PrimaryDataStoreDriver)dataStoreDriver).disconnectVolumeFromHost(volumeInfo, host, dataStore);

‎plugins/hypervisors/xen/src/com/cloud/hypervisor/XenServerGuru.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ public List<Command> finalizeExpungeVolumes(VirtualMachine vm) {
101101
if (volume.getVolumeType() == Volume.Type.DATADISK) {
102102
StoragePoolVO storagePool = _storagePoolDao.findById(volume.getPoolId());
103103

104-
if (storagePool.isManaged()) {
104+
// storagePool should be null if we are expunging a volume that was never
105+
// attached to a VM that was started (the "trick" for storagePool to be null
106+
// is that none of the VMs this volume may have been attached to were ever started,
107+
// so the volume was never assigned to a storage pool)
108+
if (storagePool != null && storagePool.isManaged()) {
105109
DataTO volTO = _volFactory.getVolume(volume.getId()).getTO();
106110
DiskTO disk = new DiskTO(volTO, volume.getDeviceId(), volume.getPath(), volume.getVolumeType());
107111

‎server/src/com/cloud/storage/VolumeApiServiceImpl.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,12 +1489,13 @@ private Volume orchestrateDetachVolumeFromVM(long vmId, long volumeId) {
14891489
}
14901490
}
14911491

1492-
DataStore dataStore = dataStoreMgr.getDataStore(volume.getPoolId(), DataStoreRole.Primary);
1493-
14941492
if (!sendCommand || (answer != null && answer.getResult())) {
14951493
// Mark the volume as detached
14961494
_volsDao.detachVolume(volume.getId());
14971495

1496+
// volume.getPoolId() should be null if the VM we are attaching the disk to has never been started before
1497+
DataStore dataStore = volume.getPoolId() != null ? dataStoreMgr.getDataStore(volume.getPoolId(), DataStoreRole.Primary) : null;
1498+
14981499
volService.disconnectVolumeFromHost(volFactory.getVolume(volume.getId()), host, dataStore);
14991500

15001501
return _volsDao.findById(volumeId);
@@ -1961,10 +1962,12 @@ private VolumeVO sendAttachVolumeCommand(UserVmVO vm, VolumeVO volumeToAttach, L
19611962
}
19621963
}
19631964

1964-
DataStore dataStore = dataStoreMgr.getDataStore(volumeToAttachStoragePool.getId(), DataStoreRole.Primary);
1965+
// volumeToAttachStoragePool should be null if the VM we are attaching the disk to has never been started before
1966+
DataStore dataStore = volumeToAttachStoragePool != null ? dataStoreMgr.getDataStore(volumeToAttachStoragePool.getId(), DataStoreRole.Primary) : null;
19651967

19661968
boolean queryForChap = true;
19671969

1970+
// if we don't have a host, the VM we are attaching the disk to has never been started before
19681971
if (host != null) {
19691972
try {
19701973
// if connectVolumeToHost returns true, then we do not want to use CHAP because the volume is already connected to the host(s)

0 commit comments

Comments
 (0)