xenserver: attach regular iso with configdrive (#9216)

* xenserver: attach regular iso with configdrive

Fixes #7902

This PR allows attaching a regular ISO to a VM when it already has the
config drive ISO attached.
Config-drive ISO is now attached with the SR name-label
<VM-NAME>-CONFIGDRIVE-ISO. While regular ISOs continue to attach with SR
name-label <VM-NAME>-ISO. VM which already have the configdrive ISO
attached before this fix will return an appropriate error and will need
to be stopped-start.
This commit is contained in:
Abhishek Kumar 2024-06-27 16:10:33 +05:30 committed by GitHub
parent 22cd00ffb1
commit 53faf0f66a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 6 deletions

View File

@ -62,6 +62,7 @@ import org.apache.cloudstack.diagnostics.DiagnosticsService;
import org.apache.cloudstack.hypervisor.xenserver.ExtraConfigurationUtility; import org.apache.cloudstack.hypervisor.xenserver.ExtraConfigurationUtility;
import org.apache.cloudstack.storage.command.browser.ListDataStoreObjectsAnswer; import org.apache.cloudstack.storage.command.browser.ListDataStoreObjectsAnswer;
import org.apache.cloudstack.storage.command.browser.ListDataStoreObjectsCommand; import org.apache.cloudstack.storage.command.browser.ListDataStoreObjectsCommand;
import org.apache.cloudstack.storage.configdrive.ConfigDrive;
import org.apache.cloudstack.storage.to.TemplateObjectTO; import org.apache.cloudstack.storage.to.TemplateObjectTO;
import org.apache.cloudstack.storage.to.VolumeObjectTO; import org.apache.cloudstack.storage.to.VolumeObjectTO;
import org.apache.cloudstack.utils.security.ParserUtils; import org.apache.cloudstack.utils.security.ParserUtils;
@ -222,6 +223,8 @@ public abstract class CitrixResourceBase extends ServerResourceBase implements S
private final static String VM_NAME_ISO_SUFFIX = "-ISO"; private final static String VM_NAME_ISO_SUFFIX = "-ISO";
private final static String VM_NAME_CONFIGDRIVE_ISO_SUFFIX = "-CONFIGDRIVE-ISO";
private final static String VM_FILE_ISO_SUFFIX = ".iso"; private final static String VM_FILE_ISO_SUFFIX = ".iso";
public final static int DEFAULTDOMRSSHPORT = 3922; public final static int DEFAULTDOMRSSHPORT = 3922;
@ -1020,12 +1023,13 @@ public abstract class CitrixResourceBase extends ServerResourceBase implements S
protected SR createIsoSRbyURI(final Connection conn, final URI uri, final String vmName, final boolean shared) { protected SR createIsoSRbyURI(final Connection conn, final URI uri, final String vmName, final boolean shared) {
try { try {
final Map<String, String> deviceConfig = new HashMap<String, String>(); final Map<String, String> deviceConfig = new HashMap<String, String>();
final boolean isConfigDrive = uri.toString().endsWith(ConfigDrive.CONFIGDRIVEDIR);
String path = uri.getPath(); String path = uri.getPath();
path = path.replace("//", "/"); path = path.replace("//", "/");
deviceConfig.put("location", uri.getHost() + ":" + path); deviceConfig.put("location", uri.getHost() + ":" + path);
final Host host = Host.getByUuid(conn, _host.getUuid()); final Host host = Host.getByUuid(conn, _host.getUuid());
final SR sr = SR.create(conn, host, deviceConfig, new Long(0), uri.getHost() + path, "iso", "iso", "iso", shared, new HashMap<String, String>()); final SR sr = SR.create(conn, host, deviceConfig, new Long(0), uri.getHost() + path, "iso", "iso", "iso", shared, new HashMap<String, String>());
sr.setNameLabel(conn, vmName + "-ISO"); sr.setNameLabel(conn, vmName + (isConfigDrive ? VM_NAME_CONFIGDRIVE_ISO_SUFFIX: VM_NAME_ISO_SUFFIX));
sr.setNameDescription(conn, deviceConfig.get("location")); sr.setNameDescription(conn, deviceConfig.get("location"));
sr.scan(conn); sr.scan(conn);
@ -2648,9 +2652,10 @@ public abstract class CitrixResourceBase extends ServerResourceBase implements S
return scsiid; return scsiid;
} }
public SR getISOSRbyVmName(final Connection conn, final String vmName) { public SR getISOSRbyVmName(final Connection conn, final String vmName, boolean isConfigDrive) {
try { try {
final Set<SR> srs = SR.getByNameLabel(conn, vmName + "-ISO"); final Set<SR> srs = SR.getByNameLabel(conn, vmName +
(isConfigDrive ? VM_NAME_CONFIGDRIVE_ISO_SUFFIX : VM_NAME_ISO_SUFFIX));
if (srs.size() == 0) { if (srs.size() == 0) {
return null; return null;
} else if (srs.size() == 1) { } else if (srs.size() == 1) {
@ -2697,9 +2702,20 @@ public abstract class CitrixResourceBase extends ServerResourceBase implements S
} catch (final URISyntaxException e) { } catch (final URISyntaxException e) {
throw new CloudRuntimeException("isoURL is wrong: " + isoURL); throw new CloudRuntimeException("isoURL is wrong: " + isoURL);
} }
isoSR = getISOSRbyVmName(conn, vmName); isoSR = getISOSRbyVmName(conn, vmName, false);
if (isoSR == null) { if (isoSR == null) {
isoSR = createIsoSRbyURI(conn, uri, vmName, false); isoSR = createIsoSRbyURI(conn, uri, vmName, false);
} else {
try {
String description = isoSR.getNameDescription(conn);
if (description.endsWith(ConfigDrive.CONFIGDRIVEDIR)) {
throw new CloudRuntimeException(String.format("VM %s already has %s ISO attached. Please " +
"stop-start VM to allow attaching-detaching both ISOs", vmName, ConfigDrive.CONFIGDRIVEDIR));
}
} catch (XenAPIException | XmlRpcException e) {
throw new CloudRuntimeException(String.format("Unable to retrieve name description for the already " +
"attached ISO on VM %s", vmName));
}
} }
final String isoName = isoURL.substring(index + 1); final String isoName = isoURL.substring(index + 1);
@ -5667,7 +5683,7 @@ public abstract class CitrixResourceBase extends ServerResourceBase implements S
s_logger.debug("Attaching config drive iso device for the VM " + vmName + " In host " + ipAddr); s_logger.debug("Attaching config drive iso device for the VM " + vmName + " In host " + ipAddr);
Set<VM> vms = VM.getByNameLabel(conn, vmName); Set<VM> vms = VM.getByNameLabel(conn, vmName);
SR sr = getSRByNameLabel(conn, vmName + VM_NAME_ISO_SUFFIX); SR sr = getSRByNameLabel(conn, vmName + VM_NAME_CONFIGDRIVE_ISO_SUFFIX);
//Here you will find only two vdis with the <vmname>.iso. //Here you will find only two vdis with the <vmname>.iso.
//one is from source host and second from dest host //one is from source host and second from dest host
Set<VDI> vdis = VDI.getByNameLabel(conn, vmName + VM_FILE_ISO_SUFFIX); Set<VDI> vdis = VDI.getByNameLabel(conn, vmName + VM_FILE_ISO_SUFFIX);

View File

@ -144,8 +144,10 @@ public final class CitrixStopCommandWrapper extends CommandWrapper<StopCommand,
networks.add(vif.getNetwork(conn)); networks.add(vif.getNetwork(conn));
} }
vm.destroy(conn); vm.destroy(conn);
final SR sr = citrixResourceBase.getISOSRbyVmName(conn, command.getVmName()); final SR sr = citrixResourceBase.getISOSRbyVmName(conn, command.getVmName(), false);
citrixResourceBase.removeSR(conn, sr); citrixResourceBase.removeSR(conn, sr);
final SR configDriveSR = citrixResourceBase.getISOSRbyVmName(conn, command.getVmName(), true);
citrixResourceBase.removeSR(conn, configDriveSR);
// Disable any VLAN networks that aren't used // Disable any VLAN networks that aren't used
// anymore // anymore
for (final Network network : networks) { for (final Network network : networks) {