Linux Hyper-V Guest VHDX resize

I inherited a Linux virtual machine that had been converted from a VMWare ESXi 5.0 install to Hyper-V. The VHDX file I ended up with was an 800GB dynamic VHDX with only about 250GB of data in it.

Because of this I embarked on a task of shrinking the VHDX file to be much more reasonable to the VM file contents, not knowing what I was getting myself into working with a Linux file system. I’m sure I’m going to butcher the terminology here based on my ignorance and lack of knowledge.

Here’s the steps I performed to achieve my goal. I’m writing this quite some time after I actual did it, so I’ll try to fill in the gaps of my documentation as much as possible.

At this point I couldn’t shrink the actual VHDX because some of my Logical Groups were near the end of the disk extents.

  • Still within the liveCD environment, use following command list extent locations on the disk
pvs -v --segments
  • This gave me output like the following (numbers roughly simulated):
    PV         VG     Fmt  Attr PSize   PFree   Start Size
    /dev/sdb1  home   lvm2 a-   50.50g  123     0     4234
    /dev/sdb1  gd01   lvm2 a-   50.50g  123     14234 4234
    /dev/sdd1  office lvm2 a-   500.00g 123     20000 12450
  • I used this to calculate the actual start and end locations of each segment, to be used in the next commands.
  • From here I took my second segment and moved it into the first available free space, like this (original extent location, followed by destination extent location):
pvmove --alloc anywhere /dev/sdb1:38400-39039 /dev/sdb1:1280-1920
pvmove --alloc anywhere /dev/sdb1:38400-39039 /dev/sdb1:1280-1920
  • I continued to do this for each segment as I slowly consolidated my free space with my segments previous to it on the disk
  • Next I shrunk the physical volume (and confirmed with system-config-lvm GUI):
pvresize --setphysicalvolumesize 250G /dev/sdb1
  • Then I booted GParted liveCD to shrink /dev/sdb1 to 250GB, and put the free space into Unallocated space
  • Finally, I could use Hyper-V Manager to shrink the dynamic VHDX file to my destined size

 

After all of this, I turned the VM back on, and Linux booted as if nothing had changed except the size of my disks were quite a bit smaller.

Leave a Reply

Your email address will not be published.