How to shrink raw qemu / kvm images

From vpsget wiki
Revision as of 16:11, 30 September 2015 by Ndi (talk | contribs) (Created page with "[DRAFT] rewrited/copied from http://www.blog.turmair.de/2010/11/how-to-shrink-raw-qemu-kvm-images/ If you allocated to much hard disk space to your virtual machines and you...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

[DRAFT] rewrited/copied from http://www.blog.turmair.de/2010/11/how-to-shrink-raw-qemu-kvm-images/

If you allocated to much hard disk space to your virtual machines and you’re running out of free space on your storage device, here’s how to reclaim space from your existing virtual machines.

First, shrink and realign the partitions on the image file as you see fit. In Linux, you may use a liveCD and the command resize2fs to shrink the root partition. Starting with Windows 2008, this is easily possible using the Management Console.

When you’re done, shutdown the machine.

On your virtualization platform, setup the image as loopback device:

losetup -fv /path/to/image.raw
# 
Loop device is /dev/loop0

Then, find out the sector size and last used sector of the last partition(marked in red):

fdisk -cul /dev/loop0 
#
Disk /dev/loop0: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
Units = sectors of 1 * 512 = <red>512</red> bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1449bcb0
Device Boot      Start         End           Blocks   Id  System

/dev/loop0p1 * 2048 11452502 5725227 7 HPFS/NTFS /dev/loop0p2 11452503 42432503 15490000 7 HPFS/NTFS