I’m using kvm for a while at work. Everything works quite fine, but today I needed to grow a disk image. I found some informations, but none are really clear so here the result :
First create a empty image file .. with this command (don’t use dd, qemu-img is really quicker than dd):
qemu-img create -f raw temp.img 10G
Next simply your image file + the temp one, in a biggest one ..
cat foo.img temp.img > bar.img
You will get a new image file which is 10G bigger than the original one .. Now you can boot your OS, and discover (via cfdisk for example), that your system has a additionnal 10G unused space .. So next step:
- Just create a new partition, and mount it in the normal way
- Boot your kvm OS from a ISO file containing Gparted
I tried the second approach, and used a ubuntu install to boot (using virt-manager, this is really easy to do). And resized the partition to my need .. simply reboot and “tada” :)
Enjoy disk ?
Can you do it faster by replacing the second step with
cat temp.img >> foo.img
?
Can you do it even faster by enlarging the first file in-place with
dd if=/dev/null of=foo.img bs=1b seek=20G count=0
(in this case the 20G is the new total size, not the increment!)?
For Marius:
cat temp.img >> foo.img
Yes, you can do this, but this would delete the backup too ..
For the second way, I don’t think this will work because enlarge it with /dev/zero (not null, i think).. and this not a valid partition..
Bye
Great post.Thanks alot..That article helps me out.Before i had some problems with it.
Greetings from Germany
Much faster way, using dd:
dd if=/dev/zero of=foo.img bs=1 count=1 seek=$(($(stat -c%s foo.img)+10*1024**3-1))
Obviously your method (and this) only work when dealing with raw images.
See also : http://wiki.libvirt.org/page/Tips#Increasing_the_disk_size_of_a_virtual_machine
There are much much simpler ways exist. Modern coreutils has an utility named truncate – it can “truncate” afile to _larger_ size. So, provided you had image.raw of size N GB, and you want it to be N+M Gb, you use truncate –size $((N+M))G image.raw. _Much_ safer and simpler… ;)
Hey why aren’t you mentioning the partition needs to be resized to fit the new “drive” ?
OK I guess you are but you need to read it a couple of times IMHO to understand that fact..
Also, you can execute:
resize2fs foo.img 5G
qemu-img resize foo.img +10G
truncate -s +15G image.raw
is safest (note the +, which guarantees you will only enlarge the image).