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!!

Marc NJ
  • 51

2 Answers2

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!

Marc NJ
  • 51
0

My method is similar to the OPs method (thank you Marc NJ), but perhaps with less steps?

How to shrink overly large ubuntu server VMDK on Hyper-V on ubuntu 24.04 with LVM.

When shrinking disk space via they Hyper-v manager on an ubuntu server VM on an error happens and the ubuntu vm wont boot any more.

I spent many many hours following other guides (thank you to everyone who posted about their efforts and what they did) on the net - and this is what worked for me: Rather than shrinking the vmdk with Hyper-v we make a new smaller vmdk and copy the data across using Linux.

Basically it works like this

  1. Take a backup of your VM in case you break it
  2. Use DF -h to see how much disk space is available/used
  3. Delete out any files you don’t want to save space, I got mine down to 55GB used out of 470GB VMDK.
  4. Make a new smaller dynamically expanding disk in hyper-v and attach it to your ubuntu vm as a second disk. I chose 108 GB (~100 GiB)
  5. Use systemrescue CD to Reduce the ORIGINAL DISKS VG size and then the LV size to 108G (to match the new disk we made and added)
  6. Use gparted live iso to reduce size of sda3 to 100GiB
  7. Use systemrescue cd to copy across the contents of the VG (and LV) to the new 108GB disk via dd command – this can take a while
  8. Remove the old disk, boot from the new 108 GB disk.

I installed the free Veeam backup and replication suite, and took a backup of the 407GB VM in case anything should go wrong. I checked it would restore ok and it did.

I then logged into my ubuntu VM deleted out any files I didn't need off the VM in order to make it as small as possible. df -h then told me I am using 55GB of my 470GB vl.

I then powered off the VM and in Hyperv manager I added a new, blank 2nd dynamically expanding virtual hard disk that was 108GB (~100 GiB) in size. So then I had two disks, the original 470GB one and the new blank 108GB one.

booted it up again with the system rescue disk. From a command prompt I gave it

vgchange -a y

in order for the system rescue disk to see the LV. Then I ran

lvdisplay

to get the name of the LV which was /dev/ubuntu-vg/ubuntu-lv

then i checked the file system with

e2fsck -fy /dev/ubuntu-vg/ubuntu-lv

I then reduced the filesystem to 100 GiB (in order to have a little headroom for growth as I am adding more sites to this VM soon) using resize2fs:

resize2fs /dev/vg/root 100G

then i reduced the LV size using

lvreduce -L 100G /dev/ubuntu-vg/ubuntu-lv

then i expanded the filesystem to be same size

resize2fs /dev/ubuntu-vg/ubuntu-lv

I then shutdown, removed the SR iso and checked the machine still booted ok it did

Then i put in Gparted live iso and reduced the size of SDA3 to 100GiB using the gui and resize function. I then made a new partition table on the new disk. I then re-made each partition and flags on the new disk so it looked the same as the old disk. (Right click on sda1 , go to sdb then paste. Manually re-create the LVM make sure to select vlm p2).

I then shutdown, removed the Gparted iso and checked the machine still booted ok it did.

Then i put SR iso back in and booted from it. I did

lsblk

to see my hdd names. SDA was my existing drive, SDB is the new one we created. run

su to make yourself root then: I used DD to clone one disk to another

dd if=/dev/sda1 of=/dev/sdb1 bs=1M status=progress

dd if=/dev/sda2 of=/dev/sdb2 bs=1M status=progress

dd if=/dev/sda3 of=/dev/sdb3 bs=1M status=progress

(if Specifies the input file (source).) (of Specifies the output file (destination) IF it fails here its probably that we didn't make the new drive big enough. In then end I made mine 120GB/~112 Gib in order to fit everything from SDA onto it.

Shutdown, remove original disk and just have the new one we created. Boot the VM. Do

df -h

to show the new disk is now using 55GB of our 100GB disk!

You will need downloaded and available to Hyper-v to add to the VMs:

  • gparted-live-1.6.0-10-amd64.iso
  • systemrescue-11.03-amd64.iso

PS Hyper-V works in GB but Linux seems to work in Gib. So the 108GB disk I made was actually ~100 GiB. Don't forget to convert your GBs and GiBs!

JaqDraco
  • 1
  • 2