Since v1.4.0, Longhorn supports trimming filesystem inside Longhorn volumes. Trimming will reclaim space wasted by the removed files of the filesystem.
Trying to trim a removed files from a valid snapshot will do nothing but the filesystem will discard this kind of in-memory trimmable file info. Later on if you mark the snapshot as removed and want to retry the trim, you may need to unmount and remount the filesystem so that the filesystem can recollect the trimmable file info.
If you allow automatically removing snapshots during filesystem trim, please be careful of using mount option
discard, which will trigger the snapshot removal frequently then interrupt some operations like backup creation.
There are two ways to do trim for a Longhorn volume: with the Longhorn UI and directly via cmd.
You can directly click volume operation
Trim Filesystem for attached volumes.
Then Longhorn will try its best to figure out the mount point and execute
fstrim <the mount point>. If something is wrong or the filesystem does not exist, the UI will return an error.
Users need to figure out the mount point of the volume then manually execute
fstrim <the mount point>.
By design each valid snapshot of a Longhorn volume is immutable. Hence Longhorn filesystem trim feature can be applied to the volume head and the followed continuous removed or system snapshots only.
To help reclaim as much space as possible automatically, Longhorn introduces setting Remove Snapshots During Filesystem Trim. This allows Longhorn filesystem trim feature to automatically mark the latest snapshot and its ancestors as removed and stops at the snapshot containing multiple children. As a result, Longhorn can reclaim space for as more snapshots as possible.
Of course there is a per-volume field
volume.Spec.UnmapMarkSnapChainAsRemoved would overwrite the global setting mentioned above.
There are 3 options for this volume field:
ignored means following the global setting, which is the default value.
You can directly set this field in the StoragaClasses so that the customized value can be applied to all volumes created by the StorageClasses.
Currently, Longhorn UI only supports filesystem trimming for RWO volume. It will be enhanced for RWX volume at https://github.com/longhorn/longhorn/issues/5143.
If you want to trim a RWX volume manually, you can:
kubectl -n longhorn-system exec -it <the share manager pod> -- bash
mount | grep /dev/longhorn/<volume name> /dev/longhorn/<volume name> on /export/<volume name> type ext4 (rw,relatime)
fstrim /export/<volume name>
By default, TRIM commands are not enabled by the device-mapper. You can check this doc for details.
If you still want to trim an encrypted Longhorn volume, you can:
discardsfor the encrypted volume. The passphrase is recorded in the corresponding secert:
cryptsetup --allow-discards --persistent refresh <Longhorn volume name>
fstrimfor the mount point of
© 2019-2023 Longhorn Authors | Documentation Distributed under CC-BY-4.0
© 2023 The Linux Foundation. All rights reserved. The Linux Foundation has registered trademarks and uses trademarks. For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.