Enabling fstrim on various storage layers
/etc/crypttab
sda1_crypt /dev/sda1 none luks,discard
/etc/lvm/lvm.conf
(...)
devices {
(...)
issue_discards = 1
(...)
}
(...)
Fstrim on any filesystem can be enabled by adding discard flag to fstab. Since it will trigger defragmentation and deletion of freed blocks on every remove operation at filesystem level, it’s highly discouraged to do so.
Instead one should run fstrim command periodically. Shedule should be adjusted to a particular use case.
Command to check which devices support fstrim:
#lsblk -o KNAME,DISC-MAX
KNAME DISC-MAX
sda 2G
sda1 2G
sda2 2G
dm-0 2G
dm-1 2G
#cat /sys/block/sda/queue/discard_max_bytes
2147450880
Those with DISC-MAX of non zero value do support fstrim.
To leverage fstrim inside domain we have to setup disk bus to utilise SCSI and SCSI controller to VirtIO SCSI:
<controller type='scsi' index='0' model='virtio-scsi'>
<alias name='scsi0'/>
<address type='pci' domain='0x0000' bus='0x02' slot='0x03' function='0x0'/>
</controller>
<disk type='network' device='disk'>
(..)
<target dev='sda' bus='scsi'/>
(...)
Then we add discard='unmap' option to disk definition:
<driver name='qemu' type='raw' cache='writeback' discard='unmap'/>
Creating LVM thin pool and thin pool volumes
At first, install thin_check tool, eg. in Ubuntu run:
apt install thin-provisioning-tools
Create optimally aligned partition for physical volume using parted:
# parted -a optimal /dev/sdb mkpart pri 0% 200G
# parted /dev/sdb align-check optimal 1
1 aligned
Create LVM physical volume and volume group:
# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
# vgcreate vg1 /dev/sdb1
Volume group "vg1" successfully created
Create thin pool volume for thin volumes allocation:
# lvcreate --type thin-pool --extents 47635 --name vg1-thin-pool vg1
Using default stripesize 64,00 KiB.
Thin pool volume with chunk size 128,00 KiB can address at most 31,62 TiB of data.
Logical volume "vg1-thin-pool" created.
Finally create logical volume withing previously created thin pool:
# lvcreate --type thin --thinpool vg1-thin-pool -V 186,07G --name n200_backup vg1
Using default stripesize 64,00 KiB.
Rounding up size to full physical extent 186,07 GiB
Logical volume "n200_backup" created.