Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hacks2learn/a9db45e4d38935262006e4d946d84f39 to your computer and use it in GitHub Desktop.
Save hacks2learn/a9db45e4d38935262006e4d946d84f39 to your computer and use it in GitHub Desktop.

Revisions

  1. @hostberg hostberg revised this gist Nov 26, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Proxmox with LVM-thin and why we should use Trim-Discard.MD
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ scsi0: data_hdd_1:vm-170-disk-0,discard=on,size=40G,ssd=1

    Options `discard` and `ssd` are enabled.

    ## Check fstrim working
    ## Check if fstrim working
    ```bash
    pve-01 $# lvs -a | egrep 'LV|vm-170-disk-0'
    LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
    @@ -44,7 +44,7 @@ pve-01 $# lvs -a | egrep 'LV|vm-170-disk-0'

    Parameter `Origin Data%` was changed from `14.65` to `8.76`.

    ## Check fstrim.timer enabled
    ## Check if fstrim.timer enabled
    ```bash
    vm-170 $# systemctl status fstrim.timer
    ● fstrim.timer - Discard unused blocks once a week
  2. @hostberg hostberg created this gist Nov 26, 2021.
    68 changes: 68 additions & 0 deletions Proxmox with LVM-thin and why we should use Trim-Discard.MD
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    # Proxmox with LVM-thin and why we should use Trim/Discard

    Excerpts from the [Proxmox VE Administration Guide](https://pve.proxmox.com/pve-docs/pve-admin-guide.html)]


    > 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](https://pve.proxmox.com/pve-docs/pve-admin-guide.html#qm_hard_disk_discard).

    > 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
    ```bash
    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 fstrim working
    ```bash
    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
    ```

    ```bash
    vm-170 $# fstrim --fstab --verbose
    /: 34.6 GiB (37143580672 bytes) trimmed on /dev/disk/by-uuid/bcaec9da-3717-4a91-938f-69eadf5baf07
    ```

    ```bash
    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 fstrim.timer enabled
    ```bash
    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.
    ```

    ```bash
    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`.