Linux: Extending a virtual raw hard-disk image
Extending a libvirt raw storage volume in Debian Squeeze
This article takes you through the necessary steps to extend a raw libvirt storage volume that is part of a libvirt storage pool on a Debian Squeeze GNU/Linux system. A libvirt storage volume is essentially a virtual raw hard-disk image that is used by a virtual machine.
In this example, I’ll extend “rawdisk.img” raw disk image from 8GB to 12GB. We use the truncate command to extend the image file size, kpartx utility to create device maps from partitions (an association between partitions on the image file and loop devices) then gparted to do the actual file system extension. You can begin by installing kpartx and gparted utilities if you do not have them already installed on your Debian system.
user@host:~$ sudo aptitude install kpartx gparted
The steps for extending the raw image file are as follows.
1) Shutdown the virtual machine
2) Take a back-up of the disk image to be extended by simply copying it in the same folder using a different name
user@host:~$ cd /var/lib/libvirt/images user@host:~$ sudo cp rawdisk.img rawdisk-backup.img
3) Extend the file size of the rawdisk.img volume from 8GB to 12GB using the truncate command.
user@host:~$ sudo truncate –size $((8+4))G rawdisk.img
In the command above, we extend the rawdisk.img file size by 4GB over and above the original 8GB to get 12GB.
4) Create device maps for partition tables found in the raw image file using kpartx.
user@host:~$ sudo kpartx -av rawdisk.img add map loop0p1 (254:4): 0 16755732 linear /dev/loop0 63]
The above command will add partition mappings for the disk image file i.e. setup a loop device (/dev/loop0 in this case) as your hard-disk and create links in /dev/mapper/ director for different partitions found on the disk image (/dev/mapper/loop0p1 in our case)
5) Create similar links in /dev/ directory since utilities like gparted expect to find partition
1 of 3 12/20/2011 03:25 PM
Debian Administration :: Extending a libvirt raw storage vo… http://www.debian-administration.org/article/Extending_a_li…
maps there. We do so by first checking which device is linked to /dev/mapper/loop0p1 and creating a symbolic link to the same device in /dev directory.
user@host:~$ ls -l /dev/mapper/loop0p1 lrwxrwxrwx 1 root root 7 Apr 23 18:16 /dev/mapper/loop0p1 -> ../dm-4
user@host:~$ ln -s /dev/dm-4 /dev/loop0p1
6) Open and edit the partitions of the mapped disk image using gparted, a gnome editor for manipulating disk partitions. You’ll need to run gparted as root to extend the partition /dev/loop01p on disk /dev/loop0
user@host:~$ sudo gparted /dev/loop0
Note that only /dev/loop0 is provided as an argument to gparted and not /dev/mapper /loop01p or /dev/loop01p.
In gparted, you’ll see the active partition with used and free space (8GB in our example) and a second partition of unallocated free space. The second unallocated free space (4GB in this example) is the space we want to add to the to the active partition.
Select then right click the active /dev/loop01p partition and choose Resize/Move. In the resize/move dialog box, set New Size with values of the Maximum size (i.e combined size of used + unallocated partition)
Next Apply all Operations to commit the change and exit gparted.
7) Well, we are not over yet. If you save the output of gparted, you’ll clearly see that the new changes were not committed to file probably due to a bug with either kpartx or gparted when working on virtual image files. We have to repeat step 4 and 6 after deleting and adding partition mappings for the new change to take effect.
8) Delete the partition mappings using kpartx
user@host:~$ sudo kpartx -d rawdisk.img loop deleted : /dev/loop0
9) Add partition mappings once again as was done in step 4 above.
user@host:~$ sudo kpartx -av rawdisk.img add map loop0p1 (254:4): 0 16755732 linear /dev/loop0 63]
10) Edit the partitions using gparted once again. user@host:~$ sudo gparted /dev/loop0
However, to get gparted to commit the changes to the image file, reduce the partition size by a small factor e.g. 10MiB then increase the partition size again by the same factor (10MiB).
2 of 3 12/20/2011 03:25 PM
Debian Administration :: Extending a libvirt raw storage vo… http://www.debian-administration.org/article/Extending_a_li…
Apply All Operations for the changes to take effect before exiting gparted. 11) Delete the partition mappings using kpartx
user@host:~$ sudo kpartx -d rawdisk.img loop deleted : /dev/loop0
12) Don’t forget to delete the symlinks in /dev/ directory created in step 5 above. user@host:~$ sudo rm /dev/loop0p1
13) Refresh your libvirt storage pool using the virsh utilities. Replace “default” with your pool name at the end of the command below.
user@host:~$ virsh pool-refresh default Pool default refreshed
14) Examine information about your relatively new extended raw image disk
user@host:~$ sudo virsh vol-info /var/lib/libvirt/images/rawdisk.img Name: rawdisk.img Type: file Capacity: 12.00 GB
Allocation: 8.00 GB
You can see that the capacity has been extended to 12GB. What had been allocated remains at 8GB but the allocation will grow in tandem with the disk.
15) Fire up you virtual machine and let any native disk utilities check and repair the newly extended file system.
DONE.
By Aogulla