Skip to content

Instantly share code, notes, and snippets.

@jaircuevajunior
Last active January 22, 2023 10:20
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaircuevajunior/552b9979293865b5e5c873d6f8f0d73a to your computer and use it in GitHub Desktop.
Save jaircuevajunior/552b9979293865b5e5c873d6f8f0d73a to your computer and use it in GitHub Desktop.
Shrinking XenServer VDI VM Disk

Shrinking XenServer VDI VM Disk

XenServer doesn't allow us to do this kinda shrinking so we better recreate the vm copying all the files and recreating grub (boot) files.

1. Prepare the new disk

  • Create a Virtual Disk with the final desired size (eg our running vm disk has currently 100G and we want to shrinkg it to 80G so this new disk is gonna be 80G)
  • Attach the new disk to the running VM. (For this we'll need to shutdown the vm for a brief moment and power up it back again)
  • Under the VM terminal we format the new disk:
fdisk /dev/xvdb #(n, p, ...) DON'T FORGET TO CREATE THE SWAPP PARTITION!!!
mkfs.ext4 /dev/xvdb1
mkswap /dev/xvdb2
  • Now we copy the files to the new partition
mount /dev/xvdb1 /mnt
rsync -ahPHAXx --delete --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found} / /mnt
  • Edit /mnt/etc/fstab like this: [Note that now we changed the HD reference from B to A]
/dev/xvda1 / ext4 errors=remount-ro 0 1
/dev/xvda2 none swap sw 0 0
  • Alternative: Theorically you could jump to Step 3 (Instal GRUB) and run those commands here, without the need of a Live CD, BUT I tried that and it didn't work. Of course you'll need to go to Step 2 after Step 3 in this case.
  • Shutdown the VM to detach the disk and optionally you could power up it back again (for SLA purposes)

2. Create the new VM

  • Under XenCenter, create a new VM, choose any ISO and a minimum size for the Storage eg 1G. (This storage will be deleted anyways). UNMARK the "automatic power on after setup" option.
  • Attach our disk
  • Delete the existing disk (that of 1G)
  • Click on Properties under the disk and change Device Position from 1 to 0.

3. Install GRUB

  • Start the VM with a live CD
  • Install grub through these commands:
mount /dev/sdXY /mnt
mount --bind /dev /mnt/dev 
mount --bind /dev/pts /mnt/dev/pts 
mount --bind /proc /mnt/proc 
mount --bind /sys /mnt/sys
chroot /mnt
grub-install /dev/sdX
grub-install --recheck /dev/sdX
update-grub
exit 
umount /mnt/sys 
umount /mnt/proc 
umount /mnt/dev/pts 
umount /mnt/dev 
umount /mnt

4. (Optional) Alternative method to install GRUB

  • Insert the boot repair disk on the Virtual DVD Drive
  • Start the VM
  • Setup network (we need internet in this process)
  • Proceed with automatic boor repair
  • Power off, take off the DVD, power up it back again...

Enjoy! :)

References

@kolonuk
Copy link

kolonuk commented Nov 22, 2022

Thanks, this really helped me.

Some comments:

  1. Step 2 was unneeded for me as I changed the bus order of the new disk after removing the old one. If you don't change the bus order, the fstab entries will need to be xvdb instead of xvda.
  2. Kernels:
  • Before running "grub-update", check that /boot has at least one kernel
  • After running "grub-update", it should list the available kernels
    In either case, you need to mount and copy kernels from existing /boot if it was a separate partition on the existing machine.
  1. Not sure about the unmount commands, as I just rebooted. I guess they mark the device as clean when it comes to file system integrity...? Would be nice to know why you have them.
  2. You can use this process to move from LVM to normal partitions if required, just remember to remove LVM at some point, otherwise you get LVM mount failure messages during boot up.
  3. Finally, in Debian, the rescue console doesn't support ext4, so the mounts will fail. Try other rescue flavours - Rocky has ext4 support

Cheers, John

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment