Tuesday 23 April 2013

Add Disk Space into XEN Virtual Machine (.img Disk Image)


Add Disk Space into XEN Virtual Machine (.img Disk Image)

Sometimes, you need more space on a virtual machine disk than you thought in the beginning. Hopefully, if your domU disk is in a .img file, you can do it quite easily (you can do it easily with lvm partition too, even if it’s a different method). Here’s how to do it, as root of course:

- Check your partitions in your domU:
df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda2             10G  706M  8.8G   8% /
varrun                2.1G   44K  2.1G   1% /var/run
varlock               2.1G     0  2.1G   0% /var/lock
udev                  2.1G   16K  2.1G   1% /dev
devshm                2.1G     0  2.1G   0% /dev/shm

- create empty file of the size we want to add: (10gb here)
dd if=/dev/zero of=/xen/temp_expand  bs=1024k count=10000

- stop your domU
xm shutdown mydomU

- backup img files
cp -a /xen/domains/mydomU /xen/domains/mydomU-bak

- add the empty file to the img file :
cat /xen/temp_expand >> /xen/domains/mydomU/disk.img
You can repeat this step as many times as you need

- Tell the file system to check and adapt its size:
resize2fs -f /xen/domains/mydomU/disk.img

- restart your domU
xm create -c mydomU.cfg

- check in your domU that everything is ok
df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda2             30G  711M   28G   3% /
varrun                2.1G   44K  2.1G   1% /var/run
varlock               2.1G     0  2.1G   0% /var/lock
udev                  2.1G   20K  2.1G   1% /dev
devshm                2.1G     0  2.1G   0% /dev/shm
Voila! Your disk is now bigger :)

You can also use this method to create a brand new blank disk image, and add it as a new disk for you domU:
dd if=/dev/zero of=/xen/domains/mydomU/otherdisk.img bs=1024k count=150000

or

cat /xen/temp_expand >> /xen/domains/mydomU/otherdisk.img

add the new disk (xvda3) to your /etc/xen/mydomU.cfg :

disk        = [
                  'tap:aio:/xen/domains/mydomU/swap.img,xvda1,w',
                  'tap:aio:/xen/domains/mydomU/disk.img,xvda2,w',
                  'tap:aio:/xen/domains/mydomU/otherdisk.img,xvda3,w',
              ]
After rebooting you domU, check if the new disk is available :

fdisk -l /dev/xvda3

Disk /dev/xvda3: 125.8 GB, 125829120000 bytes
255 heads, 63 sectors/track, 15297 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00000000

Disk /dev/xvda3 doesn't contain a valid partition table
You’re now free to create your file system on this disk, and to mount it wherever you want!
$ mkfs.ext3 /dev/xvda3
$ mkdir /mysql
$ mount /dev/xvda3 /mysql/
$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda2             30G  711M   28G   3% /
varrun                2.1G   44K  2.1G   1% /var/run
varlock               2.1G     0  2.1G   0% /var/lock
udev                  2.1G   20K  2.1G   1% /dev
devshm                2.1G     0  2.1G   0% /dev/shm
/dev/xvda3            117G  188M  111G   1% /mysql

No comments:

Post a Comment