Ask Your Question

Is TRIM command working on LVM or not?

asked 2012-06-18 16:00:54 -0500

morningstar.fallen gravatar image

updated 2012-06-22 04:38:42 -0500

I've installed Fedora 17 for the first time and I absolutely love it. However as I am still quite a newbie, I've simply clicked "Next, next..." during installation. By this I've effectively missed the fact that the installer has created a tiny 500 odd megabytes ext4 partition and a 120 GB LVM PV partition. Now I have everything nicely set up and running absolutely smoothly but I'm not sure if I can fully utilise the TRIM/Discard command on my ssd. And I really don't feel like doing the whole installation again. I have used the command that was shown in Fedora documentation and enabled discard on that tiny ext4 partition. Afterwards I was able to confirm that TRIM is indeed in place using these instructions. Unfortunately I don't know if it covers the whole disk or that small bit only. I did some research and found some hardcore tekkie talk about some sort of command pass through making TRIM possible on LVM but I couldn't really grasp the definitive answer or a how-to. So does it work or not? And if it does, how does one go about enabling it. And btw my disk isn't encrypted so the post about enabling TRIM on LVM/LUKS doesn't really aply to me.


Right, I wonder if this question is too complicated or too dumb so nobody bothers answering it :)

Anyways, I've been digging and trying and I would like to share some findings and observations:

So firstly: I couldn't get my head around what LVM actually is. Sure there is wiki and all but still I haven't been able to tell for sure. So after a lot of questioning I started to understand a bit. Correct me if I'm wrong but LVM is a sort of software RAID layer (something like Windows dynamic disks) that exists between the hardware itself (in this instance my SSD) and the file systems on it (for example ext4). It allows a lot of useful features similar to those of MS origin but disputably might not allow functioning TRIM. So if the installer has created a large LVM organized space as shown here, and then created two ext4 partitions on top of it (first and third line shown here), does then issuing the "discard" command have any effect on the underlying SSD?

Well, I've tried! Using the command example from fedora documentation I've issued the following:

mount -t ext4 -o discard /dev/mapper/vg_angel--squad-lv_root /mnt

mount -t ext4 -o discard /dev/sdc1 /mnt

mount -t ext4 -o discard /dev/mapper/vg_angel--squad-lv_home /mnt

Then used this guide to determine whether the automatic TRIM is enabled and the answer was lotta zeroes which means yes.

However I am still unsure which is true; Am I getting readings from some "virtual" layer or does it describe what is actually happening on the hardware level? Meaning that I cannot say whether the discard command ... (more)

edit retag flag offensive close merge delete


I guess TRIM works through LVM well.

lzap gravatar imagelzap ( 2012-06-22 04:31:33 -0500 )edit

2 Answers

Sort by » oldest newest most voted

answered 2012-06-22 16:55:22 -0500

mooninite gravatar image

You can see if discard is enabled or not with the following command:

$ lsblk -D

Look for the line that has your drive, then look below that to the LVM name. If you see "0B" in both the DISC-GRAN and DISC-MAX columns then discard is not passing through to the LVM partition. If you see something besides 0B then discard is enabled. If you have further questions please post the output of the command.

edit flag offensive delete link more


I dont have 0B there, but 2B. Can you tell me if TRIM is working for me? This is default F19 installation (LVM, LUKS), no changes.

lzap gravatar imagelzap ( 2013-11-13 16:30:39 -0500 )edit

Also, do I need to add "discard" option to my fstab if I don't have it there?

lzap gravatar imagelzap ( 2013-11-13 16:38:50 -0500 )edit

answered 2013-11-21 04:05:51 -0500

lzap gravatar image

I had to write a blog post about SSD:

There were some problems in Fedora 18 with LUKS not propagating TRIM commands, but this was fixed in Fedora 19. On my system, TRIM commands propagate successfully. One just needs to make few changes in the configuration. First of all, we need to check if TRIM propagates for all partitions to the end device:

[lzap@lzapx ~]$ lsblk -D
NAME                                            DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda                                                    0      512B       2G         1
├─sda1                                                 0      512B       2G         1
└─sda2                                                 0      512B       2G         1
  ├─fedora_lzapx-root                                  0      512B       2G         1
  ├─fedora_lzapx-swap                                  0      512B       2G         1
  └─fedora_lzapx-home                                  0      512B       2G         1
    └─luks-aaaaaaaa-6657-44f4-8297-bbbbbbbb1111        0      512B       2G         0

The last column shows if TRIM commands do propagate. We can see all is set, except the encrypted home (the last line). To get full TRIM support on LUKS-encrypted devices, we need to allow TRIM commands. Note that this can decrease encryption strengh. This is the Fedora 19 default crypttab file:

$ cat /etc/crypttab
luks-aaaaaaaa-6657-44f4-8297-bbbbbbbb1111 UUID=aaaaaaaa-6657-44f4-8297-a571e02e5492 none

I added allow-discards option there:

$ cat /etc/crypttab
luks-aaaaaaaa-6657-44f4-8297-bbbbbbbb1111 UUID=aaaaaaaa-6657-44f4-8297-a571e02e5492 none allow-discards

After reboot, LUKS can be checked (for discards flag) with:

[lzap@lzapx ~]$ sudo cryptsetup status luks-aaaaaaaa-6657-44f4-8297-bbbbbbbb1111
/dev/mapper/luks-aaaaaaaa-6657-44f4-8297-bbbbbbbb1111 is active and is in use.
  type:    LUKS1
  cipher:  aes-xts-plain64
  keysize: 512 bits
  device:  /dev/mapper/fedora_lzapx-home
  offset:  4096 sectors
  size:    370683904 sectors
  mode:    read/write
  flags:   discards

Now, to enable TRIM and take advantage of it, there are two options:

Optional LVM configuration

If you modify your LVM logical volumes often (e.g. shrinking, deleting), you want to set issue_discards to 1 in `/etc/lvm/lvm.conf. Then you need to do the next optional step described bellow.

Optional init RAM disk regeneration

If you have root partition encrypted by LUKS (not my case) or if you have your root partition on LVM and you want LVM trimming when shrinking or deleting (see above optional step), initial RAM disk needs to be regenerated using the following command:

dracut -f

You will need to reboot to make this change effective of course.

TRIM when deleting files

It is possible to configure ext4 to send TRIM commands while deleting data. You can do this by adding discard option to partitions in /etc/fstab. Note that this slows down deleting a bit. It depends on the SSD drive, but this can slow down quite significantly on some drives.

Do not put discard option to swap devices as this is not required (and perhaps it will not work either). Swap is SSD friendly by default and propagates TRIM command.

TRIM from cron

This is the preferred option because it can be scheduled daily, weekly or during night if you do not turn off your laptop/server:

cat /etc/cron.weekly/01-fstrim
fstrim /
fstrim /home

chmod +x /etc/cron.weekly/01-fstrim

Try to run the script now, it should not print any error message. If you changed LUKS configuration, you might need restart before doing that. If ... (more)

edit flag offensive delete link more


This answer is nice but unfortunately out of date. A few things have changed since 2013. The crypttab option is called discard now, not allow-discards. Trim using a cron job is not needed anymore. There is a service for it: systemctl enable fstrim.timer and systemctl start fstrim.service.

florian gravatar imageflorian ( 2015-12-09 12:25:43 -0500 )edit

Thanks, well the old flag still works fine here (F24), but I will update my article. Systemd service is not enabled by default, I upgraded so cannot tell for new installations.

lzap gravatar imagelzap ( 2017-02-13 03:30:07 -0500 )edit

I think systemd is the default system and session manager in Fedora since version 15 or 16.

florian gravatar imageflorian ( 2017-02-13 09:46:29 -0500 )edit

Question Tools



Asked: 2012-06-18 16:00:54 -0500

Seen: 16,355 times

Last updated: Nov 21 '13