Categories
Unix

Expanding disk space on a KVM virtual machine

I use KVM on CentOS 6.2 and had a need to expand the disk space for one of the VMs. In some cases, it makes sense to have the VM’s disk space relatively small before you know how much you are going to need, and then expand it when ready. I hate having a few VMs that are running at 20% disk usage, knowing that I am wasting space. Of course, you could use your VM with auto-expanding space, but I don’t want the trade-off of reduced disk access speed.

So, in a succinct fashion here are the steps I used to get this done! In this example, my vm is called “vm10” and I’m adding 200G

  • <shutdown your VM if running>
  • <cd into your VM image directory>
  • mv vm10.img vm10.img.bk
  •  qemu-img create -f raw addon.raw 200G
  • cat vm10.img.bak addon.raw >> vm10.img
  • <start the VM>
  • fdisk /dev/vda
  • p
  • d
  • 2
  • p
  • n
  • p
  • 2
  • <enter>
  • <enter>
  • p
  • t
  • 2
  • 8e
  • p
  • w
  • reboot
  • pvdisplay
  • pvresize /dev/vda2
  • lvresize /dev/VolGroup00/LogVol00 -l +100%FREE
  • resize2fs /dev/VolGroup00/LogVol00
  • reboot

And there you go! A newly sized VM.