Search in sources :

Example 1 with VBD

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

the class CitrixResourceBase method destroyPatchVbd.

public void destroyPatchVbd(final Connection conn, final String vmName) throws XmlRpcException, XenAPIException {
    try {
        if (!vmName.startsWith("r-") && !vmName.startsWith("s-") && !vmName.startsWith("v-")) {
            return;
        }
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        for (final VM vm : vms) {
            final Set<VBD> vbds = vm.getVBDs(conn);
            for (final VBD vbd : vbds) {
                if (vbd.getType(conn) == Types.VbdType.CD) {
                    vbd.eject(conn);
                    vbd.destroy(conn);
                    break;
                }
            }
        }
    } catch (final Exception e) {
        s_logger.debug("Cannot destory CD-ROM device for VM " + vmName + " due to " + e.toString(), e);
    }
}
Also used : VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with VBD

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

the class XenServerStorageProcessor method dettachVolume.

@Override
public Answer dettachVolume(final DettachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final DataTO data = disk.getData();
    try {
        final Connection conn = hypervisorResource.getConnection();
        final String vmName = cmd.getVmName();
        VM vm = null;
        boolean vmNotRunning = true;
        try {
            vm = hypervisorResource.getVM(conn, vmName);
            final VM.Record vmr = vm.getRecord(conn);
            vmNotRunning = vmr.powerState != VmPowerState.RUNNING;
        } catch (final CloudRuntimeException ex) {
        }
        // this should probably never actually happen
        if (vmNotRunning && !cmd.isManaged()) {
            return new DettachAnswer(disk);
        }
        if (!vmNotRunning) {
            final VDI vdi = hypervisorResource.mount(conn, null, null, data.getPath());
            // Look up all VBDs for this VDI
            final Set<VBD> vbds = vdi.getVBDs(conn);
            // Detach each VBD from its VM, and then destroy it
            for (final VBD vbd : vbds) {
                final VBD.Record vbdr = vbd.getRecord(conn);
                if (vbdr.currentlyAttached) {
                    vbd.unplug(conn);
                }
                vbd.destroy(conn);
            }
            hypervisorResource.umount(conn, vdi);
        }
        if (cmd.isManaged()) {
            hypervisorResource.handleSrAndVdiDetach(cmd.get_iScsiName(), conn);
        }
        return new DettachAnswer(disk);
    } catch (final Exception e) {
        s_logger.warn("Failed dettach volume: " + data.getPath());
        return new DettachAnswer("Failed dettach volume: " + data.getPath() + ", due to " + e.toString());
    }
}
Also used : DettachAnswer(org.apache.cloudstack.storage.command.DettachAnswer) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataTO(com.cloud.agent.api.to.DataTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 3 with VBD

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

the class XenServerStorageProcessor method attachVolume.

@Override
public AttachAnswer attachVolume(final AttachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final DataTO data = disk.getData();
    try {
        final String vmName = cmd.getVmName();
        final String vdiNameLabel = vmName + "-DATA";
        final Connection conn = hypervisorResource.getConnection();
        VM vm = null;
        boolean vmNotRunning = true;
        try {
            vm = hypervisorResource.getVM(conn, vmName);
            final VM.Record vmr = vm.getRecord(conn);
            vmNotRunning = vmr.powerState != VmPowerState.RUNNING;
        } catch (final CloudRuntimeException ex) {
        }
        final Map<String, String> details = disk.getDetails();
        final boolean isManaged = Boolean.parseBoolean(details.get(DiskTO.MANAGED));
        // this should probably never actually happen
        if (vmNotRunning && !isManaged) {
            return new AttachAnswer(disk);
        }
        VDI vdi;
        if (isManaged) {
            vdi = hypervisorResource.prepareManagedStorage(conn, details, data.getPath(), vdiNameLabel);
            if (vmNotRunning) {
                final DiskTO newDisk = new DiskTO(disk.getData(), disk.getDiskSeq(), vdi.getUuid(conn), disk.getType());
                return new AttachAnswer(newDisk);
            }
        } else {
            vdi = hypervisorResource.mount(conn, null, null, data.getPath());
        }
        hypervisorResource.destroyUnattachedVBD(conn, vm);
        final VBD.Record vbdr = new VBD.Record();
        vbdr.VM = vm;
        vbdr.VDI = vdi;
        vbdr.bootable = false;
        vbdr.userdevice = "autodetect";
        final Long deviceId = disk.getDiskSeq();
        if (deviceId != null && !hypervisorResource.isDeviceUsed(conn, vm, deviceId)) {
            vbdr.userdevice = deviceId.toString();
        }
        vbdr.mode = Types.VbdMode.RW;
        vbdr.type = Types.VbdType.DISK;
        vbdr.unpluggable = true;
        final VBD vbd = VBD.create(conn, vbdr);
        // Attach the VBD to the VM
        try {
            vbd.plug(conn);
        } catch (final Exception e) {
            vbd.destroy(conn);
            throw e;
        }
        // Update the VDI's label to include the VM name
        vdi.setNameLabel(conn, vdiNameLabel);
        final DiskTO newDisk = new DiskTO(disk.getData(), Long.parseLong(vbd.getUserdevice(conn)), vdi.getUuid(conn), disk.getType());
        return new AttachAnswer(newDisk);
    } catch (final Exception e) {
        final String msg = "Failed to attach volume for uuid: " + data.getPath() + " due to " + e.toString();
        s_logger.warn(msg, e);
        return new AttachAnswer(msg);
    }
}
Also used : Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataTO(com.cloud.agent.api.to.DataTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Example 4 with VBD

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

the class XenServerStorageProcessor method dettachIso.

@Override
public Answer dettachIso(final DettachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final DataTO data = disk.getData();
    final DataStoreTO store = data.getDataStore();
    String isoURL = null;
    if (store == null) {
        final TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
        isoURL = iso.getName();
    } else {
        if (!(store instanceof NfsTO)) {
            s_logger.debug("Can't detach a iso which is not created on nfs: ");
            return new AttachAnswer("Can't detach a iso which is not created on nfs: ");
        }
        final NfsTO nfsStore = (NfsTO) store;
        isoURL = nfsStore.getUrl() + nfsStore.getPathSeparator() + data.getPath();
    }
    try {
        final Connection conn = hypervisorResource.getConnection();
        // Find the VM
        final VM vm = hypervisorResource.getVM(conn, cmd.getVmName());
        final String vmUUID = vm.getUuid(conn);
        // Find the ISO VDI
        final VDI isoVDI = hypervisorResource.getIsoVDIByURL(conn, cmd.getVmName(), isoURL);
        final SR sr = isoVDI.getSR(conn);
        // Look up all VBDs for this VDI
        final Set<VBD> vbds = isoVDI.getVBDs(conn);
        // the ISO from it
        for (final VBD vbd : vbds) {
            final VM vbdVM = vbd.getVM(conn);
            final String vbdVmUUID = vbdVM.getUuid(conn);
            if (vbdVmUUID.equals(vmUUID)) {
                // If an ISO is already inserted, eject it
                if (!vbd.getEmpty(conn)) {
                    vbd.eject(conn);
                }
                break;
            }
        }
        if (!XenServerUtilitiesHelper.isXenServerToolsSR(sr.getNameLabel(conn))) {
            hypervisorResource.removeSR(conn, sr);
        }
        return new DettachAnswer(disk);
    } catch (final XenAPIException e) {
        final String msg = "Failed to detach volume" + " for uuid: " + data.getPath() + "  due to " + e.toString();
        s_logger.warn(msg, e);
        return new DettachAnswer(msg);
    } catch (final Exception e) {
        final String msg = "Failed to detach volume" + " for uuid: " + data.getPath() + "  due to " + e.getMessage();
        s_logger.warn(msg, e);
        return new DettachAnswer(msg);
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DettachAnswer(org.apache.cloudstack.storage.command.DettachAnswer) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) NfsTO(com.cloud.agent.api.to.NfsTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataTO(com.cloud.agent.api.to.DataTO) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) SR(com.xensource.xenapi.SR)

Example 5 with VBD

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

the class CitrixResourceBase method handleVmStartFailure.

public String handleVmStartFailure(final Connection conn, final String vmName, final VM vm, final String message, final Throwable th) {
    final String msg = "Unable to start " + vmName + " due to " + message;
    s_logger.warn(msg, th);
    if (vm == null) {
        return msg;
    }
    try {
        final VM.Record vmr = vm.getRecord(conn);
        final List<Network> networks = new ArrayList<Network>();
        for (final VIF vif : vmr.VIFs) {
            try {
                final VIF.Record rec = vif.getRecord(conn);
                if (rec != null) {
                    networks.add(rec.network);
                } else {
                    s_logger.warn("Unable to cleanup VIF: " + vif.toWireString() + " As vif record is null");
                }
            } catch (final Exception e) {
                s_logger.warn("Unable to cleanup VIF", e);
            }
        }
        if (vmr.powerState == VmPowerState.RUNNING) {
            try {
                vm.hardShutdown(conn);
            } catch (final Exception e) {
                s_logger.warn("VM hardshutdown failed due to ", e);
            }
        }
        if (vm.getPowerState(conn) == VmPowerState.HALTED) {
            try {
                vm.destroy(conn);
            } catch (final Exception e) {
                s_logger.warn("VM destroy failed due to ", e);
            }
        }
        for (final VBD vbd : vmr.VBDs) {
            try {
                vbd.unplug(conn);
                vbd.destroy(conn);
            } catch (final Exception e) {
                s_logger.warn("Unable to clean up VBD due to ", e);
            }
        }
        for (final VIF vif : vmr.VIFs) {
            try {
                vif.unplug(conn);
                vif.destroy(conn);
            } catch (final Exception e) {
                s_logger.warn("Unable to cleanup VIF", e);
            }
        }
        for (final Network network : networks) {
            if (network.getNameLabel(conn).startsWith("VLAN")) {
                disableVlanNetwork(conn, network, true);
            }
        }
    } catch (final Exception e) {
        s_logger.warn("VM getRecord failed due to ", e);
    }
    return msg;
}
Also used : VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) ArrayList(java.util.ArrayList) VBD(com.xensource.xenapi.VBD) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

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