Search in sources :

Example 1 with AttachAnswer

use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.

the class KVMStorageProcessor method attachVolume.

@Override
public Answer attachVolume(final AttachCommand 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);
        storagePoolMgr.connectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath(), disk.getDetails());
        final KVMPhysicalDisk phyDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        final String volCacheMode = vol.getCacheMode() == null ? null : vol.getCacheMode().toString();
        attachOrDetachDisk(conn, true, vmName, phyDisk, disk.getDiskSeq().intValue(), serial, vol.getBytesReadRate(), vol.getBytesReadRateMax(), vol.getBytesReadRateMaxLength(), vol.getBytesWriteRate(), vol.getBytesWriteRateMax(), vol.getBytesWriteRateMaxLength(), vol.getIopsReadRate(), vol.getIopsReadRateMax(), vol.getIopsReadRateMaxLength(), vol.getIopsWriteRate(), vol.getIopsWriteRateMax(), vol.getIopsWriteRateMaxLength(), volCacheMode);
        return new AttachAnswer(disk);
    } catch (final LibvirtException e) {
        s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
        return new AttachAnswer(e.toString());
    } catch (final InternalErrorException e) {
        s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        return new AttachAnswer(e.toString());
    } catch (final CloudRuntimeException e) {
        s_logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
        return new AttachAnswer(e.toString());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connect(org.libvirt.Connect) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) InternalErrorException(com.cloud.exception.InternalErrorException) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Example 2 with AttachAnswer

use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.

the class LibvirtComputingResourceTest method testStorageSubSystemCommand.

@Test
public void testStorageSubSystemCommand() {
    final DiskTO disk = Mockito.mock(DiskTO.class);
    final String vmName = "Test";
    final AttachCommand command = new AttachCommand(disk, vmName);
    final StorageSubsystemCommandHandler handler = Mockito.mock(StorageSubsystemCommandHandler.class);
    when(libvirtComputingResource.getStorageHandler()).thenReturn(handler);
    when(handler.handleStorageCommands(command)).thenReturn(new AttachAnswer(disk));
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertTrue(answer.getResult());
}
Also used : UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) StorageSubsystemCommandHandler(com.cloud.storage.resource.StorageSubsystemCommandHandler) AttachCommand(org.apache.cloudstack.storage.command.AttachCommand) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with AttachAnswer

use of org.apache.cloudstack.storage.command.AttachAnswer 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 AttachAnswer

use of org.apache.cloudstack.storage.command.AttachAnswer 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 AttachAnswer

use of org.apache.cloudstack.storage.command.AttachAnswer in project cloudstack by apache.

the class SimulatorStorageProcessor method attachIso.

@Override
public Answer attachIso(AttachCommand cmd) {
    DiskTO disk = cmd.getDisk();
    TemplateObjectTO isoTO = (TemplateObjectTO) disk.getData();
    DataStoreTO store = isoTO.getDataStore();
    if (!(store instanceof NfsTO)) {
        return new AttachAnswer("unsupported protocol");
    }
    return new Answer(cmd);
}
Also used : CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) ResignatureAnswer(org.apache.cloudstack.storage.command.ResignatureAnswer) Answer(com.cloud.agent.api.Answer) DettachAnswer(org.apache.cloudstack.storage.command.DettachAnswer) SnapshotAndCopyAnswer(org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer)

Aggregations

AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)12 DiskTO (com.cloud.agent.api.to.DiskTO)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)7 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)5 NfsTO (com.cloud.agent.api.to.NfsTO)5 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)5 Answer (com.cloud.agent.api.Answer)4 DataTO (com.cloud.agent.api.to.DataTO)4 InternalErrorException (com.cloud.exception.InternalErrorException)4 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)4 Connection (com.xensource.xenapi.Connection)3 XenAPIException (com.xensource.xenapi.Types.XenAPIException)3 VBD (com.xensource.xenapi.VBD)3 VDI (com.xensource.xenapi.VDI)3 VM (com.xensource.xenapi.VM)3 AttachCommand (org.apache.cloudstack.storage.command.AttachCommand)3 DettachAnswer (org.apache.cloudstack.storage.command.DettachAnswer)3 XmlRpcException (org.apache.xmlrpc.XmlRpcException)3 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)2 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)2