How to shrink KVM/qemu partition and image size
This article is about shrinking a filesystem and partition of a qemu kvm supported image file.
Case:
VM Disk image file name is vol1.raw
Image file is a raw format file, so if you have a qcow2 format image file convert first in raw format using bellow command -
# qemu-img convert vol1.qcow2 vol1.raw
just for info if you wish to convert back :)
# qemu-img convert -p -f raw -O qcow2 vol1.raw vol2.qcow2
Now coming to shrinking part .
collect the filesystem consumed with df -h command
so assume in my case i have 1.2 Gb used in OS out of 10GB and rest 8.8 Gb is free. Now i want the size of disk image to be as 3 GB only
We need another kvm VM in order to attach this raw image and perform file system shrink
Let assume this raw image is attached as /dev/vdb
first perform a force filesystem check
# e2fsck -f /dev/vdb1
Now resize the filesystem ( which is ext4 in my case. xfs cannot be reduced )
# resize2fs /dev/vdb1 2G ( exact size of partition/disk that you want )
now shrink the raw format disk image file size . Please add extra 1 GB in above resize2fs size command otherwise system would not boot.
# qemu-img resize --shrink vol2.raw 3G ( exact size of image that you want - partition size +1G i.e. in my case 2+1=3GB)
and done.
Now if you wish convert from raw to qcow2 with command given above in post.
Case:
VM Disk image file name is vol1.raw
Image file is a raw format file, so if you have a qcow2 format image file convert first in raw format using bellow command -
# qemu-img convert vol1.qcow2 vol1.raw
just for info if you wish to convert back :)
# qemu-img convert -p -f raw -O qcow2 vol1.raw vol2.qcow2
Now coming to shrinking part .
collect the filesystem consumed with df -h command
so assume in my case i have 1.2 Gb used in OS out of 10GB and rest 8.8 Gb is free. Now i want the size of disk image to be as 3 GB only
We need another kvm VM in order to attach this raw image and perform file system shrink
Let assume this raw image is attached as /dev/vdb
first perform a force filesystem check
# e2fsck -f /dev/vdb1
Now resize the filesystem ( which is ext4 in my case. xfs cannot be reduced )
# resize2fs /dev/vdb1 2G ( exact size of partition/disk that you want )
now shrink the raw format disk image file size . Please add extra 1 GB in above resize2fs size command otherwise system would not boot.
# qemu-img resize --shrink vol2.raw 3G ( exact size of image that you want - partition size +1G i.e. in my case 2+1=3GB)
and done.
Now if you wish convert from raw to qcow2 with command given above in post.
Comments
Post a Comment