Add disk space to a .img disk image (for use with xen for example)

Add disk space to a .img disk image (for use with xen for example)

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 :
[cc lang=”bash”]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[/cc]

– create empty file of the size we want to add : (10gb here)
[cc lang=”bash”]dd if=/dev/zero of=/xen/temp_expand bs=1024k count=10000[/cc]

– stop your domU
[cc lang=”bash”]xm shutdown mydomU[/cc]

– backup img files
[cc lang=”bash”]cp -a /xen/domains/mydomU /xen/domains/mydomU-bak[/cc]

– add the empty file to the img file :
[cc lang=”bash”]cat /xen/temp_expand >> /xen/domains/mydomU/disk.img[/cc]
You can repeat this step as many times as you need

– tell the filesystem to check and adapt its size :
[cc lang=”bash”]resize2fs -f /xen/domains/mydomU/disk.img[/cc]

– restart your domU
[cc lang=”bash”]xm create -c mydomU.cfg[/cc]

– check in your domU that everything is ok
[cc lang=”bash”]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
[/cc]
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 :
[cc lang=”bash”]dd if=/dev/zero of=/xen/domains/mydomU/otherdisk.img bs=1024k count=150000[/cc]
or
[cc lang=”bash”]cat /xen/temp_expand >> /xen/domains/mydomU/otherdisk.img[/cc]

add the new disk (xvda3) to your /etc/xen/mydomU.cfg :
[cc lang=”bash”]
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’,
][/cc]

after rebooting you domU, check if the new disk is available :
[cc lang=”bash”]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[/cc]

You’re now free to create your filesystem on this disk, and to mount it wherever you want!
[cc lang=”bash”]$ 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[/cc]

This Howto was made under Ubuntu Hardy, Xen 3.2

Comments are closed.
libero suscipit commodo massa sem, libero. nec vel, quis, Donec ut amet,