Skip to content

Commit caeb499

Browse files
Fix dest disk format while migrating volume from ceph/rbd to nfs, and some code improvements
1 parent 89813ff commit caeb499

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -364,16 +364,7 @@ public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
364364
final TemplateObjectTO newTemplate = new TemplateObjectTO();
365365
newTemplate.setPath(primaryVol.getName());
366366
newTemplate.setSize(primaryVol.getSize());
367-
368-
if(List.of(
369-
StoragePoolType.RBD,
370-
StoragePoolType.PowerFlex,
371-
StoragePoolType.Linstor,
372-
StoragePoolType.FiberChannel).contains(primaryPool.getType())) {
373-
newTemplate.setFormat(ImageFormat.RAW);
374-
} else {
375-
newTemplate.setFormat(ImageFormat.QCOW2);
376-
}
367+
newTemplate.setFormat(getFormat(primaryPool.getType()));
377368
data = newTemplate;
378369
} else if (destData.getObjectType() == DataObjectType.VOLUME) {
379370
final VolumeObjectTO volumeObjectTO = new VolumeObjectTO();
@@ -2990,7 +2981,7 @@ public Answer copyVolumeFromPrimaryToPrimary(CopyCommand cmd) {
29902981
final VolumeObjectTO srcVol = (VolumeObjectTO)srcData;
29912982
final VolumeObjectTO destVol = (VolumeObjectTO)destData;
29922983
final ImageFormat srcFormat = srcVol.getFormat();
2993-
final ImageFormat destFormat = destVol.getFormat();
2984+
ImageFormat destFormat = destVol.getFormat();
29942985
final DataStoreTO srcStore = srcData.getDataStore();
29952986
final DataStoreTO destStore = destData.getDataStore();
29962987
final PrimaryDataStoreTO srcPrimaryStore = (PrimaryDataStoreTO)srcStore;
@@ -3025,14 +3016,17 @@ public Answer copyVolumeFromPrimaryToPrimary(CopyCommand cmd) {
30253016
volume.setFormat(PhysicalDiskFormat.valueOf(srcFormat.toString()));
30263017
volume.setDispName(srcVol.getName());
30273018
volume.setVmName(srcVol.getVmName());
3028-
3029-
String destVolumeName = null;
3019+
KVMPhysicalDisk newVolume;
3020+
String destVolumeName;
3021+
destPool = storagePoolMgr.getStoragePool(destPrimaryStore.getPoolType(), destPrimaryStore.getUuid());
30303022
if (destPrimaryStore.isManaged()) {
30313023
if (!storagePoolMgr.connectPhysicalDisk(destPrimaryStore.getPoolType(), destPrimaryStore.getUuid(), destVolumePath, destPrimaryStore.getDetails())) {
30323024
logger.warn("Failed to connect dest volume {}, in storage pool {}", destVol, destPrimaryStore);
30333025
}
30343026
destVolumeName = derivePath(destPrimaryStore, destData, destPrimaryStore.getDetails());
30353027
} else {
3028+
PhysicalDiskFormat destPoolDefaultFormat = destPool.getDefaultFormat();
3029+
destFormat = getFormat(destPoolDefaultFormat);
30363030
final String volumeName = UUID.randomUUID().toString();
30373031
destVolumeName = volumeName + "." + destFormat.getFileExtension();
30383032

@@ -3042,16 +3036,15 @@ public Answer copyVolumeFromPrimaryToPrimary(CopyCommand cmd) {
30423036
}
30433037
}
30443038

3045-
destPool = storagePoolMgr.getStoragePool(destPrimaryStore.getPoolType(), destPrimaryStore.getUuid());
30463039
try {
30473040
Volume.Type volumeType = srcVol.getVolumeType();
30483041

30493042
resource.createOrUpdateLogFileForCommand(cmd, Command.State.PROCESSING_IN_BACKEND);
30503043
if (srcVol.getPassphrase() != null && (Volume.Type.ROOT.equals(volumeType) || Volume.Type.DATADISK.equals(volumeType))) {
30513044
volume.setQemuEncryptFormat(QemuObject.EncryptFormat.LUKS);
3052-
storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, destPool, cmd.getWaitInMillSeconds(), srcVol.getPassphrase(), destVol.getPassphrase(), srcVol.getProvisioningType());
3045+
newVolume = storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, destPool, cmd.getWaitInMillSeconds(), srcVol.getPassphrase(), destVol.getPassphrase(), srcVol.getProvisioningType());
30533046
} else {
3054-
storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, destPool, cmd.getWaitInMillSeconds());
3047+
newVolume = storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, destPool, cmd.getWaitInMillSeconds());
30553048
}
30563049
resource.createOrUpdateLogFileForCommand(cmd, Command.State.COMPLETED);
30573050
} catch (Exception e) { // Any exceptions while copying the disk, should send failed answer with the error message
@@ -3076,7 +3069,8 @@ public Answer copyVolumeFromPrimaryToPrimary(CopyCommand cmd) {
30763069
path = destVolumePath + File.separator + destVolumeName;
30773070
}
30783071
newVol.setPath(path);
3079-
newVol.setFormat(destFormat);
3072+
ImageFormat newVolumeFormat = getFormat(newVolume.getFormat());
3073+
newVol.setFormat(newVolumeFormat);
30803074
newVol.setEncryptFormat(destVol.getEncryptFormat());
30813075
return new CopyCmdAnswer(newVol);
30823076
} catch (final CloudRuntimeException e) {
@@ -3088,6 +3082,26 @@ public Answer copyVolumeFromPrimaryToPrimary(CopyCommand cmd) {
30883082
}
30893083
}
30903084

3085+
private Storage.ImageFormat getFormat(PhysicalDiskFormat format) {
3086+
if (format == null) {
3087+
return null;
3088+
}
3089+
3090+
return ImageFormat.valueOf(format.toString().toUpperCase());
3091+
}
3092+
3093+
private Storage.ImageFormat getFormat(StoragePoolType poolType) {
3094+
if(List.of(
3095+
StoragePoolType.RBD,
3096+
StoragePoolType.PowerFlex,
3097+
StoragePoolType.Linstor,
3098+
StoragePoolType.FiberChannel).contains(poolType)) {
3099+
return ImageFormat.RAW;
3100+
} else {
3101+
return ImageFormat.QCOW2;
3102+
}
3103+
}
3104+
30913105
/**
30923106
* True if location exists
30933107
*/

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ private KVMPhysicalDisk createPhysicalDiskByQemuImg(String name, KVMStoragePool
11161116

11171117
// make room for encryption header on raw format, use LUKS
11181118
if (format == PhysicalDiskFormat.RAW) {
1119-
destFile.setSize(destFile.getSize() - (16<<20));
1119+
destFile.setSize(destFile.getSize() - (16 << 20));
11201120
destFile.setFormat(PhysicalDiskFormat.LUKS);
11211121
}
11221122

@@ -1593,7 +1593,7 @@ to support snapshots(backuped) as qcow2 files. */
15931593
String sourcePath = disk.getPath();
15941594

15951595
KVMPhysicalDisk newDisk;
1596-
logger.debug("copyPhysicalDisk: disk size:" + toHumanReadableSize(disk.getSize()) + ", virtualsize:" + toHumanReadableSize(disk.getVirtualSize())+" format:"+disk.getFormat());
1596+
logger.debug("copyPhysicalDisk: disk size:{}, virtualsize:{} format:{}", toHumanReadableSize(disk.getSize()), toHumanReadableSize(disk.getVirtualSize()), disk.getFormat());
15971597
if (destPool.getType() != StoragePoolType.RBD) {
15981598
if (disk.getFormat() == PhysicalDiskFormat.TAR) {
15991599
newDisk = destPool.createPhysicalDisk(name, PhysicalDiskFormat.DIR, Storage.ProvisioningType.THIN, disk.getVirtualSize(), null);

0 commit comments

Comments
 (0)