SSD/NVMe Tweaks (TRIM/Discard)
Note, within the examples in the following sections, /dev/sda
refers to a SSD device while /dev/nvme0n1
refers to a NVMe device.
Get Full SSD/NVMe Device Info
For SSD devices use:
smartctl -x /dev/sda # -x, --xall; -a, --all
hdparm -I /dev/sda # doesn't support NVMe
For NVMe devices use:
smartctl -x /dev/nvme0n1 # -x, --xall; -a, --all
nvme smart-log -H /dev/nvme0n1 # apt install nvme-cli
References
- SmartMonTools: NVMe support
- Percona: Using NVMe Command Line Tools to Check NVMe Flash Health
- Unix and Linux: How to evaluate the wear level of a NVMe SSD?
- NVMexpress.org: NVMe Current Specifications
Enable TRIM/Discard in FsTab
TRIM (Trim command let an OS know which SSD blocks are not being used and can be cleared).
sudo nano /etc/fstab
#/dev/disk/by-uuid/09e7c8ed-fb55-4a44-8be4-18b1696fc714 / ext4 defaults 0 0
/dev/disk/by-uuid/09e7c8ed-fb55-4a44-8be4-18b1696fc714 / ext4 discard,async,noatime,nodiratime,errors=remount-ro 0 1
Warning: Users need to be certain that their SSD supports TRIM before attempting to use it. Data loss can occur otherwise! Tp test whether the SSD device supports TRIM/Discard option you can use either of the following commands.
sudo hdparm -I /dev/sda | grep TRIM
* Data Set Management TRIM supported (limit 8 blocks)
sudo smartctl --all /dev/sda | grep TRIM
TRIM Command: Available
To test does an NVMe supports TRIM (physical discard option) the output of the following command must be greater than 0.
References
- Arch Linux Wiki: Solid state drive
- Red Hat Documentation: Red Hat Enterprise Linux > 8 > Managing storage devices > Chapter 8. Discarding unused blocks
- How-To Geek: How to Tweak Your SSD in Ubuntu for Better Performance
- How-To Geek: What Is the Linux fstab File, and How Does It Work?
- itBeginner.net: How to tweak and optimize SSD for Ubuntu, Linux Mint
- Ask Ubuntu: How do I optimize the OS for SSDs?
Set the automatic TRIM job to daily
sudo systemctl enable fstrim.timer
sudo mkdir /etc/systemd/system/fstrim.timer.d
sudo nano /etc/systemd/system/fstrim.timer.d/override.conf
[Timer]
OnCalendar=
OnCalendar=daily
Reboot the system or do daemon-reload and check the updated value.
sudo systemctl daemon-reload
systemctl cat fstrim.timer
# /lib/systemd/system/fstrim.timer
[Unit]
Description=Discard unused blocks once a week
Documentation=man:fstrim
ConditionVirtualization=!container
[Timer]
OnCalendar=weekly
AccuracySec=1h
Persistent=true
[Install]
WantedBy=timers.target
# /etc/systemd/system/fstrim.timer.d/override.conf
[Timer]
OnCalendar=
OnCalendar=daily
Undo the change if you need.
sudo rm -v /etc/systemd/system/fstrim.timer.d/override.conf
References
- Easy Linux Tips Project: SSD – How to optimize your Solid State Drive for Linux Mint and Ubuntu
See also
- Arch Linux Wiki: Improving performance (Need to read carefully!)
- Arch Linux Wiki: Ext4 (Need to read carefully!)
- Website for Students: Improve Nginx Cache Performance with
tmpfs
on Ubuntu