Search in sources :

Example 26 with VBD

use of com.xensource.xenapi.VBD in project cosmic by MissionCriticalCloud.

the class CitrixAttachOrDettachConfigDriveCommandWrapper method execute.

@Override
public Answer execute(final AttachOrDettachConfigDriveCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final String vmName = command.getVmName();
    final List<String[]> vmData = command.getVmData();
    final String label = command.getConfigDriveLabel();
    final Boolean isAttach = command.isAttach();
    try {
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        for (final VM vm : vms) {
            if (isAttach) {
                if (!citrixResourceBase.createAndAttachConfigDriveIsoForVM(conn, vm, vmData, label)) {
                    s_logger.debug("Failed to attach config drive iso to VM " + vmName);
                }
            } else {
                // delete the config drive iso attached to VM
                final Set<VDI> vdis = VDI.getByNameLabel(conn, vmName + ".iso");
                if (vdis != null && !vdis.isEmpty()) {
                    s_logger.debug("Deleting config drive for the VM " + vmName);
                    final VDI vdi = vdis.iterator().next();
                    // Find the VM's CD-ROM VBD
                    final Set<VBD> vbds = vdi.getVBDs(conn);
                    for (final VBD vbd : vbds) {
                        final VBD.Record vbdRec = vbd.getRecord(conn);
                        if (vbdRec.type.equals(Types.VbdType.CD) && !vbdRec.empty && !vbdRec.userdevice.equals(citrixResourceBase._attachIsoDeviceNum)) {
                            if (vbdRec.currentlyAttached) {
                                vbd.eject(conn);
                            }
                            vbd.destroy(conn);
                        }
                    }
                    vdi.destroy(conn);
                }
                s_logger.debug("Successfully dettached config drive iso from the VM " + vmName);
            }
        }
    } catch (final Types.XenAPIException ex) {
        s_logger.debug("Failed to attach config drive iso to VM " + vmName + " " + ex.getMessage());
    } catch (final XmlRpcException ex) {
        s_logger.debug("Failed to attach config drive iso to VM " + vmName + " " + ex.getMessage());
    }
    return new Answer(command, true, "success");
}
Also used : Types(com.xensource.xenapi.Types) Connection(com.xensource.xenapi.Connection) Answer(com.cloud.agent.api.Answer) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 27 with VBD

use of com.xensource.xenapi.VBD in project cosmic by MissionCriticalCloud.

the class XenServer625ResourceTest method testisDeviceUsedTrue.

@Test
public void testisDeviceUsedTrue() throws Types.XenAPIException, XmlRpcException {
    final Connection conn = mock(Connection.class);
    final VM vm = mock(VM.class);
    final VBD vbd = mock(VBD.class);
    final Set<String> allowedVBDDevices = new HashSet<>();
    final Set<VBD> vbds = new HashSet<>();
    vbds.add(vbd);
    final Long deviceId = 4L;
    when(vm.getAllowedVBDDevices(conn)).thenReturn(allowedVBDDevices);
    assertTrue(xenServer625Resource.isDeviceUsed(conn, vm, deviceId));
    verify(vm, times(1)).getAllowedVBDDevices(conn);
}
Also used : VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) VBD(com.xensource.xenapi.VBD) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 28 with VBD

use of com.xensource.xenapi.VBD in project cosmic by MissionCriticalCloud.

the class XenServer625ResourceTest method testisDeviceUsedFalse.

@Test
public void testisDeviceUsedFalse() throws Types.XenAPIException, XmlRpcException {
    final Connection conn = mock(Connection.class);
    final VM vm = mock(VM.class);
    final VBD vbd = mock(VBD.class);
    final Set<String> allowedVBDDevices = new HashSet<>();
    allowedVBDDevices.add("4");
    final Set<VBD> vbds = new HashSet<>();
    vbds.add(vbd);
    final Long deviceId = 4L;
    when(vm.getAllowedVBDDevices(conn)).thenReturn(allowedVBDDevices);
    assertFalse(xenServer625Resource.isDeviceUsed(conn, vm, deviceId));
    verify(vm, times(1)).getAllowedVBDDevices(conn);
}
Also used : VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) VBD(com.xensource.xenapi.VBD) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 29 with VBD

use of com.xensource.xenapi.VBD in project cloudstack by apache.

the class CitrixResourceBase method createVbd.

public VBD createVbd(final Connection conn, final DiskTO volume, final String vmName, final VM vm, final BootloaderType bootLoaderType, VDI vdi) throws XmlRpcException, XenAPIException {
    final Volume.Type type = volume.getType();
    if (vdi == null) {
        vdi = mount(conn, vmName, volume);
    }
    if (vdi != null) {
        if ("detached".equals(vdi.getNameLabel(conn))) {
            vdi.setNameLabel(conn, vmName + "-DATA");
        }
        final Map<String, String> smConfig = vdi.getSmConfig(conn);
        for (final String key : smConfig.keySet()) {
            if (key.startsWith("host_")) {
                vdi.removeFromSmConfig(conn, key);
                break;
            }
        }
    }
    final VBD.Record vbdr = new VBD.Record();
    vbdr.VM = vm;
    if (vdi != null) {
        vbdr.VDI = vdi;
    } else {
        vbdr.empty = true;
    }
    if (type == Volume.Type.ROOT && bootLoaderType == BootloaderType.PyGrub) {
        vbdr.bootable = true;
    } else if (type == Volume.Type.ISO && bootLoaderType == BootloaderType.CD) {
        vbdr.bootable = true;
    }
    if (volume.getType() == Volume.Type.ISO) {
        vbdr.mode = Types.VbdMode.RO;
        vbdr.type = Types.VbdType.CD;
        vbdr.userdevice = "3";
    } else {
        vbdr.mode = Types.VbdMode.RW;
        vbdr.type = Types.VbdType.DISK;
        vbdr.unpluggable = (volume.getType() == Volume.Type.ROOT) ? false : true;
        vbdr.userdevice = "autodetect";
        final Long deviceId = volume.getDiskSeq();
        if (deviceId != null && (!isDeviceUsed(conn, vm, deviceId) || deviceId > 3)) {
            vbdr.userdevice = deviceId.toString();
        }
    }
    final VBD vbd = VBD.create(conn, vbdr);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("VBD " + vbd.getUuid(conn) + " created for " + volume);
    }
    return vbd;
}
Also used : Volume(com.cloud.storage.Volume) VBD(com.xensource.xenapi.VBD)

Example 30 with VBD

use of com.xensource.xenapi.VBD in project cosmic by MissionCriticalCloud.

the class CitrixResourceBase method prepareISO.

public void prepareISO(final Connection conn, final String vmName, final List<String[]> vmDataList, final String configDriveLabel) throws XmlRpcException, XenAPIException {
    final Set<VM> vms = VM.getByNameLabel(conn, vmName);
    if (vms == null || vms.size() != 1) {
        throw new CloudRuntimeException("There are " + (vms == null ? "0" : vms.size()) + " VMs named " + vmName);
    }
    final VM vm = vms.iterator().next();
    if (vmDataList != null) {
        // create SR
        final SR sr = createLocalIsoSR(conn, _configDriveSRName + getHost().getIp());
        // 1. create vm data files
        createVmdataFiles(vmName, vmDataList, configDriveLabel);
        // 2. copy config drive iso to host
        copyConfigDriveIsoToHost(conn, sr, vmName);
    }
    final Set<VBD> vbds = vm.getVBDs(conn);
    for (final VBD vbd : vbds) {
        final VBD.Record vbdr = vbd.getRecord(conn);
        if (vbdr.type == Types.VbdType.CD && vbdr.empty == false && vbdr.userdevice.equals(_attachIsoDeviceNum)) {
            final VDI vdi = vbdr.VDI;
            final SR sr = vdi.getSR(conn);
            final Set<PBD> pbds = sr.getPBDs(conn);
            if (pbds == null) {
                throw new CloudRuntimeException("There is no pbd for sr " + sr);
            }
            for (final PBD pbd : pbds) {
                final PBD.Record pbdr = pbd.getRecord(conn);
                if (pbdr.host.getUuid(conn).equals(_host.getUuid())) {
                    return;
                }
            }
            sr.setShared(conn, true);
            final Host host = Host.getByUuid(conn, _host.getUuid());
            final PBD.Record pbdr = pbds.iterator().next().getRecord(conn);
            pbdr.host = host;
            pbdr.uuid = "";
            final PBD pbd = PBD.create(conn, pbdr);
            pbdPlug(conn, pbd, pbd.getUuid(conn));
            break;
        }
    }
}
Also used : PBD(com.xensource.xenapi.PBD) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) Host(com.xensource.xenapi.Host) SR(com.xensource.xenapi.SR)

Aggregations

VBD (com.xensource.xenapi.VBD)45 VDI (com.xensource.xenapi.VDI)34 VM (com.xensource.xenapi.VM)33 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)26 Connection (com.xensource.xenapi.Connection)26 XenAPIException (com.xensource.xenapi.Types.XenAPIException)25 XmlRpcException (org.apache.xmlrpc.XmlRpcException)25 SR (com.xensource.xenapi.SR)14 InternalErrorException (com.cloud.exception.InternalErrorException)13 DiskTO (com.cloud.agent.api.to.DiskTO)10 Types (com.xensource.xenapi.Types)10 DataTO (com.cloud.agent.api.to.DataTO)8 IOException (java.io.IOException)8 MalformedURLException (java.net.MalformedURLException)8 URISyntaxException (java.net.URISyntaxException)8 TimeoutException (java.util.concurrent.TimeoutException)8 ConfigurationException (javax.naming.ConfigurationException)8 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)8 SAXException (org.xml.sax.SAXException)8 Answer (com.cloud.agent.api.Answer)6