How to shrink a bootable disk image

Wed Nov 10 2021 00:00:00 GMT+0000 (Coordinated Universal Time)

This method worked for me. I had a 64GB disk image made from a botable USB stick, and I wanted to shrink it down so that it could fit on 16GB memory sticks.

Short summary in case of bit rot:

sudo umount <the device from lsblk>
dd if=<the device from lsblk> of=/path/to/file/myimage.img
sudo modprobe loop
sudo losetup -f

That would give you the next free loop device number. If it were 25, then:

sudo losetup /dev/loop25 myimage.img
sudo partprobe /dev/loop25
sudo gparted /dev/loop25

Resize inside of Gparted


sudo losetup -d /dev/loop25
fdisk -l myimage.img

Check the end block and the block size from that command. If end block is 9181183 and block size is 512, then issue:

truncate --size=$[(9181183+1)*512] myimage.img