Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hostberg/86bfaa81e50cc0666f1745e1897c0a56 to your computer and use it in GitHub Desktop.
Save hostberg/86bfaa81e50cc0666f1745e1897c0a56 to your computer and use it in GitHub Desktop.

Proxmox with LVM-thin and why we should use Trim/Discard

Excerpts from the Proxmox VE Administration Guide]

LVM normally allocates blocks when you create a volume. LVM thin pools instead allocates blocks when they are written. This behaviour is called thin-provisioning, because volumes can be much larger than physically available space.

8.10.2. Trim/Discard It is good practice to run fstrim (discard) regularly on VMs and containers. This releases data blocks that the filesystem isn’t using anymore. It reduces data usage and resource load. Most modern operating systems issue such discard commands to their disks regularly. You only need to ensure that the Virtual Machines enable the disk discard option.

Trim/Discard If your storage supports thin provisioning (see the storage chapter in the Proxmox VE guide), you can activate the Discard option on a drive. With Discard set and a TRIM-enabled guest OS [29], when the VM’s filesystem marks blocks as unused after deleting files, the controller will relay this information to the storage, which will then shrink the disk image accordingly. For the guest to be able to issue TRIM commands, you must enable the Discard option on the drive. Some guest operating systems may also require the SSD Emulation flag to be set. Note that Discard on VirtIO Block drives is only supported on guests using Linux Kernel 5.0 or higher. If you would like a drive to be presented to the guest as a solid-state drive rather than a rotational hard disk, you can set the SSD emulation option on that drive. There is no requirement that the underlying storage actually be backed by SSDs; this feature can be used with physical media of any type. Note that SSD emulation is not supported on VirtIO Block drives.

According to official documentation, we need

  • Enable Discard and SSD Emulation flags for drive
  • Regularly run fstrim inside guest OS (doesn't matter hdd or ssd used on the host machine)

Check vm configuration

pve-01 $# qm config 170 | egrep '^scsi0:'
scsi0: data_hdd_1:vm-170-disk-0,discard=on,size=40G,ssd=1

Options discard and ssd are enabled.

Check if fstrim working

pve-01 $# lvs -a | egrep 'LV|vm-170-disk-0'
  LV                 VG       Attr       LSize   Pool       Origin Data%  Meta%  Move Log Cpy%Sync Convert
  vm-170-disk-0      vg_hdd_1 Vwi-aotz--  40.00g data_hdd_1        14.65
vm-170 $# fstrim --fstab --verbose
/: 34.6 GiB (37143580672 bytes) trimmed on /dev/disk/by-uuid/bcaec9da-3717-4a91-938f-69eadf5baf07
pve-01 $# lvs -a | egrep 'LV|vm-170-disk-0'
  LV                 VG       Attr       LSize   Pool       Origin Data%  Meta%  Move Log Cpy%Sync Convert
  vm-170-disk-0      vg_hdd_1 Vwi-aotz--  40.00g data_hdd_1        8.76

Parameter Origin Data% was changed from 14.65 to 8.76.

Check if fstrim.timer enabled

vm-170 $# systemctl status fstrim.timer
● fstrim.timer - Discard unused blocks once a week
     Loaded: loaded (/lib/systemd/system/fstrim.timer; enabled; vendor preset: enabled)
     Active: active (waiting) since Fri 2021-11-26 13:20:33 CET; 21min ago
    Trigger: Mon 2021-11-29 00:00:00 CET; 2 days left
   Triggers: ● fstrim.service
       Docs: man:fstrim

Nov 26 13:20:33 nfs-01.dfiles.cc systemd[1]: Started Discard unused blocks once a week.
vm-170 $# systemctl list-timers fstrim.timer
NEXT                        LEFT        LAST                        PASSED     UNIT         ACTIVATES
Mon 2021-11-29 00:00:00 CET 2 days left Tue 2021-11-23 15:39:22 CET 2 days ago fstrim.timer fstrim.service

1 timers listed.

Service fstrim.service will be running weekly by fstrim.timer.

@MorningLightMountain713

This is awesome. Thank you!

@nirav-mj
Copy link

Thank you very much!! Great explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment