-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix: Convert volume to another directory instead of copying it while taking volume snapshots on KVM #8041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Convert volume to another directory instead of copying it while taking volume snapshots on KVM #8041
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1715,11 +1715,11 @@ public Answer createSnapshot(final CreateObjectCommand cmd) { | |
| snapshotPath = getSnapshotPathInPrimaryStorage(primaryPool.getLocalPath(), snapshotName); | ||
|
|
||
| String diskLabel = takeVolumeSnapshot(resource.getDisks(conn, vmName), snapshotName, diskPath, vm); | ||
| String copyResult = copySnapshotToPrimaryStorageDir(primaryPool, diskPath, snapshotPath, volume); | ||
| String convertResult = convertBaseFileToSnapshotFileInPrimaryStorageDir(primaryPool, diskPath, snapshotPath, volume, cmd.getWait()); | ||
|
|
||
| mergeSnapshotIntoBaseFile(vm, diskLabel, diskPath, snapshotName, volume, conn); | ||
|
|
||
| validateCopyResult(copyResult, snapshotPath); | ||
| validateConvertResult(convertResult, snapshotPath); | ||
| } catch (LibvirtException e) { | ||
| if (!e.getMessage().contains(LIBVIRT_OPERATION_NOT_SUPPORTED_MESSAGE)) { | ||
| throw e; | ||
|
|
@@ -1784,8 +1784,8 @@ public Answer createSnapshot(final CreateObjectCommand cmd) { | |
| } | ||
| } else { | ||
| snapshotPath = getSnapshotPathInPrimaryStorage(primaryPool.getLocalPath(), snapshotName); | ||
| String copyResult = copySnapshotToPrimaryStorageDir(primaryPool, diskPath, snapshotPath, volume); | ||
| validateCopyResult(copyResult, snapshotPath); | ||
| String convertResult = convertBaseFileToSnapshotFileInPrimaryStorageDir(primaryPool, diskPath, snapshotPath, volume, cmd.getWait()); | ||
| validateConvertResult(convertResult, snapshotPath); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1838,13 +1838,13 @@ protected void takeFullVmSnapshotForBinariesThatDoesNotSupportLiveDiskSnapshot(D | |
| s_logger.debug(String.format("Full VM Snapshot [%s] of VM [%s] took [%s] seconds to finish.", snapshotName, vmName, (System.currentTimeMillis() - start)/1000)); | ||
| } | ||
|
|
||
| protected void validateCopyResult(String copyResult, String snapshotPath) throws CloudRuntimeException, IOException { | ||
| if (copyResult == null) { | ||
| protected void validateConvertResult(String convertResult, String snapshotPath) throws CloudRuntimeException, IOException { | ||
| if (convertResult == null) { | ||
| return; | ||
| } | ||
|
|
||
| Files.deleteIfExists(Paths.get(snapshotPath)); | ||
| throw new CloudRuntimeException(copyResult); | ||
| throw new CloudRuntimeException(convertResult); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1901,20 +1901,31 @@ protected void manuallyDeleteUnusedSnapshotFile(boolean isLibvirtSupportingFlagD | |
| } | ||
|
|
||
| /** | ||
| * Creates the snapshot directory in the primary storage, if it does not exist; then copies the base file (VM's old writing file) to the snapshot dir.. | ||
| * Creates the snapshot directory in the primary storage, if it does not exist; then, converts the base file (VM's old writing file) to the snapshot directory. | ||
| * @param primaryPool Storage to create folder, if not exists; | ||
| * @param baseFile Base file of VM, which will be copied; | ||
| * @param snapshotPath Path to copy the base file; | ||
| * @return null if copies successfully or a error message. | ||
| * @param baseFile Base file of VM, which will be converted; | ||
| * @param snapshotPath Path to convert the base file; | ||
| * @return null if the conversion occurs successfully or an error message that must be handled. | ||
| */ | ||
| protected String copySnapshotToPrimaryStorageDir(KVMStoragePool primaryPool, String baseFile, String snapshotPath, VolumeObjectTO volume) { | ||
| protected String convertBaseFileToSnapshotFileInPrimaryStorageDir(KVMStoragePool primaryPool, String baseFile, String snapshotPath, VolumeObjectTO volume, int wait) { | ||
| try { | ||
| s_logger.debug(String.format("Trying to convert volume [%s] (%s) to snapshot [%s].", volume, baseFile, snapshotPath)); | ||
|
|
||
| primaryPool.createFolder(TemplateConstants.DEFAULT_SNAPSHOT_ROOT_DIR); | ||
| Files.copy(Paths.get(baseFile), Paths.get(snapshotPath)); | ||
| s_logger.debug(String.format("Copied %s snapshot from [%s] to [%s].", volume, baseFile, snapshotPath)); | ||
|
|
||
| QemuImgFile srcFile = new QemuImgFile(baseFile); | ||
| srcFile.setFormat(PhysicalDiskFormat.QCOW2); | ||
|
|
||
| QemuImgFile destFile = new QemuImgFile(snapshotPath); | ||
| destFile.setFormat(PhysicalDiskFormat.QCOW2); | ||
|
|
||
| QemuImg q = new QemuImg(wait); | ||
| q.convert(srcFile, destFile); | ||
|
|
||
| s_logger.debug(String.format("Converted volume [%s] (from path \"%s\") to snapshot [%s].", volume, baseFile, snapshotPath)); | ||
| return null; | ||
| } catch (IOException ex) { | ||
| return String.format("Unable to copy %s snapshot [%s] to [%s] due to [%s].", volume, baseFile, snapshotPath, ex.getMessage()); | ||
| } catch (QemuImgException | LibvirtException ex) { | ||
| return String.format("Failed to convert %s snapshot of volume [%s] to [%s] due to [%s].", volume, baseFile, snapshotPath, ex.getMessage()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wouldn't it be convenient to throw the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @DaanHoogland I will check if we can refactor this part.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @DaanHoogland |
||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.