Score:0

How to (properly) trim Hyper-V Linux VMs on Server 2022?

kr flag

I have tried a truckload of guides, but neither seems to work properly. My issue is the following:

  • I have a host with 1TB SSD drives
  • The VMs barely use ~50G/150G of space (when checked with df -h), yet, they consume 170 and 260gb of space respectively on the disk.
  • I have the Hyper-V guest services enabled in the VMs.
  • They keep growing.

Host OS: Windows Server 2022 Datacenter 21H2
Guest OS: Ubuntu 22.04.1 (ext4 root partition)

The latest guide I tried was this: https://helgeklein.com/blog/faster-trimming-compacting-hyper-v-ubuntu-vms/

In short:

  • Issue on VMs: sudo fstrim -v /
  • Shut down VMs
  • Issue from PowerShell (change the path in command so it matches your vhdx folder): gci -File -Filter *.vhd* -Path D:\VMs -Recurse | % {Mount-VHD $_.FullName -ReadOnly; Optimize-VHD $_.FullName -Mode full; Dismount-VHD $_.FullName}

But, it does not really work. Didn't shed all that much from my VMs at all, unfortunately. The command did run for a few minutes, the NVMe SSD was spiking really high and that's about it.

It's just annoying because the SSDs are not in RAID, and if the VMs keep growing, I'll just run out of space, which is not so simple to add on a server.

Thank you.

Score:-1
to flag

I run a few web-hosting ubuntu VMs on a hyper-v server. So I'm not surprised about your issues here. The real issue is that the documentation is not really clear in many aspects and a lot of guides on the internet are dated.

E.g. I would not trust "ubuntu on hyperv" guides from before server 2019 (1809) was out. As for actual ubuntu versions, trash any guides for versions before 20.04LTS. I have/had successfully run VMs 20.04, 21.10, 22.04, 22.10 in various configurations.

Now the real answer here would be to check your kernel. Hyper-v capabilities highly rely on the kernel. If you install some lightweight non-suitable kernel not only your fstrim, but some other virtualisation things like e.g. dynamic RAM resize may stop working.

As a sidenote -- I once had a debian12 (preview) VM on hyper-v where dynamic RAM could be increased, but never decreased :D Which would mean it was always increasing upto defined limit. This was caused by incompatible kernel.

So in short my recommendation is to install HWE-EDGE kernel. My 22.04 VM currently runs on 5.19.0-41-generic, which is the most updated kernel from the following package:

 linux-generic-hwe-22.04-edge/jammy-updates,jammy-security,now
 5.19.0.41.42~22.04.14 amd64 [installed]

of course you can also run on just HWE, but I have never had any issues running edge variant on 20.04 or 22.04.

Another option, if you want maybe a smaller more optimized kernel, is to run linux-azure kernels. E.g. on the dated 20.04 machine I currently run:

linux-azure-edge/focal-updates,focal-security,now 5.15.0.1038.45~20.04.28 amd64 [installed]
in flag
What has the kernel to do with trimming disks?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.