Score:0

shrink ext4 boot partition inside qemu image

nl flag

I need to reduce my bootable qemu image (which downloaded from glance) virtual size from 12G to 2G:

$ qemu-img info w.img
image: w.img
file format: raw
virtual size: 12 GiB (12884901888 bytes)
disk size: 1.67 GiB
$ sudo virt-df -h w.img
Filesystem                                Size       Used  Available  Use%
w.img:/dev/sda1                           11G        1.4G        10G   13%
w.img:/dev/sda15                          104M       5.2M        99M    5%
$ sudo virt-resize --shrink /dev/sda1 w.img smaller.img
[   0.0] Examining work.img
virt-resize: error: /dev/sda1: This partition contains a ext4 filesystem
which will be damaged by shrinking it below 12304109568 bytes (user asked
to shrink it to 2029748224 bytes).  If you want to shrink this partition,
you need to use the ‘--resize-force’ option, but that could destroy any
data on this partition.  (This error came from ‘--shrink’ option on the
command line.)

If reporting bugs, run virt-resize with debugging enabled and include the
complete output:

  virt-resize -v -x [...]

And i need to not destroy it. Is it possible?

Score:1
sl flag

First of all - in any such tasks backup your file in advance as there just are too many rough edges and things could go wrong.

To reach your target of "shrink the disk" you'd need to free space from the end of it, but your case needs some steps to achieve that from what I can see.

First of all you need to know how to use this as a block device but do NOT mount it as all of the following will need it unmounted. For that - as it is a raw file - use losetup as explained here.

Then you need to shrink the ext FS on sda1 via resize2fs as explained here. This will make space at the end of sda1.

Next you have the extra problem that your boot partition is after the data partition, so get space at the end that you can then shrink you also need to move that e.g. as explained here.

Then you have space free at the end which allows you to resize via qemu-img resize --shrink as explained on the man page.

Finally since you moved the boot partition you might need to fix up things of the boot loader (not always true, but could apply in your case). For that finally mount your disks make sda15 to be /boot under the root FS of sda1. then chroot into this environment and run sudo update-grub to ensure all meta entries are in the right spot. This last step burdensome and not always needed (depends on too many details). Only go for that if the result of the former step won't boot by not finding the bootloader.

Overall there is a lot of complexity and potential dataloss to it, so maybe backup between each step?

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.