This updated guide uses the pi-shrink script from GitHub instead of qemu-img for shrinking the .img. Pi-shrink intelligently compacts Raspberry Pi OS images by zeroing free space and resizing partitions/filesystems. Warning: These operations erase all data on the SD card (/dev/mmcblk0). Verify the device with lsblk first.
- Backup SD card data.
- Download
pi-shrink:git clone https://github.com/Drewsif/PiShrink.gitor grabpi-shrink.sh. - Make executable:
chmod +x PiShrink/pi-shrink.sh. - Have
.imgfile ready.
sudo fdisk /dev/mmcblk0
d: Delete each partition.w: Write and exit.
sudo reboot
Run pi-shrink on the .img to minimize its size.
sudo ./PiShrink/pi-shrink.sh /path/to/image.img output_shrunk.img
- Automatically detects Raspberry Pi images, zeros free space, resizes rootfs to used space.
- Output is
output_shrunk.img(smaller; verify withls -lh). - Flags:
--resize(default),--skip-first(if skipping boot partition resize).
Use output_shrunk.img in later steps.
sudo losetup --find --partscan --show /path/to/output_shrunk.img
- Note output (e.g.,
/dev/loop0).
sudo dd if=/dev/loop0 of=/dev/mmcblk0 bs=8M status=progress conv=fsync
sudo fdisk -l /dev/mmcblk0.- Cleanup:
sudo losetup -d /dev/loop0; sudo sync.