Filesystem discard
Now with the prevalence of SSD's for storage, it is important to make sure that the DISCARD opeartion is used. This is specially true as this can increase the lifetime of your flash storage by reducing the need to re-map blocks by simply marking them as freed.
In Linux, because the flexibility of the block device infrastructure it is very easy to layer different block device drivers to add functionality. The risk of doing that is that Discard capabilities can be left out accidentally.
To examine if Discard is enabled accross the different layers, you can use the command:
lsblk --discard
This will output something like:
NAME DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
loop0 0 4K 4G 0
sda 0 4K 2G 0
└─crypt-pool 0 4K 2G 0
└─pool-imglib 0 4K 2G 0
sdb 0 0B 0B 0
├─sdb1 0 0B 0B 0
└─sdb2 0 0B 0B 0
Make sure that the values under column DISC-GRAN
and DISC-MAX
are non-zero.
Once you have identified that discard is properly configured you can start discarding using these commands:
- Discard on demand, by simply running:
fstrim --all
- Add the
discard
option to the mount command. This is supported by several filesystems and will call discard as files are deleted. - Run
fstrim --all
on a cron job either weekly or monthly.
In addition, if you are going to delete a logical volume, you could do:
blkdiscard DEVICE
on the logical volume before running lvremove
to discard the storage
associated to the logical volume.
As I mentioned earlier, it is important to make sure that all layers have discard enabled.
- Low level disc drivers. These should detect that the underlying drive is capable
of using
discard
automatically. - mdadm : supported and enabled by default. (1)
- dmcrypt - supported, defaults to off for security, requires explicity enabling. (2)
- lvm2 : suppored and enabled by default. (3)
- Filesystems:
- ext3/ext4: supports discard mount option and fstrim
- btrfs: supports discard mount option and fstrim
- f2fs: supports discard mount option and fstrim
- xfs: supports discard mount option and fstrim
- jfs: supports discard mount option and fstrim
- ntfs: supports discard mount option with the ntfs3 driver and fstrim
- vfat: supports discard mount option and fstrim
- Xen VMs using block devices will automatically detect discard and enable the interface for virtual drives.