Monday, May 3, 2010

Xen resize disk (image) into DomU

The objective is to increase the storage allocated to a xen DomU vm.
Let's say the situation before we do anything looks like this:

The virtual machine name (as seen into xm list)  rhel-5.3-HARMONY
The vm is using tapio disk image files (not lvm) for the hard drive.

root@localhost.localdomain[~]11:12:00# df -k

Filesystem 1K-blocks Used Available Use% Mounted on

/dev/xvda 9048256 3198968 5481516 37% /
tmpfs 1048576 0 1048576 0% /dev/shm

I want to add an extra 1GB space to this the 9GB already in there (the red label above).

Steps to do this:
  • Shutdown the virtual machine (vm) - xm shutdown rhel-5.3-HARMONY
  • Make a backup of the disk  - cp rhel-5.3-HARMONY.img rhel-5.3-HARMONY.img.backup
  • Create an empty file with the desired size (I choosed 1GB) - dd if=/dev/zero of=ZeroContentFile bs=1024 count=1000000
  • Append the new empty file to the initial image file - cat ZeroContentFile >> rhel-5.3-HARMONY.img -  before we did the append on the filesystem (doing ls -al):
          -rwxr-xr-x 1 root root 9414967296 May 3 11:19 rhel-5.3-HARMONY.img
          and after:
          -rwxr-xr-x 1 root root 10438967296 May 3 11:24 rhel-5.3-HARMONY.img
  • Delete the temporary empty file - rm ZeroContentFile
  • Resize the image file - resize2fs -f rhel-5.3-HARMONY.img
  • Check with fsck the file - fsck.ext3 rhel-5.3-HARMONY.img
  • Start the vm to see if the new disk has been added - xm create /etc/xen/rhel-5.3-HARMONY.cfg
  • Check the disk space and you will see
root@localhost.localdomain[~]11:31:48# df -k
Filesystem 1K-blocks Used Available Use% Mounted on

/dev/xvda 10033864 3198928 6529108 33% /
tmpfs 1048576 0 1048576 0% /dev/shm


That's it you just increased your space by 1GB for the vm.
One thing to keep in mind is to do the backup of the original disk (just in case something goes wrong).