Search in sources :

Example 86 with VolumeObjectTO

use of org.apache.cloudstack.storage.to.VolumeObjectTO in project cloudstack by apache.

the class VmwareStorageManagerImpl method execute.

@Override
public RevertToVMSnapshotAnswer execute(VmwareHostService hostService, RevertToVMSnapshotCommand cmd) {
    String snapshotName = cmd.getTarget().getSnapshotName();
    String vmName = cmd.getVmName();
    Boolean snapshotMemory = cmd.getTarget().getType() == VMSnapshot.Type.DiskAndMemory;
    List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
    VirtualMachine.PowerState vmState = VirtualMachine.PowerState.PowerOn;
    VirtualMachineMO vmMo = null;
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        // wait if there are already VM revert task running
        ManagedObjectReference taskmgr = context.getServiceContent().getTaskManager();
        List<ManagedObjectReference> tasks = context.getVimClient().getDynamicProperty(taskmgr, "recentTask");
        for (ManagedObjectReference taskMor : tasks) {
            TaskInfo info = (TaskInfo) (context.getVimClient().getDynamicProperty(taskMor, "info"));
            if (info.getEntityName().equals(cmd.getVmName()) && StringUtils.isNotBlank(info.getName()) && info.getName().equalsIgnoreCase("RevertToSnapshot_Task")) {
                s_logger.debug("There is already a VM snapshot task running, wait for it");
                context.getVimClient().waitForTask(taskMor);
            }
        }
        HostMO hostMo = (HostMO) hyperHost;
        vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
        }
        if (vmMo == null) {
            String msg = "Unable to find VM for RevertToVMSnapshotCommand";
            s_logger.debug(msg);
            return new RevertToVMSnapshotAnswer(cmd, false, msg);
        } else {
            if (cmd.isReloadVm()) {
                vmMo.reload();
            }
            boolean result = false;
            if (snapshotName != null) {
                ManagedObjectReference morSnapshot = vmMo.getSnapshotMor(snapshotName);
                result = hostMo.revertToSnapshot(morSnapshot);
            } else {
                return new RevertToVMSnapshotAnswer(cmd, false, "Unable to find the snapshot by name " + snapshotName);
            }
            if (result) {
                Map<String, String> mapNewDisk = getNewDiskMap(vmMo);
                setVolumeToPathAndSize(listVolumeTo, mapNewDisk, context, hyperHost, cmd.getVmName());
                if (!snapshotMemory) {
                    vmState = VirtualMachine.PowerState.PowerOff;
                }
                return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
            } else {
                return new RevertToVMSnapshotAnswer(cmd, false, "Error while reverting to snapshot due to execute in ESXi");
            }
        }
    } catch (Exception e) {
        String msg = "revert vm " + vmName + " to snapshot " + snapshotName + " failed due to " + e.getMessage();
        s_logger.error(msg);
        return new RevertToVMSnapshotAnswer(cmd, false, msg);
    }
}
Also used : RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TaskInfo(com.vmware.vim25.TaskInfo) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VirtualMachine(com.cloud.vm.VirtualMachine) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 87 with VolumeObjectTO

use of org.apache.cloudstack.storage.to.VolumeObjectTO in project cloudstack by apache.

the class LibvirtRevertSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
    SnapshotObjectTO snapshot = command.getData();
    VolumeObjectTO volume = snapshot.getVolume();
    PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
    DataStoreTO snapshotImageStore = snapshot.getDataStore();
    if (!(snapshotImageStore instanceof NfsTO)) {
        return new Answer(command, false, "revert snapshot on object storage is not implemented yet");
    }
    NfsTO nfsImageStore = (NfsTO) snapshotImageStore;
    String secondaryStoragePoolUrl = nfsImageStore.getUrl();
    String volumePath = volume.getPath();
    String snapshotPath = null;
    String snapshotRelPath = null;
    KVMStoragePool secondaryStoragePool = null;
    try {
        final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
        secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
        String ssPmountPath = secondaryStoragePool.getLocalPath();
        snapshotRelPath = snapshot.getPath();
        snapshotPath = ssPmountPath + File.separator + snapshotRelPath;
        KVMPhysicalDisk snapshotDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volumePath);
        KVMStoragePool primaryPool = snapshotDisk.getPool();
        if (primaryPool.getType() == StoragePoolType.RBD) {
            return new Answer(command, false, "revert snapshot to RBD is not implemented yet");
        } else {
            Script cmd = new Script(libvirtComputingResource.manageSnapshotPath(), libvirtComputingResource.getCmdsTimeout(), s_logger);
            cmd.add("-v", snapshotPath);
            cmd.add("-n", snapshotDisk.getName());
            cmd.add("-p", snapshotDisk.getPath());
            String result = cmd.execute();
            if (result != null) {
                s_logger.debug("Failed to revert snaptshot: " + result);
                return new Answer(command, false, result);
            }
        }
        return new Answer(command, true, "RevertSnapshotCommand executes successfully");
    } catch (CloudRuntimeException e) {
        return new Answer(command, false, e.toString());
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) Answer(com.cloud.agent.api.Answer) Script(com.cloud.utils.script.Script) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) KVMStoragePool(com.cloud.hypervisor.kvm.storage.KVMStoragePool) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) KVMPhysicalDisk(com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO)

Example 88 with VolumeObjectTO

use of org.apache.cloudstack.storage.to.VolumeObjectTO in project cloudstack by apache.

the class Ovm3StorageProcessor method createSnapshot.

/**
     * Creates a snapshot from a volume, but only if the VM is stopped.
     * This due qemu not being able to snap raw volumes.
     *
     * if stopped yes, if running ... no, unless we have ocfs2 when
     * using raw partitions (file:) if using tap:aio we cloud...
     * The "ancient" way:
     * We do however follow the "two stage" approach, of "snap"
     * on primary first, with the create object... and then
     * backup the snapshot with the copycmd....
     * (should transfer to createSnapshot, backupSnapshot)
     */
@Override
public Answer createSnapshot(CreateObjectCommand cmd) {
    LOGGER.debug("execute createSnapshot: " + cmd.getClass());
    DataTO data = cmd.getData();
    Xen xen = new Xen(c);
    SnapshotObjectTO snap = (SnapshotObjectTO) data;
    VolumeObjectTO vol = snap.getVolume();
    try {
        Xen.Vm vm = xen.getVmConfig(snap.getVmName());
        if (vm != null) {
            return new CreateObjectAnswer("Snapshot object creation not supported for running VMs." + snap.getVmName());
        }
        Linux host = new Linux(c);
        String uuid = host.newUuid();
        /* for root volumes this works... */
        String src = vol.getPath() + "/" + vol.getUuid() + ".raw";
        String dest = vol.getPath() + "/" + uuid + ".raw";
        /* seems that sometimes the path is already contains a file
             * in case, we just replace it.... (Seems to happen if not ROOT)
             */
        if (vol.getPath().contains(vol.getUuid())) {
            src = getVirtualDiskPath(vol.getUuid(), data.getDataStore().getUuid());
            dest = src.replace(vol.getUuid(), uuid);
        }
        LOGGER.debug("Snapshot " + src + " to " + dest);
        host.copyFile(src, dest);
        SnapshotObjectTO nsnap = new SnapshotObjectTO();
        // nsnap.setPath(dest);
        // move to something that looks the same as xenserver.
        nsnap.setPath(uuid);
        return new CreateObjectAnswer(nsnap);
    } catch (Ovm3ResourceException e) {
        return new CreateObjectAnswer("Snapshot object creation failed. " + e.getMessage());
    }
}
Also used : Xen(com.cloud.hypervisor.ovm3.objects.Xen) SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) DataTO(com.cloud.agent.api.to.DataTO) Linux(com.cloud.hypervisor.ovm3.objects.Linux) Ovm3ResourceException(com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException) CreateObjectAnswer(org.apache.cloudstack.storage.command.CreateObjectAnswer) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO)

Example 89 with VolumeObjectTO

use of org.apache.cloudstack.storage.to.VolumeObjectTO in project cloudstack by apache.

the class CitrixResourceBase method createWorkingVM.

public VM createWorkingVM(final Connection conn, final String vmName, final String guestOSType, final String platformEmulator, final List<VolumeObjectTO> listVolumeTo) throws BadServerResponse, Types.VmBadPowerState, Types.SrFull, Types.OperationNotAllowed, XenAPIException, XmlRpcException {
    // below is redundant but keeping for consistency and code readabilty
    final String guestOsTypeName = platformEmulator;
    if (guestOsTypeName == null) {
        final String msg = " Hypervisor " + this.getClass().getName() + " doesn't support guest OS type " + guestOSType + ". you can choose 'Other install media' to run it as HVM";
        s_logger.warn(msg);
        throw new CloudRuntimeException(msg);
    }
    final VM template = getVM(conn, guestOsTypeName);
    final VM vm = template.createClone(conn, vmName);
    vm.setIsATemplate(conn, false);
    final Map<VDI, VolumeObjectTO> vdiMap = new HashMap<VDI, VolumeObjectTO>();
    for (final VolumeObjectTO volume : listVolumeTo) {
        final String vdiUuid = volume.getPath();
        try {
            final VDI vdi = VDI.getByUuid(conn, vdiUuid);
            vdiMap.put(vdi, volume);
        } catch (final Types.UuidInvalid e) {
            s_logger.warn("Unable to find vdi by uuid: " + vdiUuid + ", skip it");
        }
    }
    for (final Map.Entry<VDI, VolumeObjectTO> entry : vdiMap.entrySet()) {
        final VDI vdi = entry.getKey();
        final VolumeObjectTO volumeTO = entry.getValue();
        final VBD.Record vbdr = new VBD.Record();
        vbdr.VM = vm;
        vbdr.VDI = vdi;
        if (volumeTO.getVolumeType() == Volume.Type.ROOT) {
            vbdr.bootable = true;
            vbdr.unpluggable = false;
        } else {
            vbdr.bootable = false;
            vbdr.unpluggable = true;
        }
        vbdr.userdevice = "autodetect";
        vbdr.mode = Types.VbdMode.RW;
        vbdr.type = Types.VbdType.DISK;
        Long deviceId = volumeTO.getDeviceId();
        if (deviceId != null && (!isDeviceUsed(conn, vm, deviceId) || deviceId > 3)) {
            vbdr.userdevice = deviceId.toString();
        }
        VBD.create(conn, vbdr);
    }
    return vm;
}
Also used : Types(com.xensource.xenapi.Types) HashMap(java.util.HashMap) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) Map(java.util.Map) HashMap(java.util.HashMap)

Example 90 with VolumeObjectTO

use of org.apache.cloudstack.storage.to.VolumeObjectTO in project cloudstack by apache.

the class CitrixResourceBase method mount.

protected VDI mount(final Connection conn, final String vmName, final DiskTO volume) throws XmlRpcException, XenAPIException {
    final DataTO data = volume.getData();
    final Volume.Type type = volume.getType();
    if (type == Volume.Type.ISO) {
        final TemplateObjectTO iso = (TemplateObjectTO) data;
        final DataStoreTO store = iso.getDataStore();
        if (store == null) {
            // It's a fake iso
            return null;
        }
        // corer case, xenserver pv driver iso
        final String templateName = iso.getName();
        if (templateName.startsWith("xs-tools")) {
            try {
                final Set<VDI> vdis = VDI.getByNameLabel(conn, templateName);
                if (vdis.isEmpty()) {
                    throw new CloudRuntimeException("Could not find ISO with URL: " + templateName);
                }
                return vdis.iterator().next();
            } catch (final XenAPIException e) {
                throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
            } catch (final Exception e) {
                throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
            }
        }
        if (!(store instanceof NfsTO)) {
            throw new CloudRuntimeException("only support mount iso on nfs");
        }
        final NfsTO nfsStore = (NfsTO) store;
        final String isoPath = nfsStore.getUrl() + File.separator + iso.getPath();
        final int index = isoPath.lastIndexOf("/");
        final String mountpoint = isoPath.substring(0, index);
        URI uri;
        try {
            uri = new URI(mountpoint);
        } catch (final URISyntaxException e) {
            throw new CloudRuntimeException("Incorrect uri " + mountpoint, e);
        }
        final SR isoSr = createIsoSRbyURI(conn, uri, vmName, false);
        final String isoname = isoPath.substring(index + 1);
        final VDI isoVdi = getVDIbyLocationandSR(conn, isoname, isoSr);
        if (isoVdi == null) {
            throw new CloudRuntimeException("Unable to find ISO " + isoPath);
        }
        return isoVdi;
    } else {
        final VolumeObjectTO vol = (VolumeObjectTO) data;
        return VDI.getByUuid(conn, vol.getPath());
    }
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) URISyntaxException(java.net.URISyntaxException) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) 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) DataTO(com.cloud.agent.api.to.DataTO) Volume(com.cloud.storage.Volume) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) SR(com.xensource.xenapi.SR)

Aggregations

VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)108 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)59 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)45 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)36 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)36 DataTO (com.cloud.agent.api.to.DataTO)31 NfsTO (com.cloud.agent.api.to.NfsTO)31 InternalErrorException (com.cloud.exception.InternalErrorException)27 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)20 Connection (com.xensource.xenapi.Connection)19 VDI (com.xensource.xenapi.VDI)17 IOException (java.io.IOException)17 Answer (com.cloud.agent.api.Answer)16 DiskTO (com.cloud.agent.api.to.DiskTO)16 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)16 RemoteException (java.rmi.RemoteException)16 ArrayList (java.util.ArrayList)16 SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)16 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14