Friday, May 29, 2009

LVM + XFS + VirtualBox - Resizing a guest partition

I've been doing a lot of work with virtualization lately and I am working on what I hope will become the great solution I've always wanted. The current configuration on the Host machine is a RAID0 array, with LVM logical volumes on top of that for each of my virtual machines. Each LV used for a Virtual Machine is formatted in XFS; from what I have read, it offers better performance with large files.

My Logical Volumes created for my VMs are created with a conservative sized disk. This allows plenty of free space on the LVM VG for future VMs, snapshots, scratch volumes, and expansion of existing LVs. That's right, it is possible to increase the size of all the disk components in this mess (Logical Volume, XFS Partition, Virtual Machine VDI, VM filesystem). For the most part this is a pretty painless process. The only real nasty part that involves downtime is resizing the VDI, which isn't really resizing at all. Rather, we will create a new larger VDI and copy the old disk over.

Here is the process:

1. Resize the logical volume on the host machine (if necessary).
lvextent -L+3G /dev/mapper/vg--vm-lv--ftp (extends ftp lv by 3GB)

2. Resize the filesystem on the host machine (if you resized the lv).
xfs_growfs /var/vm/ftp

3. Create a scratch lv on the host machine. This will be used to hold the resized clone, so make it large enough.
lvcreate -L 22G -n scratch vg-vm

4. Create a filesystem on scratch volume.
mkfs.xfs /dev/mapper/vg--vm-scratch

5. Create a mount point and mount the filesystem. Give vboxuser ownership.
mkdir /tmp/scratch
mount /dev/mapper/vg--vm-scratch /tmp/scratch
chown vboxuser /tmp/scratch

6. Change to vboxuser and create a new VDI of the desired size.
su vboxuser
VBoxManage createhd --filename /tmp/scratch/scratch.vdi --size 20480

7. Register scratch disk in virtualbox and verify
VBoxManage openmedium disk /tmp/scratch/scratch.vdi
VBoxManage list hdds

8. Register the gparted iso
VBoxManage openmedium dvd /root/gparted-live-0.4.5-2.iso

9. Shutdown the VM.
10. Add new VDI to VM and mount the gparted disk.
VBoxManage modifyvm "ftp" --hdb /tmp/scratch/scratch.vdi --dvd /root/gparted-live-0.4.5-2.iso

11. Start the VM, gparted should boot. Follow the default prompts to get to the desktop.
VBoxHeadless -startvm "ftp"

12. Open a prompt. Verify that the source (old) disk is hda and the destination disk (new) is hdb. Then use dd to copy hda to hdb. When finished, reread the partition table on the new disk.
sfdisk -l
dd if=/dev/hda of=/dev/hdb
hdparm -z /dev/hdb

13. Open gparted and use it to grow the disk partition you need to be bigger. Apply the changes, then shutdown the VM.
14. Remove the dvd and both drives. Then re-add the new VDI as hda. Startup the VM to verify that it boots cleanly.
VBoxManage modifyvm "ftp" --dvd none --hdb none --hda none
VBoxManage modifyvm "ftp" --hda /tmp/scratch/scratch.vdi
VBoxHeadless -startvm "ftp"

15. Set hda to none on VM. Close the two hard drive images.
VBoxManage modifyvm "ftp" --hda none
VBoxManage closemedium disk /tmp/
VBoxManage closemedium disk /var/vm/ftp/ftp.vdi

16. Move the scratch disk to the resized lv.
mv /tmp/scratch/scratch.vdi /var/vm/ftp/ftp.vdi

17. Re-open the new disk and add back into the VM
VBoxManage openmedium disk /var/vm/ftp/ftp.vdi
VBoxManage modifyvm "ftp" --hda /var/vm/ftp/ftp.vdi

18. Start VM
VBoxHeadless -startvm "ftp"

19. Unmount the scratch partition on the host machine and remove the lv to tidy up.
umount /tmp/scratch/
lvremove /dev/mapper/vg--vm-scratch

No comments:

Post a Comment