1

Initially I set up a dynamically expanding VHDX under Hyper-V that was about 6 TB and then installed Ubuntu Server 20.04 using LVM on it. I've since managed to shrink the installed Ubuntu Server down to 155 GB, but the dynamically expanding VHDX still has a capacity of 6 TB. I want to move this to a drive that has nowhere near 6 TB. I think it might work anyway since the Ubuntu Server can't go bigger than 155 GB, but I'd like to VHDX to reflect maybe 200 or 250 GB max. I can't figure out how to shrink it though without killing the installed Ubuntu Server. If I remove all checkpoints and just go into editing the VHDX via Hyper-V and shrink it that way, it somehow causes an issue and the Ubuntu Server won't load up afterwards. I must be missing a step here...any help is greatly appreciated!!

2
  • You will likely need to first zero the space on the storage device, then shrink the file. If Ubuntu thinks it still has 6TB of storage, then you may also need to make some modifications to the partitions inside the VM so that Ubuntu does not exceed the limits you are imposing on it.
    – matigo
    May 14, 2021 at 2:40
  • As far as I can tell, Ubuntu only thinks it has 155 GB and the rest of the space is unallocated. Any suggestions on how I zero the space on the storage device? And then I should use Hyper-V to just shrink it?
    – Marc NJ
    May 14, 2021 at 2:46

1 Answer 1

0

OK, so I actually think I figured it out (with help from some of the websites below) - it was not easy.

First, as mentioned, I had to previously (prior to asking the above question) reduce the size of everything in Ubuntu Server (posting below just to be complete) - I did that following the below websites:

https://maideveloper.com/blog/how-to-move-lvm-partition-to-another-disk-drive

How can I resize an LVM partition? (i.e: physical volume)

Here's what I did:

I booted into a LiveCD (Ubuntu Desktop 20.04) and in terminal ran these commands (I think - my memory is a bit hazy at this point):

sudo e2fsck -f /dev/<VOLUME_GROUP/<LOGICAL_VOLUME>
sudo resize2fs /dev/<VOLUME_GROUP>/<LOGICAL_VOLUME> 150G
sudo lvreduce -L 155G /dev/<VOLUME_GROUP>/<LOGICAL_VOLUME>
sudo resize2fs /dev/<VOLUME_GROUP>/<LOGICAL_VOLUME>

You should replace <VOLUME_GROUP> and <LOGICAL_VOLUME> with appropriate entries for your system. I believe the above commands: checks for errors, then shrinks the file system, then shrinks the logical volume (don't shrink to something smaller than new file system size you just shrunk), then extends the file system to use all available space on the logical volume. I also think something along these lines might have worked but I didn't try: sudo lvreduce --resizefs -L 155G /dev/<VOLUME_GROUP>/<LOGICAL_VOLUME>

Then I went into GParted GUI on the LiveCD and used that to shrink the partition to it's new 155 GB size and was left with a ton of unallocated space. This is where my question above picks up.

So next I found this answer: How do I move my LVM 250 GB root partition to a new 120GB hard disk?

Here's what I did:

  1. Used Hyper-V to create a new dynamically expanding VHDX hard drive (this time only 250 GB) and attached it to my existing Ubuntu Server Hyper-V
  2. Booted into LiveCD again and used GParted to create exactly the same partitions on this new VHDX hard drive (sdb) that I had on my existing 6 TB dynamically expanding VHDX hard drive (sda) - even setting the flags, etc.
  3. Booted back into my Ubuntu Server and ran these commands:
    • sudo pvcreate /dev/sdb3 (sda3 is where my logical volume resides)
    • sudo vgextend <VOLUME_GROUP> /dev/sdb3
    • sudo pvmove /dev/sda3 /dev/sdb3
    • sudo vgreduce <VOLUME_GROUP> /dev/sda3
  4. Booted into Clonezilla and cloned sda1 to sdb1 and then cloned sda2 to sdb2
  5. Modified the settings of the Hyper-V:
    • removed the new second 250 GB VHDX hard drive
    • adjusted the primary drive to point to the new 250 GB VHDX hard drive
  6. Booted up the Ubuntu Server Hyper-V and got a weird error initially (briefly) and then it booted fine and voila - I was using the 250 GB hard drive! The error I got was: System BootOrder not found. Initializing defaults. Creating boot entry "Boot0004 with label "ubuntu" for file "\EFI\ubuntu\shimx64.efi"
  7. Realized that if you went to look at the Hyper-V settings under Firmware -> Boot order, there were now 2 shimx64.efi files - one at the very top (the new one that apparently was created by Hyper-V in step 6 above) and one below DVD Drive (the old one). No easy way to remove the old one - so I found this site: https://vmlabblog.com/2018/08/how-to-change-vm-bootorder-with-powershell/ which provided some guidance
  8. In an administrator PowerShell, ran these commands:
    • $ubuntuserver = Get-VMFirmware "<NAME OF VIRTUAL MACHINE IN HYPER-V>"
    • $goodfile = $ubuntuserver.BootOrder[0]
    • $dvddrive = $ubuntuserver.BootOrder[1]
    • $badfile = $ubuntuserver.BootOrder[2]
    • $hddrive = $ubuntuserver.BootOrder[3]
    • $network = $ubuntuserver.BootOrder[4]
    • Set-VMFirmware -VMName "<NAME OF VIRTUAL MACHINE IN HYPER-V>" -BootOrder $dvddrive,$goodfile,$hddrive,$network
    • $ubuntuserver.BootOrder (just to confirm everything)
  9. And I think that did the trick. I believe I'm now all set.

I would welcome any comments or checks on the above since I'm not entirely sure what I did is OK, but it seems to be. Hope this helps others in the future too!

You must log in to answer this question.

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