Skip to content

Instantly share code, notes, and snippets.

@yordanoweb
Created January 23, 2026 13:30
Show Gist options
  • Select an option

  • Save yordanoweb/91791a6a006fdb6714252d4e114c48d9 to your computer and use it in GitHub Desktop.

Select an option

Save yordanoweb/91791a6a006fdb6714252d4e114c48d9 to your computer and use it in GitHub Desktop.
Copy with dd to SDCARD/USB

Copying Large Images (>8GB) to SD Card

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.

Prerequisites

  • Backup SD card data.
  • Download pi-shrink: git clone https://github.com/Drewsif/PiShrink.git or grab pi-shrink.sh.
  • Make executable: chmod +x PiShrink/pi-shrink.sh.
  • Have .img file ready.

Step 1: Wipe SD Card Partitions

sudo fdisk /dev/mmcblk0
  • d: Delete each partition.
  • w: Write and exit.

Step 2: Reboot to Refresh Kernel

sudo reboot

Step 3: Shrink Image with Pi-Shrink

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 with ls -lh).
  • Flags: --resize (default), --skip-first (if skipping boot partition resize).

Use output_shrunk.img in later steps.

Step 4: Mount Shrunk Image as Loop

sudo losetup --find --partscan --show /path/to/output_shrunk.img
  • Note output (e.g., /dev/loop0).

Step 5: Copy to SD Card

sudo dd if=/dev/loop0 of=/dev/mmcblk0 bs=8M status=progress conv=fsync

Verification

  • sudo fdisk -l /dev/mmcblk0.
  • Cleanup: sudo losetup -d /dev/loop0; sudo sync.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment