Add disk space to a .img disk image (for use with xen for example)
Filed Under (Sysadmin) by Amandine on 13-08-2010
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 :
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)
- stop your domU
- backup img files
- add the empty file to the img file :
You can repeat this step as many times as you need
- tell the filesystem to check and adapt its size :
- restart your domU
- check in your domU that everything is ok
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 :
or
add the new disk (xvda3) to your /etc/xen/mydomU.cfg :
'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 :
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 filesystem on this disk, and to mount it wherever you want!
$ 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
This Howto was made under Ubuntu Hardy, Xen 3.2

Tiens, un petit village d’irréductibles lorrains encore sous Xen et Hardy :-)
héhé ouais :D t’utilises quoi toi? :)
Thank you very much for this guide!