Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.apache.cloudstack.storage.command;

import java.util.Map;

import com.cloud.agent.api.to.DiskTO;

public class DettachCommand extends StorageSubSystemCommand {
Expand All @@ -28,13 +30,21 @@ public class DettachCommand extends StorageSubSystemCommand {
private String _iScsiName;
private String _storageHost;
private int _storagePort;
private Map<String, String> params;

public DettachCommand(final DiskTO disk, final String vmName) {
super();
this.disk = disk;
this.vmName = vmName;
}

public DettachCommand(final DiskTO disk, final String vmName, Map<String, String> params) {
super();
this.disk = disk;
this.vmName = vmName;
this.params = params;
}

@Override
public boolean executeInSequence() {
return false;
Expand Down Expand Up @@ -88,6 +98,14 @@ public int getStoragePort() {
return _storagePort;
}

public Map<String, String> getParams() {
return params;
}

public void setParams(Map<String, String> params) {
this.params = params;
}

@Override
public void setExecuteInSequence(final boolean inSeq) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2405,7 +2405,7 @@ So if getMinSpeed() returns null we fall back to getSpeed().
DiskDef.DiskBus busT = getDiskModelFromVMDetail(vmTO);

if (busT == null) {
busT = getGuestDiskModel(vmTO.getPlatformEmulator());
busT = getGuestDiskModel(vmTO.getPlatformEmulator(), isUefiEnabled);
}

// If we're using virtio scsi, then we need to add a virtual scsi controller
Expand Down Expand Up @@ -2490,7 +2490,8 @@ public int compare(final DiskTO arg0, final DiskTO arg1) {
}
});

if (MapUtils.isNotEmpty(details) && details.containsKey(GuestDef.BootType.UEFI.toString())) {
boolean isUefiEnabled = MapUtils.isNotEmpty(details) && details.containsKey(GuestDef.BootType.UEFI.toString());
if (isUefiEnabled) {
isSecureBoot = isSecureMode(details.get(GuestDef.BootType.UEFI.toString()));
}
if (vmSpec.getOs().toLowerCase().contains("window")) {
Expand Down Expand Up @@ -2554,7 +2555,7 @@ public int compare(final DiskTO arg0, final DiskTO arg1) {
// if params contains a rootDiskController key, use its value (this is what other HVs are doing)
DiskDef.DiskBus diskBusType = getDiskModelFromVMDetail(vmSpec);
if (diskBusType == null) {
diskBusType = getGuestDiskModel(vmSpec.getPlatformEmulator());
diskBusType = getGuestDiskModel(vmSpec.getPlatformEmulator(), isUefiEnabled);
}

// I'm not sure why previously certain DATADISKs were hard-coded VIRTIO and others not, however this
Expand All @@ -2564,16 +2565,9 @@ public int compare(final DiskTO arg0, final DiskTO arg1) {
final DiskDef disk = new DiskDef();
int devId = volume.getDiskSeq().intValue();
if (volume.getType() == Volume.Type.ISO) {
if (volPath == null) {
if (isSecureBoot) {
disk.defISODisk(null, devId,isSecureBoot,isWindowsTemplate);
} else {
/* Add iso as placeholder */
disk.defISODisk(null, devId);
}
} else {
disk.defISODisk(volPath, devId);
}

disk.defISODisk(volPath, devId, isUefiEnabled);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

third param here is UEFI secure mode or not, please check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sureshanaparti here we check if UEFI is enabled because cdrom disk controller should be SATA for q35 machines


if (_guestCpuArch != null && _guestCpuArch.equals("aarch64")) {
disk.setBusType(DiskDef.DiskBus.SCSI);
}
Expand All @@ -2598,14 +2592,14 @@ public int compare(final DiskTO arg0, final DiskTO arg1) {
disk.defNetworkBasedDisk(glusterVolume + path.replace(mountpoint, ""), pool.getSourceHost(), pool.getSourcePort(), null,
null, devId, diskBusType, DiskProtocol.GLUSTER, DiskDef.DiskFmtType.QCOW2);
} else if (pool.getType() == StoragePoolType.CLVM || physicalDisk.getFormat() == PhysicalDiskFormat.RAW) {
if (volume.getType() == Volume.Type.DATADISK) {
if (volume.getType() == Volume.Type.DATADISK && !(isWindowsTemplate && isUefiEnabled)) {
disk.defBlockBasedDisk(physicalDisk.getPath(), devId, diskBusTypeData);
}
else {
disk.defBlockBasedDisk(physicalDisk.getPath(), devId, diskBusType);
}
} else {
if (volume.getType() == Volume.Type.DATADISK) {
if (volume.getType() == Volume.Type.DATADISK && !(isWindowsTemplate && isUefiEnabled)) {
disk.defFileBasedDisk(physicalDisk.getPath(), devId, diskBusTypeData, DiskDef.DiskFmtType.QCOW2);
} else {
if (isSecureBoot) {
Expand Down Expand Up @@ -3410,7 +3404,7 @@ private String getHypervisorPath(final Connect conn) {
}

boolean isGuestPVEnabled(final String guestOSName) {
DiskDef.DiskBus db = getGuestDiskModel(guestOSName);
DiskDef.DiskBus db = getGuestDiskModel(guestOSName, false);
return db != DiskDef.DiskBus.IDE;
}

Expand Down Expand Up @@ -3445,7 +3439,7 @@ public DiskDef.DiskBus getDiskModelFromVMDetail(final VirtualMachineTO vmTO) {
return null;
}

private DiskDef.DiskBus getGuestDiskModel(final String platformEmulator) {
private DiskDef.DiskBus getGuestDiskModel(final String platformEmulator, boolean isUefiEnabled) {
if (_guestCpuArch != null && _guestCpuArch.equals("aarch64")) {
return DiskDef.DiskBus.SCSI;
}
Expand All @@ -3454,6 +3448,8 @@ private DiskDef.DiskBus getGuestDiskModel(final String platformEmulator) {
return DiskDef.DiskBus.IDE;
} else if (platformEmulator.startsWith("Other PV Virtio-SCSI")) {
return DiskDef.DiskBus.SCSI;
} else if (isUefiEnabled && platformEmulator.startsWith("Windows")) {
return DiskDef.DiskBus.SATA;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this only required in case of UEFI secure mode only?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is required for both secure/legacy UEFI boot type on Windows machines

} else if (platformEmulator.contains("Ubuntu") ||
platformEmulator.startsWith("Fedora") ||
platformEmulator.startsWith("CentOS") ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,16 +716,14 @@ private String getDevLabel(int devId, DiskBus bus, boolean forIso) {
} else if (bus == DiskBus.VIRTIO) {
return "vd" + getDevLabelSuffix(devId);
} else if (bus == DiskBus.SATA){
if (!forIso) {
return "sda";
}
return "sd" + getDevLabelSuffix(devId);
}
if (forIso) {
devId --;
} else if(devId >= 2) {
devId += 2;
}
return (DiskBus.SATA == bus) ? "sdb" : "hd" + getDevLabelSuffix(devId);
return "hd" + getDevLabelSuffix(devId);

}

Expand Down Expand Up @@ -784,6 +782,16 @@ public void defISODisk(String volPath) {
_bus = DiskBus.IDE;
}

public void defISODisk(String volPath, boolean isUefiEnabled) {
_diskType = DiskType.FILE;
_deviceType = DeviceType.CDROM;
_sourcePath = volPath;
_bus = isUefiEnabled ? DiskBus.SATA : DiskBus.IDE;
_diskLabel = getDevLabel(3, _bus, true);
_diskFmtType = DiskFmtType.RAW;
_diskCacheMode = DiskCacheMode.NONE;
}

public void defISODisk(String volPath, Integer devId) {
if (devId == null) {
defISODisk(volPath);
Expand All @@ -798,20 +806,15 @@ public void defISODisk(String volPath, Integer devId) {
}
}

public void defISODisk(String volPath, Integer devId,boolean isSecure, boolean isWindowOs) {
public void defISODisk(String volPath, Integer devId,boolean isSecure) {
if (!isSecure) {
defISODisk(volPath, devId);
} else {
_diskType = DiskType.FILE;
_deviceType = DeviceType.CDROM;
_sourcePath = volPath;
if (isWindowOs) {
_diskLabel = getDevLabel(devId, DiskBus.SATA, true);
_bus = DiskBus.SATA;
} else {
_diskLabel = getDevLabel(devId, DiskBus.SCSI, true);
_bus = DiskBus.SCSI;
}
_diskLabel = getDevLabel(devId, DiskBus.SATA, true);
_bus = DiskBus.SATA;
_diskFmtType = DiskFmtType.RAW;
_diskCacheMode = DiskCacheMode.NONE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
import org.apache.cloudstack.utils.qemu.QemuImgException;
import org.apache.cloudstack.utils.qemu.QemuImgFile;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.libvirt.Connect;
Expand Down Expand Up @@ -1055,9 +1056,10 @@ private void deleteSnapshotViaManageSnapshotScript(final String snapshotName, KV
}
}

protected synchronized String attachOrDetachISO(final Connect conn, final String vmName, String isoPath, final boolean isAttach) throws LibvirtException, URISyntaxException,
protected synchronized String attachOrDetachISO(final Connect conn, final String vmName, String isoPath, final boolean isAttach, Map<String, String> params) throws LibvirtException, URISyntaxException,
InternalErrorException {
String isoXml = null;
boolean isUefiEnabled = MapUtils.isNotEmpty(params) && params.containsKey("UEFI");
if (isoPath != null && isAttach) {
final int index = isoPath.lastIndexOf("/");
final String path = isoPath.substring(0, index);
Expand All @@ -1067,11 +1069,11 @@ protected synchronized String attachOrDetachISO(final Connect conn, final String
isoPath = isoVol.getPath();

final DiskDef iso = new DiskDef();
iso.defISODisk(isoPath);
iso.defISODisk(isoPath, isUefiEnabled);
isoXml = iso.toString();
} else {
final DiskDef iso = new DiskDef();
iso.defISODisk(null);
iso.defISODisk(null, isUefiEnabled);
isoXml = iso.toString();
}

Expand All @@ -1097,7 +1099,7 @@ public Answer attachIso(final AttachCommand cmd) {
try {
String dataStoreUrl = getDataStoreUrlFromStore(store);
final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName());
attachOrDetachISO(conn, cmd.getVmName(), dataStoreUrl + File.separator + isoTO.getPath(), true);
attachOrDetachISO(conn, cmd.getVmName(), dataStoreUrl + File.separator + isoTO.getPath(), true, cmd.getControllerInfo());
} catch (final LibvirtException e) {
return new Answer(cmd, false, e.toString());
} catch (final URISyntaxException e) {
Expand All @@ -1120,7 +1122,7 @@ public Answer dettachIso(final DettachCommand cmd) {
try {
String dataStoreUrl = getDataStoreUrlFromStore(store);
final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName());
attachOrDetachISO(conn, cmd.getVmName(), dataStoreUrl + File.separator + isoTO.getPath(), false);
attachOrDetachISO(conn, cmd.getVmName(), dataStoreUrl + File.separator + isoTO.getPath(), false, cmd.getParams());
} catch (final LibvirtException e) {
return new Answer(cmd, false, e.toString());
} catch (final URISyntaxException e) {
Expand Down
15 changes: 13 additions & 2 deletions server/src/main/java/com/cloud/template/TemplateManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
import com.cloud.agent.api.to.DataTO;
import com.cloud.agent.api.to.DiskTO;
import com.cloud.agent.api.to.NfsTO;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.api.ApiDBUtils;
import com.cloud.api.ApiResponseHelper;
import com.cloud.api.query.dao.UserVmJoinDao;
Expand All @@ -135,6 +136,8 @@
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.hypervisor.Hypervisor;
import com.cloud.hypervisor.HypervisorGuru;
import com.cloud.hypervisor.HypervisorGuruManager;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.projects.Project;
import com.cloud.projects.ProjectManager;
Expand Down Expand Up @@ -198,6 +201,7 @@
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.VirtualMachineProfile;
import com.cloud.vm.VirtualMachineProfileImpl;
import com.cloud.vm.VmDetailConstants;
import com.cloud.vm.dao.UserVmDao;
import com.cloud.vm.dao.VMInstanceDao;
Expand Down Expand Up @@ -282,6 +286,8 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
MessageBus _messageBus;
@Inject
private VMTemplateDetailsDao _tmpltDetailsDao;
@Inject
private HypervisorGuruManager _hvGuruMgr;

private boolean _disableExtraction = false;
private List<TemplateAdapter> _adapters;
Expand Down Expand Up @@ -1290,11 +1296,16 @@ private boolean attachISOToVM(long vmId, long isoId, boolean attach) {

DataTO isoTO = tmplt.getTO();
DiskTO disk = new DiskTO(isoTO, null, null, Volume.Type.ISO);

HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType());
VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
VirtualMachineTO vmTO = hvGuru.implement(profile);

Command cmd = null;
if (attach) {
cmd = new AttachCommand(disk, vmName);
cmd = new AttachCommand(disk, vmName, vmTO.getDetails());
} else {
cmd = new DettachCommand(disk, vmName);
cmd = new DettachCommand(disk, vmName, vmTO.getDetails());
}
Answer a = _agentMgr.easySend(vm.getHostId(), cmd);
return (a != null && a.getResult());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.cloud.host.Status;
import com.cloud.host.dao.HostDao;
import com.cloud.hypervisor.Hypervisor;
import com.cloud.hypervisor.HypervisorGuruManager;
import com.cloud.storage.Storage;
import com.cloud.storage.TemplateProfile;
import com.cloud.projects.ProjectManager;
Expand Down Expand Up @@ -180,6 +181,8 @@ public class TemplateManagerImplTest {
@Inject
private VMTemplateDao _tmpltDao;

@Inject
HypervisorGuruManager _hvGuruMgr;

public class CustomThreadPoolExecutor extends ThreadPoolExecutor {
AtomicInteger ai = new AtomicInteger(0);
Expand Down Expand Up @@ -697,6 +700,11 @@ public VMTemplateDetailsDao vmTemplateDetailsDao() {
return Mockito.mock(VMTemplateDetailsDao.class);
}

@Bean
public HypervisorGuruManager hypervisorGuruManager() {
return Mockito.mock(HypervisorGuruManager.class);
}

public static class Library implements TypeFilter {
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
Expand Down
Loading