Arik Hadas has uploaded a new change for review. Change subject: core: fix removal of attached disks to stateless vm ......................................................................
core: fix removal of attached disks to stateless vm Change the way we handle disks when stateless VM goes down, to prevent situations where disks are being removed. This might happen when disk is attached to stateless VM while it was running (hot-plug) so no snapshot was taken for it as part of the stateless snapshot of the VM, and when restoring the stateless snapshot we'll remove it. The solution is to find disks which don't have volumes attached to the stateless snapshot and to detach them from the VM before restoring the stateless snapshot. Change-Id: I3a1d6eb8d2f4606622c7ed5c73370792406bb9b3 Bug-Url: https://bugzilla.redhat.com/1174812 Signed-off-by: Arik Hadas <aha...@redhat.com> --- M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DetachDiskFromVmCommand.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreStatelessVmCommand.java 2 files changed, 46 insertions(+), 12 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/49/36249/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DetachDiskFromVmCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DetachDiskFromVmCommand.java index 882235a..e4daf4a 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DetachDiskFromVmCommand.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/DetachDiskFromVmCommand.java @@ -1,5 +1,6 @@ package org.ovirt.engine.core.bll; +import org.ovirt.engine.core.bll.context.CommandContext; import org.ovirt.engine.core.bll.utils.VmDeviceUtils; import org.ovirt.engine.core.bll.validator.LocalizedVmStatus; import org.ovirt.engine.core.common.AuditLogType; @@ -20,7 +21,11 @@ private VmDevice vmDevice; public DetachDiskFromVmCommand(T parameters) { - super(parameters); + this(parameters, null); + } + + public DetachDiskFromVmCommand(T parameters, CommandContext cmdContext) { + super(parameters, cmdContext); } @Override diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreStatelessVmCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreStatelessVmCommand.java index 4cf4630..54ab2b0 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreStatelessVmCommand.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/RestoreStatelessVmCommand.java @@ -1,15 +1,17 @@ package org.ovirt.engine.core.bll; -import org.ovirt.engine.core.bll.context.CommandContext; - +import java.util.Collections; import java.util.List; +import org.ovirt.engine.core.bll.context.CommandContext; +import org.ovirt.engine.core.common.action.AttachDetachVmDiskParameters; import org.ovirt.engine.core.common.action.RestoreAllSnapshotsParameters; import org.ovirt.engine.core.common.action.UpdateVmVersionParameters; import org.ovirt.engine.core.common.action.VdcActionType; import org.ovirt.engine.core.common.action.VdcReturnValueBase; import org.ovirt.engine.core.common.action.VmOperationParameterBase; import org.ovirt.engine.core.common.businessentities.DiskImage; +import org.ovirt.engine.core.common.businessentities.Entities; import org.ovirt.engine.core.common.businessentities.Snapshot.SnapshotType; import org.ovirt.engine.core.common.businessentities.SnapshotActionEnum; import org.ovirt.engine.core.compat.Guid; @@ -53,20 +55,47 @@ } private boolean restoreInitialState() { - Guid snapshotId = getSnapshotDao().getId(getVmId(), SnapshotType.STATELESS); - if (snapshotId == null) { + Guid statelessVmSnapshotId = getVmSnapshotIdForType(SnapshotType.STATELESS); + if (statelessVmSnapshotId == null) { return true; } - List<DiskImage> imagesList = getDiskImageDao().getAllSnapshotsForVmSnapshot(snapshotId); - if (imagesList == null || imagesList.isEmpty()) { - return true; + List<DiskImage> statelessDiskSnapshots = getDiskSnapshotsForVmSnapshot(statelessVmSnapshotId); + + Guid activeVmSnapshotId = getVmSnapshotIdForType(SnapshotType.ACTIVE); + List<DiskImage> activeDiskSnapshots = getDiskSnapshotsForVmSnapshot(activeVmSnapshotId); + List<Guid> disksWithStatelessSnapshot = Entities.getIds(statelessDiskSnapshots); + for (DiskImage activeDiskSnapshot : activeDiskSnapshots) { + if (!disksWithStatelessSnapshot.contains(activeDiskSnapshot.getId())) { + VdcReturnValueBase returnValue = runInternalAction ( + VdcActionType.DetachDiskFromVm, + new AttachDetachVmDiskParameters( + getVmId(), activeDiskSnapshot.getId(), false, false)); + + if (!returnValue.getSucceeded()) { + log.error("Could not restore stateless VM {} due to a failure to detach Disk {}", + getVmId(), activeDiskSnapshot.getId()); + return false; + } + } } - // restore all snapshots - return runInternalActionWithTasksContext(VdcActionType.RestoreAllSnapshots, - buildRestoreAllSnapshotsParameters(imagesList), - getLock()).getSucceeded(); + if (!statelessDiskSnapshots.isEmpty()) { + // restore all snapshots + return runInternalActionWithTasksContext(VdcActionType.RestoreAllSnapshots, + buildRestoreAllSnapshotsParameters(statelessDiskSnapshots), + getLock()).getSucceeded(); + } + return true; + } + + private Guid getVmSnapshotIdForType(SnapshotType type) { + return getSnapshotDao().getId(getVmId(), type); + } + + private List<DiskImage> getDiskSnapshotsForVmSnapshot(Guid snapshotId) { + List<DiskImage> images = getDiskImageDao().getAllSnapshotsForVmSnapshot(snapshotId); + return images != null ? images : Collections.<DiskImage>emptyList(); } private RestoreAllSnapshotsParameters buildRestoreAllSnapshotsParameters(List<DiskImage> imagesList) { -- To view, visit http://gerrit.ovirt.org/36249 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I3a1d6eb8d2f4606622c7ed5c73370792406bb9b3 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: ovirt-engine-3.5 Gerrit-Owner: Arik Hadas <aha...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches