Trim VM filesystem while vm running?

fx919

New Member
Mar 19, 2019
13
3
3
48
Hi,

is it possible and safe to trim/discard a VMs filesystem from outside(proxmox host) while the vm is online?

I have some non-fstrim-enabled VMs on a proxmox host with lvm thin provisioning and I want to fstrim the vms filesystem regularly. So I thought I shutdown the VM and mount the VM filesystem from the proxmox host and run fstrim on the mountpoint. But I did not check that the VM really had shut down. In fact it hadn't, because it was stuck at some point. So I obviously did an fstrim on a twice mounted filesystem. I thought I trashed the virtual machine by that, but that did not happen. The VM started up without no problem.

I wonder whether it is generally safe to do that?

What I did to trim the VMs filesystem:
  • added partition device files
    Code:
    kpartx -av /dev/mapper/pve--vm--100--disk--0
  • mounted VM filesystem on the host
    Code:
    mount /dev/mapper/pve--vm--disk--0p1 /mnt2
  • ren fstrim
    Code:
    fstrim -v /mnt2
  • unmounted filesystem
    Code:
    umount /mnt2
  • delete partition devices files
    Code:
    kpartx -dv /dev/mapper/pve--vm--100--disk--0
 
Last edited:
The double mount alone is a bad idea in general. Filesystems that are not designed for that can suffer data-loss from just that step alone. Doing a TRIM on a running, double-mounted disk is trouble² indeed, while it may have worked once, I wouldn't recommend it.

I assume what happened to not have it break that one time is that the VM was stuck shutting down, in a state where the guest OS had already stopped issuing writes.
 
  • Like
Reactions: fx919 and hellfire
I assume what happened to not have it break that one time is that the VM was stuck shutting down, in a state where the guest OS had already stopped issuing writes.

That sounds like it may be safe that, when I pause a VM, do fstrim and unpause the VM afterwards?
 
That sounds like it may be safe that, when I pause a VM, do fstrim and unpause the VM afterwards?
It was not my intention to make it sound like any combination of doing that will be safe :) Even then you're double-mounting the filesystem (pausing the VM doesn't unmount the FS from the guest's view), which means the filesystem might be in an inconsistent state altogether.
 
  • Like
Reactions: fx919 and hellfire