Search in sources :

Example 36 with VolumeObjectTO

use of com.cloud.storage.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.

the class LibvirtRestoreVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RestoreVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    final String vmName = cmd.getVmName();
    final List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
    final PowerState vmState = PowerState.PowerOn;
    Domain dm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnection();
        dm = libvirtComputingResource.getDomain(conn, vmName);
        if (dm == null) {
            return new RestoreVMSnapshotAnswer(cmd, false, "Restore VM Snapshot Failed due to can not find vm: " + vmName);
        }
        final String xmlDesc = dm.getXMLDesc(0);
        final List<VMSnapshotTO> snapshots = cmd.getSnapshots();
        final Map<Long, VMSnapshotTO> snapshotAndParents = cmd.getSnapshotAndParents();
        for (final VMSnapshotTO snapshot : snapshots) {
            final VMSnapshotTO parent = snapshotAndParents.get(snapshot.getId());
            final String vmSnapshotXML = libvirtUtilitiesHelper.generateVMSnapshotXML(snapshot, parent, xmlDesc);
            s_logger.debug("Restoring vm snapshot " + snapshot.getSnapshotName() + " on " + vmName + " with XML:\n " + vmSnapshotXML);
            try {
                // VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE = 1
                int flags = 1;
                if (snapshot.getCurrent()) {
                    // VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT = 2
                    flags += 2;
                }
                dm.snapshotCreateXML(vmSnapshotXML, flags);
            } catch (final LibvirtException e) {
                s_logger.debug("Failed to restore vm snapshot " + snapshot.getSnapshotName() + " on " + vmName);
                return new RestoreVMSnapshotAnswer(cmd, false, e.toString());
            }
        }
        return new RestoreVMSnapshotAnswer(cmd, listVolumeTo, vmState);
    } catch (final LibvirtException e) {
        final String msg = " Restore snapshot failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new RestoreVMSnapshotAnswer(cmd, false, msg);
    } finally {
        if (dm != null) {
            try {
                dm.free();
            } catch (final LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) Domain(org.libvirt.Domain) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Example 37 with VolumeObjectTO

use of com.cloud.storage.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.

the class LibvirtRevertToVMSnapshotCommandWrapper method execute.

@Override
public Answer execute(final RevertToVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
    final String vmName = cmd.getVmName();
    final List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
    final VMSnapshot.Type vmSnapshotType = cmd.getTarget().getType();
    final Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
    PowerState vmState;
    Domain dm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnection();
        dm = libvirtComputingResource.getDomain(conn, vmName);
        if (dm == null) {
            return new RevertToVMSnapshotAnswer(cmd, false, "Revert to VM Snapshot Failed due to can not find vm: " + vmName);
        }
        final DomainSnapshot snapshot = dm.snapshotLookupByName(cmd.getTarget().getSnapshotName());
        if (snapshot == null) {
            return new RevertToVMSnapshotAnswer(cmd, false, "Cannot find vmSnapshot with name: " + cmd.getTarget().getSnapshotName());
        }
        dm.revertToSnapshot(snapshot);
        snapshot.free();
        if (!snapshotMemory) {
            dm.destroy();
            if (dm.isPersistent() == 1) {
                dm.undefine();
            }
            vmState = PowerState.PowerOff;
        } else {
            vmState = PowerState.PowerOn;
        }
        return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
    } catch (final LibvirtException e) {
        final String msg = " Revert to VM snapshot failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new RevertToVMSnapshotAnswer(cmd, false, msg);
    } finally {
        if (dm != null) {
            try {
                dm.free();
            } catch (LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
            ;
        }
    }
}
Also used : RevertToVMSnapshotAnswer(com.cloud.agent.api.RevertToVMSnapshotAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) DomainSnapshot(org.libvirt.DomainSnapshot) VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) Domain(org.libvirt.Domain) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Example 38 with VolumeObjectTO

use of com.cloud.storage.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method handlePath.

private void handlePath(final DiskTO[] disks, final HypervisorType hypervisorType) {
    if (hypervisorType != HypervisorType.KVM) {
        return;
    }
    if (disks != null) {
        for (final DiskTO disk : disks) {
            final Map<String, String> details = disk.getDetails();
            final boolean isManaged = details != null && Boolean.parseBoolean(details.get(DiskTO.MANAGED));
            if (isManaged && disk.getPath() == null) {
                final Long volumeId = disk.getData().getId();
                final VolumeVO volume = _volsDao.findById(volumeId);
                disk.setPath(volume.get_iScsiName());
                if (disk.getData() instanceof VolumeObjectTO) {
                    final VolumeObjectTO volTo = (VolumeObjectTO) disk.getData();
                    volTo.setPath(volume.get_iScsiName());
                }
                volume.setPath(volume.get_iScsiName());
                _volsDao.update(volumeId, volume);
            }
        }
    }
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 39 with VolumeObjectTO

use of com.cloud.storage.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method syncDiskChainChange.

private void syncDiskChainChange(final StartAnswer answer) {
    final VirtualMachineTO vmSpec = answer.getVirtualMachine();
    for (final DiskTO disk : vmSpec.getDisks()) {
        if (disk.getType() != Volume.Type.ISO) {
            final VolumeObjectTO vol = (VolumeObjectTO) disk.getData();
            final VolumeVO volume = _volsDao.findById(vol.getId());
            // returned null instead of an actual path (because it was out of date with the DB).
            if (vol.getPath() != null) {
                volumeMgr.updateVolumeDiskChain(vol.getId(), vol.getPath(), vol.getChainInfo());
            } else {
                volumeMgr.updateVolumeDiskChain(vol.getId(), volume.getPath(), vol.getChainInfo());
            }
        }
    }
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 40 with VolumeObjectTO

use of com.cloud.storage.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.

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 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<>();
    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;
        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(com.cloud.storage.to.VolumeObjectTO) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

VolumeObjectTO (com.cloud.storage.to.VolumeObjectTO)58 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)36 PrimaryDataStoreTO (com.cloud.storage.to.PrimaryDataStoreTO)22 CopyCmdAnswer (com.cloud.storage.command.CopyCmdAnswer)21 Connection (com.xensource.xenapi.Connection)19 InternalErrorException (com.cloud.exception.InternalErrorException)18 VDI (com.xensource.xenapi.VDI)17 NfsTO (com.cloud.agent.api.to.NfsTO)16 DataTO (com.cloud.agent.api.to.DataTO)15 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)14 ArrayList (java.util.ArrayList)14 SR (com.xensource.xenapi.SR)13 XenAPIException (com.xensource.xenapi.Types.XenAPIException)13 XmlRpcException (org.apache.xmlrpc.XmlRpcException)13 VMSnapshotTO (com.cloud.agent.api.VMSnapshotTO)10 IOException (java.io.IOException)10 LibvirtException (org.libvirt.LibvirtException)10 VolumeVO (com.cloud.storage.VolumeVO)9 URI (java.net.URI)9 HashMap (java.util.HashMap)8