Search in sources :

Example 1 with DettachAnswer

use of com.cloud.storage.command.DettachAnswer in project cosmic by MissionCriticalCloud.

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);
            }
            // Update the VDI's label to be "detached"
            vdi.setNameLabel(conn, "detached");
            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(com.cloud.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 2 with DettachAnswer

use of com.cloud.storage.command.DettachAnswer in project cosmic by MissionCriticalCloud.

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 attach a iso which is not created on nfs: ");
            return new AttachAnswer("Can't attach 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 (!sr.getNameLabel(conn).startsWith("XenServer Tools")) {
            hypervisorResource.removeSR(conn, sr);
        }
        return new DettachAnswer(disk);
    } catch (final XenAPIException e) {
        final String msg = "Failed to dettach 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 dettach volume" + " for uuid: " + data.getPath() + "  due to " + e.getMessage();
        s_logger.warn(msg, e);
        return new DettachAnswer(msg);
    }
}
Also used : PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DettachAnswer(com.cloud.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(com.cloud.storage.to.TemplateObjectTO) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(com.cloud.storage.command.AttachAnswer) SR(com.xensource.xenapi.SR)

Example 3 with DettachAnswer

use of com.cloud.storage.command.DettachAnswer in project cosmic by MissionCriticalCloud.

the class KvmStorageProcessor method dettachVolume.

@Override
public Answer dettachVolume(final DettachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final VolumeObjectTO vol = (VolumeObjectTO) disk.getData();
    final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) vol.getDataStore();
    final String vmName = cmd.getVmName();
    final String serial = resource.diskUuidToSerial(vol.getUuid());
    try {
        final Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
        final KvmPhysicalDisk phyDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        attachOrDetachDisk(conn, false, vmName, phyDisk, disk.getDiskSeq().intValue(), serial);
        storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        return new DettachAnswer(disk);
    } catch (final LibvirtException e) {
        logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        return new DettachAnswer(e.toString());
    } catch (final InternalErrorException e) {
        logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        return new DettachAnswer(e.toString());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DettachAnswer(com.cloud.storage.command.DettachAnswer) Connect(org.libvirt.Connect) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) InternalErrorException(com.cloud.exception.InternalErrorException) DiskTO(com.cloud.agent.api.to.DiskTO)

Aggregations

DiskTO (com.cloud.agent.api.to.DiskTO)3 InternalErrorException (com.cloud.exception.InternalErrorException)3 DettachAnswer (com.cloud.storage.command.DettachAnswer)3 DataTO (com.cloud.agent.api.to.DataTO)2 PrimaryDataStoreTO (com.cloud.storage.to.PrimaryDataStoreTO)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 Connection (com.xensource.xenapi.Connection)2 XenAPIException (com.xensource.xenapi.Types.XenAPIException)2 VBD (com.xensource.xenapi.VBD)2 VDI (com.xensource.xenapi.VDI)2 VM (com.xensource.xenapi.VM)2 XmlRpcException (org.apache.xmlrpc.XmlRpcException)2 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)1 NfsTO (com.cloud.agent.api.to.NfsTO)1 AttachAnswer (com.cloud.storage.command.AttachAnswer)1 TemplateObjectTO (com.cloud.storage.to.TemplateObjectTO)1 VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)1 SR (com.xensource.xenapi.SR)1 Connect (org.libvirt.Connect)1 LibvirtException (org.libvirt.LibvirtException)1