Skip to content

Commit e2364cf

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 151c239 commit e2364cf

4 files changed

Lines changed: 86 additions & 20 deletions

File tree

‎engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,15 +1123,17 @@ private void handlePath(DiskTO[] disks, Map<String, String> iqnToPath) {
11231123
Map<String, String> details = disk.getDetails();
11241124
boolean isManaged = details != null && Boolean.parseBoolean(details.get(DiskTO.MANAGED));
11251125

1126-
if (isManaged && disk.getPath() == null) {
1126+
if (isManaged) {
11271127
Long volumeId = disk.getData().getId();
11281128
VolumeVO volume = _volsDao.findById(volumeId);
11291129
String iScsiName = volume.get_iScsiName();
11301130
String path = iqnToPath.get(iScsiName);
11311131

1132-
volume.setPath(path);
1132+
if (path != null) {
1133+
volume.setPath(path);
11331134

1134-
_volsDao.update(volumeId, volume);
1135+
_volsDao.update(volumeId, volume);
1136+
}
11351137
}
11361138
}
11371139
}

‎engine/orchestration/src/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ public VolumeInfo copyVolumeFromSecToPrimary(VolumeInfo volume, VirtualMachine v
438438
@DB
439439
public VolumeInfo createVolume(VolumeInfo volume, VirtualMachine vm, VirtualMachineTemplate template, DataCenter dc, Pod pod, Long clusterId, ServiceOffering offering,
440440
DiskOffering diskOffering, List<StoragePool> avoids, long size, HypervisorType hyperType) {
441+
// update the volume's hypervisor_ss_reserve from its disk offering (used for managed storage)
441442
volume = updateHypervisorSnapshotReserveForVolume(diskOffering, volume, hyperType);
442443

443444
StoragePool pool = null;
@@ -1156,6 +1157,14 @@ private Pair<VolumeVO, DataStore> recreateVolume(VolumeVO vol, VirtualMachinePro
11561157
// retry one more time in case of template reload is required for Vmware case
11571158
AsyncCallFuture<VolumeApiResult> future = null;
11581159
if (templateId == null) {
1160+
DiskOffering diskOffering = _entityMgr.findById(DiskOffering.class, volume.getDiskOfferingId());
1161+
HypervisorType hyperType = vm.getVirtualMachine().getHypervisorType();
1162+
1163+
// update the volume's hypervisor_ss_reserve from its disk offering (used for managed storage)
1164+
updateHypervisorSnapshotReserveForVolume(diskOffering, volume, hyperType);
1165+
1166+
volume = volFactory.getVolume(newVol.getId(), destPool);
1167+
11591168
future = volService.createVolumeAsync(volume, destPool);
11601169
} else {
11611170
TemplateInfo templ = tmplFactory.getTemplate(templateId, DataStoreRole.Image);
@@ -1174,6 +1183,16 @@ private Pair<VolumeVO, DataStore> recreateVolume(VolumeVO vol, VirtualMachinePro
11741183
throw new StorageUnavailableException("Unable to create " + newVol + ":" + result.getResult(), destPool.getId());
11751184
}
11761185
}
1186+
1187+
StoragePoolVO storagePool = _storagePoolDao.findById(destPool.getId());
1188+
1189+
if (newVol.getVolumeType() == Type.DATADISK && storagePool.isManaged()) {
1190+
long hostId = vm.getVirtualMachine().getHostId();
1191+
Host host = _hostDao.findById(hostId);
1192+
1193+
volService.connectVolumeToHost(volFactory.getVolume(newVol.getId()), host, destPool);
1194+
}
1195+
11771196
newVol = _volsDao.findById(newVol.getId());
11781197
break; //break out of template-redeploy retry loop
11791198
} catch (InterruptedException e) {

‎plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java‎

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,10 +1192,12 @@ protected VDI mount(Connection conn, String vmName, DiskTO volume) throws XmlRpc
11921192
}
11931193
}
11941194

1195-
protected VBD createVbd(Connection conn, DiskTO volume, String vmName, VM vm, BootloaderType bootLoaderType) throws XmlRpcException, XenAPIException {
1195+
protected VBD createVbd(Connection conn, DiskTO volume, String vmName, VM vm, BootloaderType bootLoaderType, VDI vdi) throws XmlRpcException, XenAPIException {
11961196
Volume.Type type = volume.getType();
11971197

1198-
VDI vdi = mount(conn, vmName, volume);
1198+
if (vdi == null) {
1199+
vdi = mount(conn, vmName, volume);
1200+
}
11991201

12001202
if ( vdi != null ) {
12011203
if ("detached".equals(vdi.getNameLabel(conn))) {
@@ -1686,7 +1688,15 @@ public StartAnswer execute(StartCommand cmd) {
16861688
vm = createVmFromTemplate(conn, vmSpec, host);
16871689

16881690
for (DiskTO disk : vmSpec.getDisks()) {
1689-
createVbd(conn, disk, vmName, vm, vmSpec.getBootloader());
1691+
VDI newVdi = prepareManagedDisk(conn, disk, vmName);
1692+
1693+
if (newVdi != null) {
1694+
String path = newVdi.getUuid(conn);
1695+
1696+
iqnToPath.put(disk.getDetails().get(DiskTO.IQN), path);
1697+
}
1698+
1699+
createVbd(conn, disk, vmName, vm, vmSpec.getBootloader(), newVdi);
16901700
}
16911701

16921702
if (vmSpec.getType() != VirtualMachine.Type.User) {
@@ -1796,6 +1806,53 @@ public StartAnswer execute(StartCommand cmd) {
17961806
}
17971807
}
17981808

1809+
// the idea here is to see if the DiskTO in question is from managed storage and
1810+
// does not yet have an SR
1811+
// if no SR, create it and create a VDI in it
1812+
private VDI prepareManagedDisk(Connection conn, DiskTO disk, String vmName) throws Exception {
1813+
Map<String, String> details = disk.getDetails();
1814+
1815+
if (details == null) {
1816+
return null;
1817+
}
1818+
1819+
boolean isManaged = new Boolean(details.get(DiskTO.MANAGED)).booleanValue();
1820+
1821+
if (!isManaged) {
1822+
return null;
1823+
}
1824+
1825+
String iqn = details.get(DiskTO.IQN);
1826+
1827+
Set<SR> srNameLabels = SR.getByNameLabel(conn, iqn);
1828+
1829+
if (srNameLabels.size() != 0) {
1830+
return null;
1831+
}
1832+
1833+
String vdiNameLabel = vmName + "-DATA";
1834+
1835+
return prepareManagedStorage(conn, details, null, vdiNameLabel);
1836+
}
1837+
1838+
protected VDI prepareManagedStorage(Connection conn, Map<String, String> details, String path, String vdiNameLabel) throws Exception {
1839+
String iScsiName = details.get(DiskTO.IQN);
1840+
String storageHost = details.get(DiskTO.STORAGE_HOST);
1841+
String chapInitiatorUsername = details.get(DiskTO.CHAP_INITIATOR_USERNAME);
1842+
String chapInitiatorSecret = details.get(DiskTO.CHAP_INITIATOR_SECRET);
1843+
Long volumeSize = Long.parseLong(details.get(DiskTO.VOLUME_SIZE));
1844+
1845+
SR sr = getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, true);
1846+
1847+
VDI vdi = getVDIbyUuid(conn, path, false);
1848+
1849+
if (vdi == null) {
1850+
vdi = createVdi(sr, vdiNameLabel, volumeSize);
1851+
}
1852+
1853+
return vdi;
1854+
}
1855+
17991856
protected Answer execute(ModifySshKeysCommand cmd) {
18001857
return new Answer(cmd);
18011858
}

‎plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServerStorageProcessor.java‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public AttachAnswer attachVolume(AttachCommand cmd) {
185185
catch (CloudRuntimeException ex) {
186186
}
187187

188-
Map<String, String> details = cmd.getDisk().getDetails();
188+
Map<String, String> details = disk.getDetails();
189189
boolean isManaged = Boolean.parseBoolean(details.get(DiskTO.MANAGED));
190190

191191
// if the VM is not running and we're not dealing with managed storage, just return success (nothing to do here)
@@ -197,19 +197,7 @@ public AttachAnswer attachVolume(AttachCommand cmd) {
197197
VDI vdi = null;
198198

199199
if (isManaged) {
200-
String iScsiName = details.get(DiskTO.IQN);
201-
String storageHost = details.get(DiskTO.STORAGE_HOST);
202-
String chapInitiatorUsername = disk.getDetails().get(DiskTO.CHAP_INITIATOR_USERNAME);
203-
String chapInitiatorSecret = disk.getDetails().get(DiskTO.CHAP_INITIATOR_SECRET);
204-
Long volumeSize = Long.parseLong(details.get(DiskTO.VOLUME_SIZE));
205-
206-
SR sr = this.hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, true);
207-
208-
vdi = hypervisorResource.getVDIbyUuid(conn, data.getPath(), false);
209-
210-
if (vdi == null) {
211-
vdi = hypervisorResource.createVdi(sr, vdiNameLabel, volumeSize);
212-
}
200+
vdi = hypervisorResource.prepareManagedStorage(conn, details, data.getPath(), vdiNameLabel);
213201

214202
if (vmNotRunning) {
215203
DiskTO newDisk = new DiskTO(disk.getData(), disk.getDiskSeq(), vdi.getUuid(conn), disk.getType());

0 commit comments

Comments
 (0)