# No Fstrim

## BWStevens

Hi community,

I've got a 250gig Kingston SSD. I don't enable discard in my fstab as I like to just do a fstrim -v /

but I'm getting the discard option is not supported.

Am I missing something in my install? I've checked in /sbin and fstrim is there I just can't use it.

It's always worked before on Arch but I can't get it to work on Gentoo.

Thanks

Respectfully

Brett Stevens

----------

## frostschutz

Which filesystem are you using?

Is it LUKS encrypted? With LUKS you need to explicitely --allow-discards.

----------

## BWStevens

Hi there I'm just using ext4. I've got 2 partitions /dev/sda1 is 128M /boot with ext2

and /dev/sda2 is / with ext4.

I don't run a swap file as I have 32 gigs of ram..

----------

## Roman_Gruber

check those fstab options (i use that with ssd)

or you can run trim by hand

----------

## BWStevens

Hi thanks for the reply.

My options for my root drive is noatime thats all I've got.

I tried adding defaults and discard but my computer wouldn't boot into the desktop and it mounted my drive as read only.

I've never had fstrim not work before.

----------

## frostschutz

Strange... it's not an USB disk, is it?

hdparm -I /dev/sda?

Example: 

```

      *   Data Set Management TRIM supported (limit 8 blocks)

      *   Deterministic read data after TRIM

```

If it works on Arch or even a Live CD on the same machine, it would be a misconfiguration in the kernel although I'm not sure which kernel option would be responsible...

----------

## BWStevens

Hi 

No its not a usb drive. I did hdparm -I /dev/sda and this was in the list.

Data Set Management TRIM supported (limit 1 block)

So I know it supports trim. I just can't get it to work.

I might check the kernel and see if I've missed something. Other than that everything is working fine.

Thanks..

----------

## frostschutz

Does blkdiscard work? Be careful, it's a very dangerous command that trims an entire device.

You could test with your boot partition. Create a backup image, blkdiscard, see if it's gone, restore the backup image.

Example:

```

umount /boot

dd bs=1M if=/dev/sda1 of=/boot.img

blkdiscard /dev/sda1

echo 3 > /proc/sys/vm/drop_caches

hexdump -C /dev/sda1 | head

dd bs=1M if=/boot.img of=/dev/sda1

mount /boot

```

Just to find out whether it already fails at the hardware level or, if it's just the filesystem itself refusing to trim for some reason.

Expected output for the hexdump in case trim succeeded and device returns zero after trim:

```

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

*

08000000

```

----------

## BWStevens

Here is my hexdump mate.

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

*

02000400  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

*

02000420  03 00 00 00 00 00 ff 01  00 00 00 00 00 00 00 00  |................|

02000430  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

*

02000460  fc ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

02000470  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

*

----------

## frostschutz

While that doesn't look like it's been completely zeroed, it does not look like a filesystem either.

So I take it the blkdiscard command did not yield an error message?

Strange.

Maybe it's something silly like / being mounted read-only? I'm really running out of ideas here. If you ever find out what caused this problem, let us know...

----------

## Petross404

 *BWStevens wrote:*   

> Hi community,
> 
> I've got a 250gig Kingston SSD. I don't enable discard in my fstab as I like to just do a fstrim -v /
> 
> but I'm getting the discard option is not supported.
> ...

 

I get that this thread is old, but someone might find it usefull in the future. I used to get errors when I tried to fstrim my SSD but this article saved my day.

 *Quote:*   

> How to active TRIM on Linux? The first thing to know is that TRIM should be enabled on all I/O abstraction layers. This means that if you have an ext4 partition on top of LVM, which in turn is on top of an encrypted volume with LUKS/dm-crypt, then you must enable support for TRIM in these three layers: The filesystem, LVM and dm-crypt. There is no point in enabling it at the filesystem level if you don’t enable it also on the other layers. The TRIM command should be translated from one layer to another until reaching the SSD.

 

1. Enabling TRIM support on dm-crypt

Simply add the option discard inside our crypttab:

```

#/etc/crypttab

# <target name>    <source device>    <key file>       <options>

sda2_crypt         /dev/sda2          none             luks,discard

```

In case we are talking about the rootfs, then add this and update grub configuration before reboot: 

```

#/etc/default/grub

GRUB_CMDLINE_LINUX_DEFAULT="root_trim=yes"
```

2.So far so good, we have enabled trim support for the LUKS container. Let's move on to the LVM:

We have to enable the option issue_discards in the LVM configuration.

```

# /etc/lvm/lvm.conf

# [...]

devices {

   # [...]

   issue_discards = 1

   # [...]

}

# [...]
```

3. Finally, the most the most interesting part is the filesystem. Most people simply add the option “discard” in the mounting options at /etc/fstab. However, this means that every time you delete a file, the OS will be reporting in real-time to the SSD which blocks were occupied by that file and are not longer in use, and then the SSD will have to perform a defragmentation and deletion of those internal blocks, operation which will take an amount of time higher than desired.

Instead of putting an unnecessary extra amount of work over the SSD, one can run periodically a script with fstrim.

Other advantages of the fstrim way are:

*    If you didn’t enabled correctly the TRIM support in the above layers of your setup, you will receive an error when executing fstrim. On the other hand, if you were using the discard option at fstab you wouldn’t have received any error and you would end thinking that you managed to get TRIM working properly when you didn’t.

*    If you delete a file by mistake (you know it happens), you can recover it before anacron runs your script fstrim. On the other hand, if you were using the discard-at-fstab option you wouldn’t have any chance of recovering the file, because the OS would have told the SSD to TRIM that blocks as soon as you deleted the file, and consequently the SSD has irreversibly destroyed such blocks.

Here you have simple script to run fstrim on the /, /boot and /home partitions, which can be programmed to be executed periodically by anacron : 

```

#/etc/cron.weekly/dofstrim

#! /bin/sh

for mount in / /boot /home; do

   fstrim $mount

done
```

Hope this will help someone.

http://blog.neutrino.es/2013/howto-properly-activate-trim-for-your-ssd-on-linux-fstrim-lvm-and-dmcrypt/

----------

