Extracting qcow2 image to a smaller real drive

7,648

Before you can put a larger image (512 GiB disk image) on a smaller block device (128 GiB SSD), you have to shrink the image first. You can do this by booting a virtual machine from installation media (for example, ubuntu-14.04.2-desktop-amd64.iso) together with your qcow2 file. Then use GParted to shrink the image to a size smaller than 128 GiB.

Make no mistake in determining the final image size. You need to take the following into account:

  • Reserved space outside partitions. For classic "msdos" partition tables, there is a 512 bytes gap in the beginning of the disk. For modern GPT partitioning schemes, there exists a GPT table at the beginning and the end of the disk.
  • Swap partitions (if any) must be disabled before you can modify it. This can be done from GParted.
  • 128 GiB is not 128 GB. 128 GiB = 137.4 GB and 128 GB = 119.2 GiB (1 GiB = 1024^3 = 1,073,741,824 bytes, 1 GB = 1000^3 = 1,000,000,000 bytes). Watch for this difference when computing new sizes!
  • It is better to be more conservative in your size choice, leaving more space unused. Enlarging is easier than shrinking and reduces the likelihood of having a too large image.
  • Your disk image must reside on a separate disk than the destination disk.
  • Consults the manual pages of the involved tools if you are not sure. Make a backup of your source image just to be sure!

After shrinking the partitions inside the disk image, shut down the virtual machine and shrink the disk image qemu-img resize your.qcow2 128G (128 GiB!). If you are not sure, you can also use the virt-resize(1) program instead.

When your disk image has shrunk (you can verify it with qemu-img info your.qcow2), convert it from qcow2 to your raw disk (replace /dev/sdb by your unused SSD):

qemu-img convert -p -O raw your.qcow2 /dev/sdb

For GPT partition tables, you might have to repair the GPT table at the end of the disk.

Share:
7,648

Related videos on Youtube

Jackson Doak
Author by

Jackson Doak

Updated on September 18, 2022

Comments

  • Jackson Doak
    Jackson Doak almost 2 years

    How can i extract a qcow2 image (512gb, mostly free space) to a 128gb SSD? it will then be used as the boot drive

    • meuh
      meuh almost 9 years
      qemu-img convert might do it.