Clone/Resize a QCOW or VMDK File

With guestfish it is possible to create a copy of a vmdk or qcow in an automated fashion. Using this recipe you will be able to resize your image while keeping the contents the same. Note that the original qcow or vmdk is unchanged by this script. This script can be quite time-consuming; hence if you simply wany an *exact copy* of your image it would be much more efficient to simply use cp. The intent here is to give you the possibility to resize the filesystem in the new image or even edit the new image on the fly using guestfish commands. You may also want to take a look at the official guestfish clone recipe which shows how to edit the contents of the new image.

clone.sh

Sample Usage: clone.sh original_image_file new_image_file 10000M

#!/bin/sh -

oldimage="$1"
newimage="$2"
imgsize="$3"


guestfish -a "$oldimage" <<EOF
run
mount /dev/sda1 /
tgz-out / /tmp/out.tgz
umount-all
quit
EOF

guestfish <<
sparse "$newimage" "$imgsize"
run
part-disk /dev/sda mbr
mkfs ext3 /dev/sda1
mount /dev/sda1 /
tgz-in /tmp/out.tgz /
write-file /boot/grub/device.map "(hd0) /dev/vda" 0
grub-install / /dev/sda
umount-all
quit
EOF

The new_image_file will be a sparse file in "raw" format with a capacity equal to that specified in the final argument. To convert it to a vmdk or qcow2 use "qemu-img convert", eg:

qemu-img convert -f raw new_image_file -O vmdk new_vmdk_file
or
qemu-img convert -f raw new_image_file -O qcow2 new_qcow_file


© 2010-2013 Stacklet LLC - Privacy