Search in sources :

Example 41 with VBD

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

the class CitrixAttachOrDettachConfigDriveCommandWrapper method execute.

@Override
public Answer execute(final AttachOrDettachConfigDriveCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    String vmName = command.getVmName();
    List<String[]> vmData = command.getVmData();
    String label = command.getConfigDriveLabel();
    Boolean isAttach = command.isAttach();
    try {
        Set<VM> vms = VM.getByNameLabel(conn, vmName);
        for (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
                Set<VDI> vdis = VDI.getByNameLabel(conn, vmName + ".iso");
                if (vdis != null && !vdis.isEmpty()) {
                    s_logger.debug("Deleting config drive for the VM " + vmName);
                    VDI vdi = vdis.iterator().next();
                    // Find the VM's CD-ROM VBD
                    Set<VBD> vbds = vdi.getVBDs(conn);
                    for (VBD vbd : vbds) {
                        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 (Types.XenAPIException ex) {
        s_logger.debug("Failed to attach config drive iso to VM " + vmName + " " + ex.getMessage());
    } catch (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 42 with VBD

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

the class CitrixDestroyCommandWrapper method execute.

@Override
public Answer execute(final DestroyCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final VolumeTO vol = command.getVolume();
    // Look up the VDI
    final String volumeUUID = vol.getPath();
    VDI vdi = null;
    try {
        vdi = citrixResourceBase.getVDIbyUuid(conn, volumeUUID);
    } catch (final Exception e) {
        return new Answer(command, true, "Success");
    }
    Set<VBD> vbds = null;
    try {
        vbds = vdi.getVBDs(conn);
    } catch (final Exception e) {
        final String msg = "VDI getVBDS for " + volumeUUID + " failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new Answer(command, false, msg);
    }
    for (final VBD vbd : vbds) {
        try {
            vbd.unplug(conn);
            vbd.destroy(conn);
        } catch (final Exception e) {
            final String msg = "VM destroy for " + volumeUUID + "  failed due to " + e.toString();
            s_logger.warn(msg, e);
            return new Answer(command, false, msg);
        }
    }
    try {
        final Set<VDI> snapshots = vdi.getSnapshots(conn);
        for (final VDI snapshot : snapshots) {
            snapshot.destroy(conn);
        }
        vdi.destroy(conn);
    } catch (final Exception e) {
        final String msg = "VDI destroy for " + volumeUUID + " failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new Answer(command, false, msg);
    }
    return new Answer(command, true, "Success");
}
Also used : Answer(com.cloud.agent.api.Answer) VolumeTO(com.cloud.agent.api.to.VolumeTO) Connection(com.xensource.xenapi.Connection) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI)

Example 43 with VBD

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

the class CitrixDeleteVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final DeleteVMSnapshotCommand command, final CitrixResourceBase citrixResourceBase) {
    final String snapshotName = command.getTarget().getSnapshotName();
    final Connection conn = citrixResourceBase.getConnection();
    try {
        final List<VDI> vdiList = new ArrayList<VDI>();
        final Set<VM> snapshots = VM.getByNameLabel(conn, snapshotName);
        if (snapshots == null || snapshots.size() == 0) {
            s_logger.warn("VM snapshot with name " + snapshotName + " does not exist, assume it is already deleted");
            return new DeleteVMSnapshotAnswer(command, command.getVolumeTOs());
        }
        final VM snapshot = snapshots.iterator().next();
        final Set<VBD> vbds = snapshot.getVBDs(conn);
        for (final VBD vbd : vbds) {
            if (vbd.getType(conn) == Types.VbdType.DISK) {
                final VDI vdi = vbd.getVDI(conn);
                vdiList.add(vdi);
            }
        }
        if (command.getTarget().getType() == VMSnapshot.Type.DiskAndMemory) {
            vdiList.add(snapshot.getSuspendVDI(conn));
        }
        snapshot.destroy(conn);
        for (final VDI vdi : vdiList) {
            vdi.destroy(conn);
        }
        try {
            Thread.sleep(5000);
        } catch (final InterruptedException ex) {
        }
        // re-calculate used capacify for this VM snapshot
        for (final VolumeObjectTO volumeTo : command.getVolumeTOs()) {
            try {
                final long size = citrixResourceBase.getVMSnapshotChainSize(conn, volumeTo, command.getVmName(), snapshotName);
                volumeTo.setSize(size);
            } catch (final CloudRuntimeException cre) {
            }
        }
        return new DeleteVMSnapshotAnswer(command, command.getVolumeTOs());
    } catch (final Exception e) {
        s_logger.warn("Catch Exception: " + e.getClass().toString() + " due to " + e.toString(), e);
        return new DeleteVMSnapshotAnswer(command, false, e.getMessage());
    }
}
Also used : Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) DeleteVMSnapshotAnswer(com.cloud.agent.api.DeleteVMSnapshotAnswer)

Example 44 with VBD

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

the class CitrixResourceBase method prepareISO.

public void prepareISO(final Connection conn, final String vmName, List<String[]> vmDataList, 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();
    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)

Example 45 with VBD

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

the class CitrixResourceBase method attachConfigDriveToMigratedVm.

public boolean attachConfigDriveToMigratedVm(Connection conn, String vmName, String ipAddr) {
    try {
        s_logger.debug("Attaching config drive iso device for the VM " + vmName + " In host " + ipAddr);
        Set<VM> vms = VM.getByNameLabel(conn, vmName);
        SR sr = getSRByNameLabel(conn, vmName + VM_NAME_ISO_SUFFIX);
        // Here you will find only two vdis with the <vmname>.iso.
        // one is from source host and second from dest host
        Set<VDI> vdis = VDI.getByNameLabel(conn, vmName + VM_FILE_ISO_SUFFIX);
        if (vdis.isEmpty()) {
            s_logger.debug("Could not find config drive ISO: " + vmName);
            return false;
        }
        VDI configdriveVdi = null;
        for (VDI vdi : vdis) {
            SR vdiSr = vdi.getSR(conn);
            if (vdiSr.getUuid(conn).equals(sr.getUuid(conn))) {
                // get this vdi to attach to vbd
                configdriveVdi = vdi;
                s_logger.debug("VDI for the config drive ISO  " + vdi);
            } else {
                // delete the vdi in source host so that the <vmname>.iso file is get removed
                s_logger.debug("Removing the source host VDI for the config drive ISO  " + vdi);
                vdi.destroy(conn);
            }
        }
        if (configdriveVdi == null) {
            s_logger.debug("Config drive ISO VDI is not found ");
            return false;
        }
        for (VM vm : vms) {
            // create vbd
            VBD.Record cfgDriveVbdr = new VBD.Record();
            cfgDriveVbdr.VM = vm;
            cfgDriveVbdr.empty = true;
            cfgDriveVbdr.bootable = false;
            cfgDriveVbdr.userdevice = "autodetect";
            cfgDriveVbdr.mode = Types.VbdMode.RO;
            cfgDriveVbdr.type = Types.VbdType.CD;
            VBD cfgDriveVBD = VBD.create(conn, cfgDriveVbdr);
            s_logger.debug("Inserting vbd " + configdriveVdi);
            cfgDriveVBD.insert(conn, configdriveVdi);
            break;
        }
        return true;
    } catch (BadServerResponse e) {
        s_logger.warn("Failed to attach config drive ISO to the VM  " + vmName + " In host " + ipAddr + " due to a bad server response.", e);
        return false;
    } catch (XenAPIException e) {
        s_logger.warn("Failed to attach config drive ISO to the VM  " + vmName + " In host " + ipAddr + " due to a xapi problem.", e);
        return false;
    } catch (XmlRpcException e) {
        s_logger.warn("Failed to attach config drive ISO to the VM  " + vmName + " In host " + ipAddr + " due to a problem in a remote call.", e);
        return false;
    }
}
Also used : BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VDI(com.xensource.xenapi.VDI) XmlRpcException(org.apache.xmlrpc.XmlRpcException) 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