5

I have installed Ubuntu 18.04 LTS on a Hyper-V VM, everything was fine until I noticed that the VHDX file takes up unusually enormous amount of disk space. From within the VM, you can see that the system only takes less than 10GB of space

VM disk space usage

However on the windows host the VHDX file takes up more than 20GB of disk space!

Host disk space usage

And this is after I have run a disk compaction with the following steps:

  1. within the VM, run sudo fstrim /
  2. shutdown the VM, start a PowerShell console with administrator rights
  3. mount the VHDX with Mount-VHD '.\ubuntu 18.04 lts.vhdx' -ReadOnly
  4. compact the VHDX with Optimize-VHD '.\ubuntu 18.04 lts.vhdx' -Mode Full
  5. unmount the VHDX with Dismount-VHD '.\ubuntu 18.04 lts.vhdx'

It sorta works as the size of the VHDX was reduced from 23GB before to 21GB now, but considering inside the VM it shows only less than 10GB of space is currently being used, this is hardly much compaction in size.

I've also tried running the Disk Compact function from the Hyper-V administration UI but to no effect. So any idea what might be the cause of this enormous VHDX size and how may I properly compact it? Thanks.

1
  • fstrim worked well for me, try compacting all filesystems, ie fstrim -a -v and remove all VM checkpoints (snapshots) Commented Dec 1, 2019 at 1:02

1 Answer 1

5

I faced some issue, I tried everything what I know already or found via Google. Finally I resolve it this way:

  • after some trim, dd zeroing and other magic (I don't know these are matter or not)
  • I created a new vhdx, a little bit larger than what I need (+1GB)
  • boot from gparted live CD
  • and finally I copied boot and data partition with this tool onto new vhdx.
  • move the new hdd up to top of boot order list or just detach from VM
  • I'm happy now :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .