From 5526342f4ab7fe712ec272a89c7a962161889da6 Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Thu, 25 Jun 2020 07:10:31 +0200 Subject: [PATCH] server: Do not resize volume of running vm on KVM host if host is not Up or not Enabled (#4148) If we resize a volume of a vm running on a host which is not Up or not Enable, the job will be scheduled to another normal host. Then the volume will be resized by "qemu-img resize" instead of "virsh blockresize", the image might be corrupted after resize. --- .../com/cloud/storage/VolumeApiServiceImpl.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java index e6022513508..ec24719105a 100644 --- a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java +++ b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java @@ -109,11 +109,13 @@ import com.cloud.exception.ResourceAllocationException; import com.cloud.exception.StorageUnavailableException; import com.cloud.gpu.GPU; import com.cloud.host.HostVO; +import com.cloud.host.Status; import com.cloud.host.dao.HostDao; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.hypervisor.HypervisorCapabilitiesVO; import com.cloud.hypervisor.dao.HypervisorCapabilitiesDao; import com.cloud.org.Grouping; +import com.cloud.resource.ResourceState; import com.cloud.serializer.GsonHelper; import com.cloud.server.ResourceTag; import com.cloud.server.TaggedResourceService; @@ -1187,6 +1189,21 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic if (currentSize != newSize && _volsDao.getHypervisorType(volume.getId()) == HypervisorType.XenServer && !userVm.getState().equals(State.Stopped)) { throw new InvalidParameterValueException(errorMsg); } + + /* Do not resize volume of running vm on KVM host if host is not Up or not Enabled */ + if (currentSize != newSize && userVm.getState() == State.Running && userVm.getHypervisorType() == HypervisorType.KVM) { + if (userVm.getHostId() == null) { + throw new InvalidParameterValueException("Cannot find the hostId of running vm " + userVm.getUuid()); + } + HostVO host = _hostDao.findById(userVm.getHostId()); + if (host == null) { + throw new InvalidParameterValueException("The KVM host where vm is running does not exist"); + } else if (host.getStatus() != Status.Up) { + throw new InvalidParameterValueException("The KVM host where vm is running is not Up"); + } else if (host.getResourceState() != ResourceState.Enabled) { + throw new InvalidParameterValueException("The KVM host where vm is running is not Enabled"); + } + } } ResizeVolumePayload payload = new ResizeVolumePayload(newSize, newMinIops, newMaxIops, newHypervisorSnapshotReserve, shrinkOk, instanceName, hosts, isManaged);