how to fstrim on lvm?
- User
how to fstrim on lvm?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
how to fstrim on lvm?
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
fstrim on LVM works pretty much like fstrim on any other block device, but there's a crucial configuration step within LVM itself to ensure that discard (TRIM) commands are passed down to the underlying physical volumes (PVs).
There are two main ways to use fstrim: periodic/manual and continuous (online). For most systems, periodic fstrim is recommended due to potential performance impacts of continuous TRIM.
Here's how to set it up:
lvm.conf)Before anything else, you need to ensure LVM is configured to pass discard commands. By default, some distributions might have this disabled or commented out.
Edit lvm.conf:
Open the LVM configuration file, typically /etc/lvm/lvm.conf, with root privileges:
sudo nano /etc/lvm/lvm.conf
Enable issue_discards:
Search for the line issue_discards within the devices section.
Change issue_discards = 0 to issue_discards = 1 (or uncomment it if it's commented out):
# In the 'devices' section:
devices {
# ... other settings ...
issue_discards = 1
# ... other settings ...
}
Save and Exit: Save the file (Ctrl+O, Enter) and exit nano (Ctrl+X).
Apply Changes (Important!): For this change to take effect, you need to either:
vgchange -ay for each of your volume groups. This re-activates the logical volumes with the new discard settings. For example, if your VG is named vg0, run sudo vgchange -ay vg0. If you have multiple, you might just run sudo vgchange -ay to reactivate all of them.Note: Without this step, even if issue_discards is set to 1, LVM might not pass the commands until the LVs are reactivated.
This is the most common and generally recommended method. It uses fstrim to periodically send discard commands for all unused blocks.
How it works:
fstrim.timer) runs fstrim -a (trim all mounted filesystems that support TRIM) typically once a week by default.fstrim command works on the filesystem level. The LVM layer, now configured with issue_discards = 1, will pass these commands down to the underlying physical SSDs/NVMe drives.Steps:
Check fstrim.timer status:
systemctl status fstrim.timer
On most modern Linux distributions (like Ubuntu, Fedora, Debian, CentOS 8+), this timer is already present and enabled by default.
Enable and Start (if not already): If it's not enabled, enable and start it:
sudo systemctl enable fstrim.timer
sudo systemctl start fstrim.timer
Manually Run (for immediate testing/trim):
You can run fstrim manually at any time. It's often good to do this after enabling issue_discards and rebooting/reactivating LVs.
sudo fstrim -av
-a flag means "all mounted filesystems". The -v flag means "verbose" (shows how much data was trimmed).sudo fstrim -v /
This method configures the filesystem to send discard commands immediately when blocks are freed.
Why it's less recommended:
Steps:
Edit /etc/fstab:
Open your /etc/fstab file:
sudo nano /etc/fstab
Add discard option:
For each filesystem on an LVM logical volume that you want to continuously TRIM, add the discard option to its mount options.
Example: Before:
/dev/mapper/vg0-rootvol / ext4 defaults 0 1
/dev/mapper/vg0-homevol /home ext4 defaults 0 2
After:
/dev/mapper/vg0-rootvol / ext4 defaults,discard 0 1
/dev/mapper/vg0-homevol /home ext4 defaults,discard 0 2
(Note: defaults implies relatime which is good for SSDs, no need to change that usually).
Save and Exit: Save the file and exit.
Apply Changes: You need to remount the filesystems for the changes to take effect, or simply reboot.
sudo mount -a
After making changes, you want to ensure TRIM commands are actually being passed through.
Check fstrim -v output:
Run sudo fstrim -av and observe the output. If it shows bytes trimmed, it's a good sign.
Check lsblk for discard capabilities:
This won't tell you if TRIM is happening, but if the underlying drive supports it.
lsblk --discard
Look at the DISC-MAX and DISC-GRAN columns. They should show non-zero values (e.g., 0B is bad, 4G or 4K is good). This indicates the block device itself supports discard.
Check dmsetup for discard support (Advanced):
For logical volumes, you can inspect the device mapper table to see if it supports "zero" or "discard" operations.
sudo dmsetup table <your_logical_volume_name> --target trim
Replace <your_logical_volume_name> with the full path, e.g., vg0-rootvol.
If the output includes trim or zero entries with valid ranges, it indicates that the device mapper target (your LV) is configured to pass discards.
0 10485760 linear 253:0 2048 0 10485760 trim 253:0 2048
trim 253:0 2048.Summary and Recommendation:
issue_discards = 1 in /etc/lvm/lvm.conf and reboot or reactivate LVs. This is non-negotiable for TRIM to work through LVM.fstrim.timer for periodic TRIM. This is the most balanced and recommended approach for performance and SSD longevity.discard in /etc/fstab unless you have a specific, tested reason for continuous TRIM and are aware of potential performance implications.By following these steps, you'll ensure that your SSDs underlying LVM are properly trimmed, maintaining their performance and lifespan.