[lxc-devel] [lxd/master] Storage: ZFS Remove snapshot when migrating as main volume

tomponline on Github lxc-bot at linuxcontainers.org
Tue Jun 16 14:02:19 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 550 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200616/270f3a7b/attachment.bin>
-------------- next part --------------
From 9679d8cd094f93be09fed09608b9b87f2f1548b1 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 16 Jun 2020 14:59:49 +0100
Subject: [PATCH] lxd/storage/drivers/driver/zfs/volumes: Remove snapshot when
 migrating as main volume

When migrating a snapshot to a main volume and using optimized transfer method, the original snapshot is also transferred.

We need to delete this, as it is not needed.

Fixes #7532

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/storage/drivers/driver_zfs_volumes.go | 28 +++++++++++++++++++----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/lxd/storage/drivers/driver_zfs_volumes.go b/lxd/storage/drivers/driver_zfs_volumes.go
index 43caa00a55..3741c9f71b 100644
--- a/lxd/storage/drivers/driver_zfs_volumes.go
+++ b/lxd/storage/drivers/driver_zfs_volumes.go
@@ -646,13 +646,31 @@ func (d *zfs) CreateVolumeFromMigration(vol Volume, conn io.ReadWriteCloser, vol
 		return err
 	}
 
-	// Filter only the snapshots.
-	for _, entry := range entries {
-		if strings.HasPrefix(entry, "@snapshot-") {
-			continue
+	// keepDataset returns whether to keep the data set or delete it. Data sets that are non-snapshots or
+	// snapshots that match the requested snapshots in volTargetArgs.Snapshots are kept. Any other snapshot
+	// data sets should be removed.
+	keepDataset := func(dataSetName string) bool {
+		// Keep non-snapshot data sets and snapshots that don't have the LXD snapshot prefix indicator.
+		dataSetSnapshotPrefix := "@snapshot-"
+		if !strings.HasPrefix(dataSetName, "@") || !strings.HasPrefix(dataSetName, dataSetSnapshotPrefix) {
+			return false
+		}
+
+		// Check if snapshot data set matches one of the requested snapshots in volTargetArgs.Snapshots.
+		// If so, then keep it, otherwise request it be removed.
+		entrySnapName := strings.TrimPrefix(dataSetName, dataSetSnapshotPrefix)
+		for _, snapName := range volTargetArgs.Snapshots {
+			if entrySnapName == snapName {
+				return true // Keep snapshot data set if present in the requested snapshots list.
+			}
 		}
 
-		if strings.HasPrefix(entry, "@") {
+		return false // Delete any other snapshot data sets that have been transferred.
+	}
+
+	// Remove any snapshots that were transferred but are not needed.
+	for _, entry := range entries {
+		if !keepDataset(entry) {
 			_, err := shared.RunCommand("zfs", "destroy", fmt.Sprintf("%s%s", d.dataset(vol, false), entry))
 			if err != nil {
 				return err


More information about the lxc-devel mailing list