VM HD Resize

I had a small virtual machine (Ubuntu 12, Virtual Box) being hosted on a headless server. It had an 8Gb drive which I needed to resize.It’s done now, but took more googling than it should have done. Here’s a summary.

Stop the virtual machine

Copy the .vmdk to the workstation

Copy the .vmdk to a .vdi

Resize the .vdi

Attach the .vdi (in place of the .vmdk)

Boot to gparted

Create a swap partition

Extend sda2 to fill the unallocated space

Extend sda5 to fill the partition

Reboot

Extend the logical volume

Resize the filing system

Move the swap partition

Shutdown the machine (test)

Copy the .vdi back to the host server

Detach the existing drive

Attach the new drive

Restart the virtual machine

Steps 1 to 4

As the host server is headless I wanted to do as much of the work as possible on my workstation. I originally created the vm on my machine and copied it over to the host, so only needed to copy the disk image back over. You might need to copy the whole machine (folder) over to your workstation. To stop the vm ssh on to it and sudo shutdown -h now.

Once the .vmdk is copied back to the workstation you can use the graphical VM VirtualBox Manager to copy the drive image to a .vdi. I then resized it on the command line with

VBoxManage modifyhd vm-disk1.vdi --resize 51200

Then using the manager app I detached the .vmdk and attached the .vdi drive. I also set the optical drive to my gparted iso.

Steps 5 to 9

Now start the vm. It should boot to gparted. I went to the command line and then ran startx. I found that trying to boot to X didn’t work. Start the gparted app. Start be deactivating the active drives. I then added an 8Gb swap partition (sda3) at the end of the space before resizing the extended partition (sda2). (A dedicated swap partition is not necessary. Ubuntu seems to use a file on the drive. I’m used to CentOS and I’ve always seen swap on it’s own partition. For a vm I can’t see that it makes any difference.)

I applied the changes after the extended partition had been extended. Then deactivate the drives again and extend the LVM partition (sda5) to fill the now available space. Apply changes. Then shutdown the machine and unmount the gparted iso.

Steps 10 to 15

Now reboot the vm and working it’s command line first extend the logical volume

lvextend -l +100%FREE vm-vg/root

then the filing system

resize2fs /dev/vm-vg/root

If you have taken the opportunity to move the swap partition then turn off swap and mark the new partition as swap

swapoff -a
mkswap /dev/sda3

At this point edit (sudo) /etc/fstab and tell it that swap is now /dev/sda3. Then turn swap back on with

swapon /dev/sda3

Now shutdown the machine. At this point you might want to ensure that it starts okay. I suffered kernel panics a few times; not sure why, perhaps swap was misconfigured). Once you’re happy copy the .vdi drive image back over to the host server.

Steps 16 to 18

Now ssh on to the host server and detach/attach drives. (Not sure about the portcount switch; this worked but gave a slightly different configuration from the original.)