Search in sources :

Example 1 with DettachAnswer

use of com.cloud.legacymodel.communication.answer.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 = this.resource.diskUuidToSerial(vol.getUuid());
    try {
        final Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
        final KvmPhysicalDisk phyDisk = this.storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        attachOrDetachDisk(conn, false, vmName, phyDisk, disk.getDiskSeq().intValue(), disk.getDiskController(), disk.getDiskFormat(), serial, vol);
        this.storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        return new DettachAnswer(disk);
    } catch (final LibvirtException | InternalErrorException e) {
        this.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.legacymodel.to.PrimaryDataStoreTO) DettachAnswer(com.cloud.legacymodel.communication.answer.DettachAnswer) Connect(org.libvirt.Connect) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) DiskTO(com.cloud.legacymodel.to.DiskTO)

Example 2 with DettachAnswer

use of com.cloud.legacymodel.communication.answer.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.legacymodel.communication.answer.DettachAnswer) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DataTO(com.cloud.legacymodel.to.DataTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) DiskTO(com.cloud.legacymodel.to.DiskTO)

Example 3 with DettachAnswer

use of com.cloud.legacymodel.communication.answer.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.legacymodel.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) DettachAnswer(com.cloud.legacymodel.communication.answer.DettachAnswer) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) NfsTO(com.cloud.legacymodel.to.NfsTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DataTO(com.cloud.legacymodel.to.DataTO) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.legacymodel.to.TemplateObjectTO) DiskTO(com.cloud.legacymodel.to.DiskTO) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) SR(com.xensource.xenapi.SR)

Aggregations

DettachAnswer (com.cloud.legacymodel.communication.answer.DettachAnswer)3 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)3 DiskTO (com.cloud.legacymodel.to.DiskTO)3 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)2 DataTO (com.cloud.legacymodel.to.DataTO)2 PrimaryDataStoreTO (com.cloud.legacymodel.to.PrimaryDataStoreTO)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 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)1 DataStoreTO (com.cloud.legacymodel.to.DataStoreTO)1 NfsTO (com.cloud.legacymodel.to.NfsTO)1 TemplateObjectTO (com.cloud.legacymodel.to.TemplateObjectTO)1 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)1 SR (com.xensource.xenapi.SR)1 Connect (org.libvirt.Connect)1 LibvirtException (org.libvirt.LibvirtException)1