Search in sources :

Example 1 with VMSnapshotOptions

use of org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotOptions in project cloudstack by apache.

the class VMSnapshotManagerImpl method createRestoreCommand.

@Override
public RestoreVMSnapshotCommand createRestoreCommand(UserVmVO userVm, List<VMSnapshotVO> vmSnapshotVOs) {
    if (!HypervisorType.KVM.equals(userVm.getHypervisorType()))
        return null;
    List<VMSnapshotTO> snapshots = new ArrayList<VMSnapshotTO>();
    Map<Long, VMSnapshotTO> snapshotAndParents = new HashMap<Long, VMSnapshotTO>();
    for (VMSnapshotVO vmSnapshotVO : vmSnapshotVOs) {
        if (vmSnapshotVO.getType() == VMSnapshot.Type.DiskAndMemory) {
            VMSnapshotVO snapshot = _vmSnapshotDao.findById(vmSnapshotVO.getId());
            VMSnapshotTO parent = getSnapshotWithParents(snapshot).getParent();
            VMSnapshotOptions options = snapshot.getOptions();
            boolean quiescevm = true;
            if (options != null)
                quiescevm = options.needQuiesceVM();
            VMSnapshotTO vmSnapshotTO = new VMSnapshotTO(snapshot.getId(), snapshot.getName(), snapshot.getType(), snapshot.getCreated().getTime(), snapshot.getDescription(), snapshot.getCurrent(), parent, quiescevm);
            snapshots.add(vmSnapshotTO);
            snapshotAndParents.put(vmSnapshotVO.getId(), parent);
        }
    }
    if (snapshotAndParents.isEmpty())
        return null;
    // prepare RestoreVMSnapshotCommand
    String vmInstanceName = userVm.getInstanceName();
    List<VolumeObjectTO> volumeTOs = getVolumeTOList(userVm.getId());
    GuestOSVO guestOS = _guestOSDao.findById(userVm.getGuestOSId());
    RestoreVMSnapshotCommand restoreSnapshotCommand = new RestoreVMSnapshotCommand(vmInstanceName, null, volumeTOs, guestOS.getDisplayName());
    restoreSnapshotCommand.setSnapshots(snapshots);
    restoreSnapshotCommand.setSnapshotAndParents(snapshotAndParents);
    return restoreSnapshotCommand;
}
Also used : HashMap(java.util.HashMap) VMSnapshotOptions(org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotOptions) ArrayList(java.util.ArrayList) GuestOSVO(com.cloud.storage.GuestOSVO) RestoreVMSnapshotCommand(com.cloud.agent.api.RestoreVMSnapshotCommand) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO)

Example 2 with VMSnapshotOptions

use of org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotOptions in project cloudstack by apache.

the class DefaultVMSnapshotStrategy method takeVMSnapshot.

@Override
public VMSnapshot takeVMSnapshot(VMSnapshot vmSnapshot) {
    Long hostId = vmSnapshotHelper.pickRunningHost(vmSnapshot.getVmId());
    UserVm userVm = userVmDao.findById(vmSnapshot.getVmId());
    VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
    try {
        vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshotVO, VMSnapshot.Event.CreateRequested);
    } catch (NoTransitionException e) {
        throw new CloudRuntimeException(e.getMessage());
    }
    CreateVMSnapshotAnswer answer = null;
    boolean result = false;
    try {
        GuestOSVO guestOS = guestOSDao.findById(userVm.getGuestOSId());
        List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(userVm.getId());
        VMSnapshotTO current = null;
        VMSnapshotVO currentSnapshot = vmSnapshotDao.findCurrentSnapshotByVmId(userVm.getId());
        if (currentSnapshot != null)
            current = vmSnapshotHelper.getSnapshotWithParents(currentSnapshot);
        VMSnapshotOptions options = ((VMSnapshotVO) vmSnapshot).getOptions();
        boolean quiescevm = true;
        if (options != null)
            quiescevm = options.needQuiesceVM();
        VMSnapshotTO target = new VMSnapshotTO(vmSnapshot.getId(), vmSnapshot.getName(), vmSnapshot.getType(), null, vmSnapshot.getDescription(), false, current, quiescevm);
        if (current == null)
            vmSnapshotVO.setParent(null);
        else
            vmSnapshotVO.setParent(current.getId());
        HostVO host = hostDao.findById(hostId);
        GuestOSHypervisorVO guestOsMapping = guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), host.getHypervisorType().toString(), host.getHypervisorVersion());
        CreateVMSnapshotCommand ccmd = new CreateVMSnapshotCommand(userVm.getInstanceName(), userVm.getUuid(), target, volumeTOs, guestOS.getDisplayName());
        if (guestOsMapping == null) {
            ccmd.setPlatformEmulator(null);
        } else {
            ccmd.setPlatformEmulator(guestOsMapping.getGuestOsName());
        }
        ccmd.setWait(_wait);
        answer = (CreateVMSnapshotAnswer) agentMgr.send(hostId, ccmd);
        if (answer != null && answer.getResult()) {
            processAnswer(vmSnapshotVO, userVm, answer, hostId);
            s_logger.debug("Create vm snapshot " + vmSnapshot.getName() + " succeeded for vm: " + userVm.getInstanceName());
            result = true;
            for (VolumeObjectTO volumeTo : answer.getVolumeTOs()) {
                publishUsageEvent(EventTypes.EVENT_VM_SNAPSHOT_CREATE, vmSnapshot, userVm, volumeTo);
            }
            return vmSnapshot;
        } else {
            String errMsg = "Creating VM snapshot: " + vmSnapshot.getName() + " failed";
            if (answer != null && answer.getDetails() != null)
                errMsg = errMsg + " due to " + answer.getDetails();
            s_logger.error(errMsg);
            throw new CloudRuntimeException(errMsg);
        }
    } catch (OperationTimedoutException e) {
        s_logger.debug("Creating VM snapshot: " + vmSnapshot.getName() + " failed: " + e.toString());
        throw new CloudRuntimeException("Creating VM snapshot: " + vmSnapshot.getName() + " failed: " + e.toString());
    } catch (AgentUnavailableException e) {
        s_logger.debug("Creating VM snapshot: " + vmSnapshot.getName() + " failed", e);
        throw new CloudRuntimeException("Creating VM snapshot: " + vmSnapshot.getName() + " failed: " + e.toString());
    } finally {
        if (!result) {
            try {
                vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationFailed);
            } catch (NoTransitionException e1) {
                s_logger.error("Cannot set vm snapshot state due to: " + e1.getMessage());
            }
        }
    }
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) VMSnapshotOptions(org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotOptions) GuestOSVO(com.cloud.storage.GuestOSVO) CreateVMSnapshotCommand(com.cloud.agent.api.CreateVMSnapshotCommand) HostVO(com.cloud.host.HostVO) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) UserVm(com.cloud.uservm.UserVm) CreateVMSnapshotAnswer(com.cloud.agent.api.CreateVMSnapshotAnswer) VMSnapshotTO(com.cloud.agent.api.VMSnapshotTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO)

Example 3 with VMSnapshotOptions

use of org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotOptions in project cloudstack by apache.

the class VMSnapshotManagerImpl method orchestrateCreateVMSnapshot.

private VMSnapshot orchestrateCreateVMSnapshot(Long vmId, Long vmSnapshotId, Boolean quiescevm) {
    UserVmVO userVm = _userVMDao.findById(vmId);
    if (userVm == null) {
        throw new InvalidParameterValueException("Create vm to snapshot failed due to vm: " + vmId + " is not found");
    }
    List<VolumeVO> volumeVos = _volumeDao.findByInstanceAndType(vmId, Type.ROOT);
    if (volumeVos == null || volumeVos.isEmpty()) {
        throw new CloudRuntimeException("Create vm to snapshot failed due to no root disk found");
    }
    VolumeVO rootVolume = volumeVos.get(0);
    if (!rootVolume.getState().equals(Volume.State.Ready)) {
        throw new CloudRuntimeException("Create vm to snapshot failed due to vm: " + vmId + " has root disk in " + rootVolume.getState() + " state");
    }
    VMSnapshotVO vmSnapshot = _vmSnapshotDao.findById(vmSnapshotId);
    if (vmSnapshot == null) {
        throw new CloudRuntimeException("VM snapshot id: " + vmSnapshotId + " can not be found");
    }
    VMSnapshotOptions options = new VMSnapshotOptions(quiescevm);
    vmSnapshot.setOptions(options);
    try {
        VMSnapshotStrategy strategy = findVMSnapshotStrategy(vmSnapshot);
        VMSnapshot snapshot = strategy.takeVMSnapshot(vmSnapshot);
        return snapshot;
    } catch (Exception e) {
        s_logger.debug("Failed to create vm snapshot: " + vmSnapshotId, e);
        return null;
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VMSnapshotOptions(org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotOptions) VMSnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotStrategy) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) ManagementServerException(com.cloud.exception.ManagementServerException)

Aggregations

VMSnapshotOptions (org.apache.cloudstack.engine.subsystem.api.storage.VMSnapshotOptions)3 VMSnapshotTO (com.cloud.agent.api.VMSnapshotTO)2 GuestOSVO (com.cloud.storage.GuestOSVO)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)2 CreateVMSnapshotAnswer (com.cloud.agent.api.CreateVMSnapshotAnswer)1 CreateVMSnapshotCommand (com.cloud.agent.api.CreateVMSnapshotCommand)1 RestoreVMSnapshotCommand (com.cloud.agent.api.RestoreVMSnapshotCommand)1 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 ManagementServerException (com.cloud.exception.ManagementServerException)1 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)1 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 VirtualMachineMigrationException (com.cloud.exception.VirtualMachineMigrationException)1 HostVO (com.cloud.host.HostVO)1 GuestOSHypervisorVO (com.cloud.storage.GuestOSHypervisorVO)1 VolumeVO (com.cloud.storage.VolumeVO)1