SSDs have sophisticated wear-leveling algorithms which need to know about free blocks to work correctly [1]. Widespread Linux filesystems (e.g. Ext4, XFS, Btrfs) support to issue the needed commands automatically after removing data by adding the discard mount option but doing so can have a negative effect on performance [2], [3].
To read more about SSD handling in Linux see the help pages of your distribution (e.g. Arch Linux, Debian, Fedora, SUSE Linux).
This script does currently do two things:
- Run
fstrim
on all mounted filesystems that reside on block devices supporting discards.
This does not check if the actual file system supports discard. - Run
blkdiscard
on any free discardable space covered by LVM.
It does so by creating temporary volumes covering 100% of the free VG space and issuing blkdiscard on them.
You need to have fstrim
and blkdiscard
(as well as all other support utilities used such as lvm
and blockdev
) available for execution.
Besides that, the file systems as well as all block layers beneath them (e.g. LVM, dm-crypt) need to be configured to pass the trim commands on to the next layer.
Of course it does only make sense to use discard-everything.sh
if you don't mount your filesystems with the discard
option.
Make sure you have issue_discards = 1
in the devices
section of your lvm.conf
(see [4] or your local manpage: man lvm.conf
).
For devices created by crypttab
add allow-discards
as optional argument [5].
For those created by cryptsetup
use the --allow-discards
parameter (see [6] or your local manpage: man cryptsetup
).
Just copy discard-everything.sh into /etc/cron.weekly/ or a similar place, or execute it manually regularly (as root).