From 7e681a701cceb88b8b111b0b8ba213a4309ce385 Mon Sep 17 00:00:00 2001 From: Koushik Das Date: Mon, 13 Aug 2012 17:11:26 +0530 Subject: [PATCH] CS-15908 : LDV: Prevent attach local data volume to a VM created in a zone with local storage disabled Following scenarios are not allowed in case local storage is disabled on zone: -Creation of volume (new or from snapshot) with local disk offering. -Attach/reattach of volume with local disk offering. Reviewed-by: Nitin --- .../src/com/cloud/storage/StorageManagerImpl.java | 15 ++++++++++----- server/src/com/cloud/vm/UserVmManagerImpl.java | 12 +++++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java index 7df4802feda..25ab1ed0ae3 100755 --- a/server/src/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/com/cloud/storage/StorageManagerImpl.java @@ -1856,6 +1856,7 @@ public class StorageManagerImpl implements StorageManager, Manager, ClusterManag Long zoneId = cmd.getZoneId(); Long diskOfferingId = null; + DiskOfferingVO diskOffering = null; Long size = null; // validate input parameters before creating the volume @@ -1875,11 +1876,9 @@ public class StorageManagerImpl implements StorageManager, Manager, ClusterManag throw new InvalidParameterValueException("Disk size must be larger than 0", null); } } - if (diskOfferingId == null) { - throw new InvalidParameterValueException("Missing parameter(s),either a positive volume size or a valid disk offering id must be specified.", null); - } + // Check that the the disk offering is specified - DiskOfferingVO diskOffering = _diskOfferingDao.findById(diskOfferingId); + diskOffering = _diskOfferingDao.findById(diskOfferingId); if ((diskOffering == null) || diskOffering.getRemoved() != null || !DiskOfferingVO.Type.Disk.equals(diskOffering.getType())) { throw new InvalidParameterValueException("Please specify a valid disk offering.", null); } @@ -1924,7 +1923,8 @@ public class StorageManagerImpl implements StorageManager, Manager, ClusterManag Snapshot.Status.BackedUp + " state yet and can't be used for volume creation", idList); } - diskOfferingId = (cmd.getDiskOfferingId() != null) ? cmd.getDiskOfferingId() : snapshotCheck.getDiskOfferingId(); + diskOfferingId = snapshotCheck.getDiskOfferingId(); + diskOffering = _diskOfferingDao.findById(diskOfferingId); zoneId = snapshotCheck.getDataCenterId(); size = snapshotCheck.getSize(); // ; disk offering is used for tags purposes @@ -1953,6 +1953,11 @@ public class StorageManagerImpl implements StorageManager, Manager, ClusterManag throw new PermissionDeniedException("Cannot perform this operation, Zone is currently disabled: " + zoneId); } + // If local storage is disabled then creation of volume with local disk offering not allowed + if (!zone.isLocalStorageEnabled() && diskOffering.getUseLocalStorage()) { + throw new InvalidParameterValueException("Zone is not configured to use local storage but volume's disk offering " + diskOffering.getName() + " uses it", null); + } + // Check that there is a shared primary storage pool in the specified zone List storagePools = _storagePoolDao.listByDataCenterId(zoneId); boolean sharedPoolExists = false; diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 4e78b1e3f52..e180e5eb7a4 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -134,6 +134,7 @@ import com.cloud.network.security.SecurityGroup; import com.cloud.network.security.SecurityGroupManager; import com.cloud.network.security.dao.SecurityGroupDao; import com.cloud.network.security.dao.SecurityGroupVMMapDao; +import com.cloud.offering.DiskOffering; import com.cloud.offering.NetworkOffering; import com.cloud.offering.NetworkOffering.Availability; import com.cloud.offering.ServiceOffering; @@ -581,6 +582,15 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager throw new InvalidParameterValueException("Please specify a VM that is in the same zone as the volume.", null); } + // If local storage is disabled then attaching a volume with local disk offering not allowed + DataCenterVO dataCenter = _dcDao.findById(volume.getDataCenterId()); + if (!dataCenter.isLocalStorageEnabled()) { + DiskOfferingVO diskOffering = _diskOfferingDao.findById(volume.getDiskOfferingId()); + if (diskOffering.getUseLocalStorage()) { + throw new InvalidParameterValueException("Zone is not configured to use local storage but volume's disk offering " + diskOffering.getName() + " uses it", null); + } + } + //permission check _accountMgr.checkAccess(caller, null, true, volume, vm); @@ -728,7 +738,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager throw new CloudRuntimeException(e.toString()); } } else { - throw new CloudRuntimeException("Moving a local data volume " + volume + " is not allowed"); + throw new CloudRuntimeException("Failed to attach local data volume " + volume.getName() + " to VM " + vm.getDisplayName() + " as migration of local data volume is not allowed"); } } }