Resize (shrink) qemu image
Submitted by cv_ops on Tue, 12/01/2009 - 7:17pm.
I tried to resize a Centos 5.4 image from 10G to 1G:
losetup -o 32256 -f --show centos.5-4.x86-64.qemu.img
e2fsck -f /dev/loop1
resize2fs /dev/loop1 1G
losetup -d /dev/loop1
dd if=centos.5-4.x86-64.qemu.img of=centos-small.img bs=1M count=1001
However the resulting image won't boot with qemu-kvm. I get a "GRUB Geom Error"
Any other way to do this?
Second try:
I downloaded the latest alpha code release from bitbucket to try and build the Centos 5.4 x86_64 image myself.
I had to add centos.5-4.x86-64 to the assets directory. However when I grabbed the files here from bitbucket (corePackages etc), the corePackages file was wrong (it was an XML file instead of a line-by-line list of packages). I copied the corePackages from the centos.5-4.x86 assets directory and replaced i386 with x86_64. Of course this didn't work either -- I had to change i686 to x86_64 also.
Anyway at long last I got a 1000M image in /var/stacklet. I tried booting it with qemu-kvm. Unfortunately this time it jumps straight to the GRUB prompt (grub>). If I type "boot" at this prompt, grub complains:
Error 8: Kernel must be loaded before booting.
The kernel is definitely there in /boot/ inside the image (so is the initrd etc).
Hi,
Going back to your first post, you just need to rerun grub on the resized image after the steps you outlined:
/bin/echo -e "root (hd0,0) \n setup (hd0)" | grub --config-file=/boot/grub/grub.conf --batch --device-map=centos-small.img.device.map
The device map file (centos-small.img.device.map) is simply a text file that looks like:
(hd0) /path to/centos-small.img
Obviously replace the "path to" to wherever you have the file stored.
Rerun grub works
Thanks
Couple of mistakes I made:
1. Needed to resize the primary partition within the image file before doing the dd. I used parted. Is there any other way?
2. resize2fs 1G = 1024M, I think, so the resize should be more like 960M