Resizing an ext3 Xen disk image

I’ve seen this a few other places and it didn’t quite work. Let’s say you download a half-gig barebones Linux VM to use with Xen. It runs great, but you want more than 500MB of disk space. Here’s exactly what I had to do:

  • Shut down the virtual machine and make sure nothing is using the disk image!
  • Make a backup of the current image (cp imagefile.img imagefile.img_backup) in case anything goes awry
  • Make a blank file with dd of the size you want to add, e.g., dd if=/dev/zero of=10gig.img bs=1024 count=10000000 for a 10GB image. This may take a bit.
  • Append that to the end of your file, e.g., cat 10gig.img >> imagefile.img
  • “Mount” it as a loopback device: losetup /dev/loop0 imagefile.img
  • Run an fsck, which will find errors, since growing a filesystem this way is bizarre: e2fsck -f /dev/loop0
  • After that completes, resize the filesystem. This may take a while: resize2fs /dev/loop0
  • “Delete” the loopback device (think “unmount”): losetup -d /dev/loop0
  • Start your VM up, and it should now see 10GB extra space!

Leave a Reply

Your email address will not be published. Required fields are marked *