[lxc-devel] [lxd/master] lxd/storage/zfs: Use autotrim when available

stgraber on Github lxc-bot at linuxcontainers.org
Thu Jul 9 12:59:33 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200709/5eaea9b3/attachment.bin>
-------------- next part --------------
From ddfda244d3c5f6205fb06f30a1f7af0da7d8df45 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 9 Jul 2020 08:44:19 -0400
Subject: [PATCH] lxd/storage/zfs: Use autotrim when available
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 doc/storage.md                    | 15 +++++++++++++++
 lxd/storage/drivers/driver_zfs.go | 22 ++++++++++++++++++++--
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/doc/storage.md b/doc/storage.md
index 5c06473e7b..421656ba84 100644
--- a/doc/storage.md
+++ b/doc/storage.md
@@ -466,3 +466,18 @@ sudo zpool set autoexpand=off lxd
 ```
 
 (NOTE: For users of the snap, use `/var/snap/lxd/common/lxd/ instead of /var/lib/lxd/`)
+
+#### Enabling TRIM on existing pools
+LXD will automatically enable trimming support on all newly created pools on ZFS 0.8 or later.
+
+This helps with the lifetime of SSDs by allowing better block re-use by the controller.
+This also will allow freeing space on the root filesystem when using a loop backed ZFS pool.
+
+For systems which were upgraded from pre-0.8 to 0.8, this can be enabled with a one time action of:
+
+ - zpool upgrade ZPOOL-NAME
+ - zpool set autotrim=on ZPOOL-NAME
+ - zpool trim ZPOOL-NAME
+
+This will make sure that TRIM is automatically issued in the future as
+well as cause TRIM on all currently unused space.
diff --git a/lxd/storage/drivers/driver_zfs.go b/lxd/storage/drivers/driver_zfs.go
index 30e76dc640..606b1efd1b 100644
--- a/lxd/storage/drivers/driver_zfs.go
+++ b/lxd/storage/drivers/driver_zfs.go
@@ -22,6 +22,7 @@ import (
 var zfsVersion string
 var zfsLoaded bool
 var zfsDirectIO bool
+var zfsTrim bool
 
 var zfsDefaultSettings = map[string]string{
 	"mountpoint": "none",
@@ -88,9 +89,10 @@ func (d *zfs) load() error {
 		return err
 	}
 
-	// If v0.8 is older or the same as current version, we can use direct I/O.
-	if ver80.Compare(ourVer) <= 0 {
+	// If running 0.8 or older, we can use direct I/O and trim.
+	if ourVer.Compare(ver80) >= 0 {
 		zfsDirectIO = true
+		zfsTrim = true
 	}
 
 	zfsLoaded = true
@@ -154,6 +156,14 @@ func (d *zfs) Create() error {
 		if err != nil {
 			return err
 		}
+
+		// Apply auto-trim if supported.
+		if zfsTrim {
+			_, err := shared.RunCommand("zpool", "set", "autotrim=on", d.config["zfs.pool_name"])
+			if err != nil {
+				return err
+			}
+		}
 	} else if filepath.IsAbs(d.config["source"]) {
 		// Handle existing block devices.
 		if !shared.IsBlockdevPath(d.config["source"]) {
@@ -179,6 +189,14 @@ func (d *zfs) Create() error {
 			return err
 		}
 
+		// Apply auto-trim if supported.
+		if zfsTrim {
+			_, err := shared.RunCommand("zpool", "set", "autotrim=on", d.config["zfs.pool_name"])
+			if err != nil {
+				return err
+			}
+		}
+
 		// We don't need to keep the original source path around for import.
 		d.config["source"] = d.config["zfs.pool_name"]
 	} else {


More information about the lxc-devel mailing list