Skip to content

Instantly share code, notes, and snippets.

@CandleHater
Last active February 9, 2020 23:24
Show Gist options
  • Save CandleHater/79e1b1e63cc6bbc999651ffc1a8fdfbd to your computer and use it in GitHub Desktop.
Save CandleHater/79e1b1e63cc6bbc999651ffc1a8fdfbd to your computer and use it in GitHub Desktop.
Create an image from a SD card and compress it.

Config

Make sure to set the variables DEVICE and IMAGE_FILE according to your needs.

# check the disks
sudo fdisk -l

# example
IMAGE_FILE=/home/$USER/Desktop/pi-init-`date +%Y%m%d`.img
DEVICE=sdX

Generate Image

sudo umount /dev/$DEVICE?*

sudo dd if=/dev/$DEVICE of=$IMAGE_FILE bs=4M status=progress
sudo chown $USER:$USER $IMAGE_FILE

Compress Image

# download pishrink.sh
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh -O /tmp/pishrink.sh
chmod +x /tmp/pishrink.sh

# -z: gzip compress image after shrinking
# -p: remove logs, apt archives, dhcp leases and ssh hostkeys
sudo /tmp/pishrink.sh -z $IMAGE_FILE

sudo chown $USER:$USER $IMAGE_FILE.gz

Write Image to SD

# .img
sudo dd if=/dev/$IMAGE_FILE of=/dev/$DEVICE bs=4M conv=fsync status=progress

# .zip
unzip -p $IMAGE_FILE | sudo dd of=/dev/$DEVICE bs=4M conv=fsync status=progress

# .gz
gunzip -c $IMAGE_FILE | sudo dd of=/dev/$DEVICE bs=4M conv=fsync status=progress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment