Skip to content

How to clone Raspberry Pi SD Card on Linux and shrink it to actual size

On Linux you can use dd to make a backup from SD card. Reverse if and of (i.e. to where they point – source and destination) afterwards to restore, but be careful not to restore to a wrong disk. It will be destroyed without a warning!!!

First use fdisk to get the device id of you SD card (check the size)

fdisk -l

then use dd to make a diskimage (change /dev/sdb with what you found with fdisk -l):

dd bs=4M if=/dev/sdb of=image1-`date +%d%m%y`.img

or this to make a compressed image:

dd bs=4M if=/dev/sdb | gzip > image1-`date +%d%m%y`.img.gz

The process will take some time. After the image is created you will notice that it’s too big(size of a SD card). To shrink it we will use a perfect script that I found on github here https://github.com/Drewsif/PiShrink.git called PiShrink.

PiShrink is a bash script that automatically shrink a pi image that will then resize to the max size of the SD card on boot. This will make putting the image back onto the SD card faster and the shrunk images will compress better.

Installation

wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin

Usage example

$ sudo pishrink.sh pi.img
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/loop1: 88262/1929536 files (0.2% non-contiguous), 842728/7717632 blocks
resize2fs 1.42.9 (28-Dec-2013)
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/loop1 to 773603 (4k) blocks.
Begin pass 2 (max = 100387)
Relocating blocks             XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 236)
Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 4 (max = 7348)
Updating inode references     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/loop1 is now 773603 blocks long.

Shrunk pi.img from 30G to 3.1G

It take’s several seconds and you get 3G image out of 30G

Published inBatchLinuxOtherRaspberry PiScriptShell

Comments are closed.