diff --git a/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/driver/OntapPrimaryDatastoreDriver.java b/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/driver/OntapPrimaryDatastoreDriver.java index 1375d5d17c2d..169b6dd17be3 100644 --- a/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/driver/OntapPrimaryDatastoreDriver.java +++ b/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/driver/OntapPrimaryDatastoreDriver.java @@ -503,18 +503,26 @@ private CreateCmdResult createTemplateOnPrimary(StoragePoolVO storagePool, Templ } /** - * Persists SAN cache identity on {@code template_spool_ref} when a LUN was created. - * NFS returns the template uuid as the create path; {@code install_path} is filled later. + * Persists the template virtual size on {@code template_spool_ref}. + * iSCSI also stores the cache LUN uuid in {@code local_download_path}. + * NFS has no LUN; {@code install_path} is filled later by the KVM copy. + * + *
The size is written here, before that copy, because the copy answer reports the + * qcow2's allocated size. {@code TemplateObject} stores that answer only while + * {@code template_size} is still 0.
*/ private String recordTemplateCacheOnSpoolRef(StoragePoolVO storagePool, TemplateInfo templateInfo, CloudStackVolume created, long sizeInBytes) { + VMTemplateStoragePoolVO templatePoolRef = findTemplatePoolRef(storagePool.getId(), templateInfo.getId()); + if (sizeInBytes > 0) { + templatePoolRef.setTemplateSize(sizeInBytes); + } if (created == null || created.getLun() == null) { + vmTemplatePoolDao.update(templatePoolRef.getId(), templatePoolRef); return templateInfo.getUuid(); } Lun lun = created.getLun(); - VMTemplateStoragePoolVO templatePoolRef = findTemplatePoolRef(storagePool.getId(), templateInfo.getId()); templatePoolRef.setLocalDownloadPath(lun.getUuid()); - templatePoolRef.setTemplateSize(sizeInBytes); vmTemplatePoolDao.update(templatePoolRef.getId(), templatePoolRef); return lun.getName(); } @@ -554,9 +562,13 @@ private CloudStackVolume cloneCloudStackVolumeFromTemplate(StoragePoolVO storage templateId, volumeInfo.getId(), storagePool.getId()); long requestedSize = getDataObjectSizeIncludingHypervisorSnapshotReserve(volumeInfo, storagePool); - if (requestedSize > templatePoolRef.getTemplateSize()) { + long templateSize = templatePoolRef.getTemplateSize(); + if (requestedSize > templateSize) { logger.info("cloneCloudStackVolumeFromTemplate: Growing clone of template [{}] from {} to {} bytes for volume [{}]", - templateId, templatePoolRef.getTemplateSize(), requestedSize, volumeInfo.getId()); + templateId, templateSize, requestedSize, volumeInfo.getId()); + if (templateSize > 0) { + cloned.setCurrentSize(templateSize); + } resizeClonedVolumeOrCleanup(storageStrategy, cloned, requestedSize, "cloneCloudStackVolumeFromTemplate template [" + templateId + "] volume [" + volumeInfo.getId() + "]"); } @@ -684,6 +696,7 @@ private CloudStackVolume cloneCloudStackVolumeFromSnapshot(StoragePoolVO storage if (snapshotSize > 0 && requestedSize > snapshotSize) { logger.info("cloneCloudStackVolumeFromSnapshot: Growing clone of snapshot [{}] from {} to {} bytes for volume [{}]", csSnapshotId, snapshotSize, requestedSize, volumeInfo.getId()); + cloned.setCurrentSize(snapshotSize); resizeClonedVolumeOrCleanup(storageStrategy, cloned, requestedSize, "cloneCloudStackVolumeFromSnapshot snapshot [" + csSnapshotId + "] volume [" + volumeInfo.getId() + "]"); } @@ -707,7 +720,7 @@ private CloudStackVolume cloneCloudStackVolumeFromSnapshot(StoragePoolVO storage /** * Grows a just-created ONTAP clone to the requested size. If resize fails, deletes the clone - * so the CloudStack create-failure path does not leave an orphaned LUN/file on the array. + * so the CloudStack create-failure path does not leave an orphaned LUN or file on the array. */ private void resizeClonedVolumeOrCleanup(StorageStrategy storageStrategy, CloudStackVolume cloned, long requestedSize, String context) { diff --git a/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/StorageStrategy.java b/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/StorageStrategy.java index 64a6f1af4e0a..2ebca606a886 100644 --- a/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/StorageStrategy.java +++ b/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/StorageStrategy.java @@ -970,7 +970,11 @@ abstract public CloudStackVolume copyCloudStackVolume(VolumeInfo srcVolumeInfo, * Grows an existing backend object to {@code sizeInBytes}. * *Needed after cloning a cached template, because a clone inherits the size of its source - * while the service offering may ask for a larger disk.
+ * while the disk offering may ask for a larger disk. The volume row already holds that + * offering size, so a clone sets {@link org.apache.cloudstack.storage.service.model.CloudStackVolume#setCurrentSize} + * to the cloned file's virtual size before this call. NFS uses that value as the current + * size. A normal resize leaves it unset and the volume row still holds the current size. + * iSCSI resizes the LUN to {@code sizeInBytes}. * *When the request already carries a QoS policy on the LUN or file, that policy is applied
* as part of this resize. iSCSI sends one LUN update with the new size and the policy.
diff --git a/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/UnifiedNASStrategy.java b/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/UnifiedNASStrategy.java
index 18bc3116259d..8c2e4ed014d0 100644
--- a/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/UnifiedNASStrategy.java
+++ b/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/UnifiedNASStrategy.java
@@ -251,7 +251,7 @@ public void resizeCloudStackVolume(CloudStackVolume cloudstackVolume, long sizeI
}
DataObject volumeInfo = cloudstackVolume.getVolumeInfo();
- Answer answer = resizeVolumeOnKVMHost(volumeInfo, sizeInBytes);
+ Answer answer = resizeVolumeOnKVMHost(cloudstackVolume, sizeInBytes);
if (answer == null || !answer.getResult()) {
String errMsg = answer != null ? answer.getDetails() : "Failed to resize qcow2 on KVM host";
logger.error("resizeCloudStackVolume: " + errMsg);
@@ -263,7 +263,8 @@ public void resizeCloudStackVolume(CloudStackVolume cloudstackVolume, long sizeI
logger.info("resizeCloudStackVolume: Resized volume [{}] to {} bytes", volumeInfo.getUuid(), sizeInBytes);
}
- private Answer resizeVolumeOnKVMHost(DataObject volumeInfo, long sizeInBytes) {
+ private Answer resizeVolumeOnKVMHost(CloudStackVolume cloudstackVolume, long sizeInBytes) {
+ DataObject volumeInfo = cloudstackVolume.getVolumeInfo();
VolumeObject volumeObject = (VolumeObject) volumeInfo;
VolumeVO volume = volumeDao.findById(volumeObject.getId());
if (volume == null) {
@@ -278,11 +279,15 @@ private Answer resizeVolumeOnKVMHost(DataObject volumeInfo, long sizeInBytes) {
// instanceName is set by VolumeApiServiceImpl.orchestrateResizeVolume() before calling the
// driver — it is the VM instance name when attached, or "none" when the volume is detached.
ResizeVolumePayload resizePayload = volumeObject.getpayload() instanceof ResizeVolumePayload
- ? (ResizeVolumePayload) volumeObject.getpayload()
- : null;
- String instanceName = resizePayload != null ? resizePayload.instanceName : "none";
- ResizeVolumeCommand cmd = new ResizeVolumeCommand(volume.getPath(), new StorageFilerTO(storagePool),
- volume.getSize(), sizeInBytes, false, instanceName);
+ ? (ResizeVolumePayload) volumeObject.getpayload()
+ : null;
+ String instanceName = resizePayload != null ? resizePayload.instanceName : "none";
+ // A clone's volume row is already the offering size. Use the cloned file's virtual size
+ // when the driver recorded it, so the agent does not see current == new and skip the grow.
+ Long clonedFileSize = cloudstackVolume.getCurrentSize();
+ long currentSize = clonedFileSize != null && clonedFileSize > 0 ? clonedFileSize : volume.getSize();
+ ResizeVolumeCommand cmd = new ResizeVolumeCommand(volume.getPath(), new StorageFilerTO(storagePool),
+ currentSize, sizeInBytes, false, instanceName);
EndPoint ep = epSelector.select(volumeInfo);
if (ep == null) {
diff --git a/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/model/CloudStackVolume.java b/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/model/CloudStackVolume.java
index ab38e3045f51..930e5db9e72c 100644
--- a/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/model/CloudStackVolume.java
+++ b/plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/service/model/CloudStackVolume.java
@@ -53,6 +53,13 @@ public class CloudStackVolume {
private DataObject volumeInfo; // This is needed as we need DataObject to be passed to agent to create volume
+ /**
+ * Virtual size of a qcow2 that was just cloned, when the volume row already holds the
+ * disk offering size. NFS resize uses this as the current size so the KVM agent grows the
+ * file. Null on a normal resize, where the volume row still holds the current size.
+ */
+ private Long currentSize;
+
public FileInfo getFile() {
return file;
}
@@ -101,4 +108,12 @@ public void setDestinationPath(String destinationPath) {
this.destinationPath = destinationPath;
}
+ public Long getCurrentSize() {
+ return currentSize;
+ }
+
+ public void setCurrentSize(Long currentSize) {
+ this.currentSize = currentSize;
+ }
+
}
diff --git a/plugins/storage/volume/ontap/src/test/java/org/apache/cloudstack/storage/driver/OntapPrimaryDatastoreDriverTest.java b/plugins/storage/volume/ontap/src/test/java/org/apache/cloudstack/storage/driver/OntapPrimaryDatastoreDriverTest.java
index fd297cb71fad..d97f043f0e3c 100644
--- a/plugins/storage/volume/ontap/src/test/java/org/apache/cloudstack/storage/driver/OntapPrimaryDatastoreDriverTest.java
+++ b/plugins/storage/volume/ontap/src/test/java/org/apache/cloudstack/storage/driver/OntapPrimaryDatastoreDriverTest.java
@@ -1465,16 +1465,21 @@ void testCreateAsync_TemplateWithISCSI_UnknownSize_Fails() {
}
@Test
- void testCreateAsync_TemplateWithNFS_IsMetadataOnly() {
+ void testCreateAsync_TemplateWithNFS_RecordsVirtualSize() {
storagePoolDetails.put(OntapStorageConstants.PROTOCOL, ProtocolType.NFS3.name());
when(dataStore.getId()).thenReturn(1L);
when(dataStore.getName()).thenReturn("ontap-pool");
when(templateInfo.getType()).thenReturn(TEMPLATE);
+ when(templateInfo.getId()).thenReturn(50L);
when(templateInfo.getUuid()).thenReturn("template-uuid");
+ when(templateInfo.getSize()).thenReturn(3758096384L);
when(storagePoolDao.findById(1L)).thenReturn(storagePool);
+ when(storagePool.getId()).thenReturn(1L);
when(storagePoolDetailsDao.listDetailsKeyPairs(1L)).thenReturn(storagePoolDetails);
+ when(vmTemplatePoolDao.findByPoolTemplate(1L, 50L, null)).thenReturn(templatePoolRef);
+ when(templatePoolRef.getId()).thenReturn(7L);
try (MockedStatic