Discard activation (TRIM) on Linux for SSD

Modern data storage devices such as SSDs need the TRA command of the ATA interface, and for this, the OS based on the Linux kernel provides two control methods at the file system level:


  • discard - installed as an option to mount the file system. Allows the Linux kernel to immediately send the TRIM command to the device as soon as the file system reports it.
  • fstrim - a utility that is launched manually or on schedule as an OS service, sends a list of deleted blocks from the FS to clean them on the device.

To enable fstrim, it is enough to activate the service fstrim.servicein systemd, but instead of a service that will hang in memory, it is better to use a timer fstrim.timerthat will start the weekly TRIM.


Service activation example:


# ,     :
systemctl enable fstrim.service && \
systemctl start fstrim.service && \
systemctl status fstrim.service

But these measures are not enough if your file systems reside on LVM volumes and LVM in LUKS , , :




The first thing to do is verify that the SATA controller is in AHCI mode and not IDE, otherwise TRIM will not work:


sudo hdparm -I /dev/sda | grep TRIM
    *    Data Set Management TRIM supported (limit 8 blocks)
    *    Deterministic read ZEROs after TRIM

TRIM supported, SATA AHCI BIOS UEFI.


, discard :


  • ( )
  • /etc/fstab
  • cryptsetup — /etc/crypttab
  • LVM — /etc/lvm/lvm.conf
  • /boot/grub/grub.cfg

. Arch Linux , Linux.


discard EXT4


/etc/fstab discard defaults, . EXT4. , SATA - /etc/fstab.


discard EXT4. :


sudo tune2fs -o discard /dev/mapper/vg1-lvroot
sudo tune2fs -o discard /dev/mapper/vg1-lvhome
sudo tune2fs -o discard /dev/mapper/vg1-lvvar

tune2fs. /dev/mapper/vg1-lvroot , EXT4 LVM:


sudo tune2fs -l /dev/mapper/vg1-lvroot | grep options

discard fstab


SSD , discard /etc/fstab , EXT4, .


, discard swap :


# /dev/mapper/vg1-lvroot
UUID=e86ab458-341d-4f59-8344-0271d2c363e8    /            ext4    rw,noatime,discard   0 0
# /dev/mapper/vg1-lvvar
UUID=44b31816-1193-4dc1-9f58-f70df2250e1a    /var         ext4    rw,noatime,discard   0 0
# /dev/mapper/vg1-lvhome
UUID=372bc9ae-b581-49a4-abed-ca9f3b67edb6    /home        ext4    rw,noatime,discard   0 0
# /dev/sda1
UUID=0BE5-60FB                               /boot/efi    vfat    rw,relatime,discard,...,errors=remount-ro   0 0
# /dev/mapper/vg1-lvswap
UUID=cf67ae1e-3a17-4e5e-ac58-ef23725d2359    none         swap    defaults,discard,pri=-2   0 0

discard LVM


/etc/lvm/lvm.conf issue_discards 1:


devices {
  issue_discards = 1
}

, TRIM , TRIM , , lvremove, lvreduce ..


discard root-


, discard , ( , . .) , crypttab:


WARNING: This command can have a negative security impact because it can make filesystem-level operations visible on the physical device. For example, information leaking filesystem type, used space, etc. may be extractable from the physical device if the discarded blocks can be located later. If in doubt, do not use it.

, . , , , , , ? , ? ? :)




, "" LUKS. TRIM , cryptsetup' --allow-discards /etc/crypttab , , /etc/crypttab, root- .


initramfs, initramfs grub Linux.


allow-discards /etc/default/grub cryptdevice GRUB_CMDLINE_LINUX.


:


GRUB_CMDLINE_LINUX="cryptdevice=UUID=3c121aac-ead9-4d57-88be-c1199acf72f0:cryptlvm"

:


GRUB_CMDLINE_LINUX="cryptdevice=UUID=3c121aac-ead9-4d57-88be-c1199acf72f0:cryptlvm:allow-discards"

"" grub':


sudo grub-mkconfig -o /boot/grub/grub.cfg

, initramfs encrypt cryptsetup lvm2:


cat /etc/mkinitcpio.conf | grep ^HOOKS

HOOKS=(base udev autodetect keyboard keymap consolefont modconf block encrypt lvm2 resume filesystems)

grub .


discard




, /home LUKS. , discard /etc/crypttab /etc/fstab.


: man crypttab


TRIM


:


lsblk --discard

NAME             DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda                     0      512B       2G         0
├─sda1                  0      512B       2G         0
└─sda2                  0      512B       2G         0
  └─cryptlvm            0        0B       0B         0
      ├─vg1-lvroot      0        0B       0B         0
      ├─vg1-lvvar       0        0B       0B         0
      ├─vg1-lvswap      0        0B       0B         0
      └─vg1-lvhome      0        0B       0B         0

DISC-GRAN (discard granularity) DISC-MAX (discard max bytes), TRIM .


TRIM:


sudo fstrim -v /

/: 7,4 GiB (7906193408 bytes) trimmed

, TRIM . TRIM :


lsblk --discard

NAME             DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda                     0      512B       2G         0
├─sda1                  0      512B       2G         0
└─sda2                  0      512B       2G         0
  └─cryptlvm            0      512B       2G         0
    ├─vg1-lvroot        0      512B       2G         0
    ├─vg1-lvvar         0      512B       2G         0
    ├─vg1-lvswap        0      512B       2G         0
    └─vg1-lvhome        0      512B       2G         0

DISC-GRAN 512B SSD 512 bytes. TRIM , . :


sudo cryptsetup status cryptlvm

/dev/mapper/cryptlvm is active and is in use.
type:    LUKS1
cipher:  aes-xts-plain64
keysize: 512 bits
key location: dm-crypt
device:  /dev/sda2
sector size:  512
offset:  4096 sectors
size:    487806976 sectors
mode:    read/write

sudo hdparm -I /dev/sda | grep -i "sector size"

      Logical  Sector size:                   512 bytes
      Physical Sector size:                   512 bytes

sudo smartctl -a /dev/sda | grep -i "sector size"

Sector Size:      512 bytes logical/physical

!




UPDATE 14.04.2020 14:20: . AAngstrom .
UPDATE 23.04.2020 22:00: , "" , . vitaliy2


All Articles