Howto: Resize Xen Loop Disk Image

Win4lin, KVM, QEMU, Virtualbox and Xen are all widely used technologies, used in virtual servers. Fairly often a virtual server run from within a loop filesystem and generally start with a fairly small file (1GB is big for a normal file, but not when it is pretending to be an entire filesystem!)

However things often grow over time.

Here is a nice simple procedure for increasing the size of a loop filesystem, if the filesystem is ext2 or ext3 (the procedure should work for ext4 too, but I havn’t tested it yet)

1. Stop the virtual server that is using the loop filesystem
2. Add some extra space to your loop filesystem file
# dd if=/dev/zero bs=1024k count=1024 >> loop_image_file

This adds 1GB to the end of a file called loop_image_file (make sure to use the append output redirector >> not a single >, otherwise you’ll have an empty 1GB file!)

3. Force a check on the resized/increased filesystem
# e2fsck -f loop_image_file

4. Resize the filesystem within the loop filesystem file
# resize2fs loop_image_file

5. Start the virtual server again

7 Comments.

  1. Isn’t there a proper solution for online resizing? Like, run fallocate on the host, do some xen magic (which?), and use resize2fs inside the guest.

  2. Howto: Xen Loop Disk Image vergrössern | Stetix - pingback on July 6, 2010 at 11:59
  3. Forgot to mention, the above was done with a Debian Lenny Xen host and Etch guest. It was a shrink action as I wanted to reclaim space on the Xen host.

    I expected to have to use dd to physically resize the file from 4 to something just over 3.1 but resize2fs appears to have catered for this. This seems to be at variance with the man page.

  4. To resize a Xen guest filesystem that exists as a loopback file:

    1. ensure the guest is not running
    2. make a copy of the file to resize, just in case (ensure you have sufficient free space first). Lets say the file is disk.img, backup is disk.img-backup
    3. as root, run resize2fs – it may prompt you to run “e2fsck -f disk.img” – if it does then do so.
    4. do the resize resize2fs disk.img 3G
    5. confirm the size with du -h disk.img
    6. now start up the xen guest and confirm the size is ok from within the guest: df -h

  5. Ciprian Pantea, to shrink a partition you need to first run resize2fs to set the filesystem to a smaller size, then afterwards you need to truncate the loop file to that size.

    I am not sure on the correct way to truncate the file, however I would suggest leaving a bit of room at the end unless you are certain about your math.

  6. (lol at the spam protection system :) )))))

    thanks for the tip, is there a way to shrink the partitions? also what partition type is the best used when using xen? reiser, xfs, ext2, ext3?

Trackbacks and Pingbacks: