The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/7635

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
Signed-off-by: Stéphane Graber <stgra...@ubuntu.com>
From ddfda244d3c5f6205fb06f30a1f7af0da7d8df45 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgra...@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 <stgra...@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 {
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to