If you have a Raspberry Pi, you may need to know a bash script called PiShrink, which makes the Raspberry Pi image smaller. PiShrink will automatically shrink the image and then resize it to the maximum size of the SD card at startup. This will copy the image to the SD card faster, while the smaller image will be better compressed. This is very useful for putting a large-capacity image on an SD card. In this short guide, we will learn how to shrink the Raspberry Pi image to a smaller size on Unix-like systems.

Install PiShrink

To install PiShrink on a Linux machine, first download the latest version using the following command:

wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh

Next, add executable permissions to PiShrink:

chmod +x pishrink.sh

Finally, move to the directory:

sudo mv pishrink.sh /usr/local/bin/

Make the Raspberry Pi image smaller

As you probably already know, Raspbian is the official operating system for all Raspberry Pi models. The Raspberry Pi Foundation has developed Raspberry Pi desktop versions for PC and Mac. You can create a live CD, run it in a virtual machine, or even install it on your desktop. The Raspberry Pi also has a small number of unofficial operating system images. For testing, I downloaded the official Raspbian system from the official download page.

Extract the downloaded system image:

unzip 2019-04-08-raspbian-stretch-lite.zip

The above command will extract the contents of the 2019-04-08-raspbian-stretch-lite.zip file in the current directory.

Let's look at the actual size of the extracted file:

du -h 2019-04-08-raspbian-stretch-lite.img
1.7G 2019-04-08-raspbian-stretch-lite.img

As you can see, the extracted Raspberry Pi system image size is 1.7G.

Now use PiShrink to reduce the size of this file as follows:

sudo pishrink.sh 2019-04-08-raspbian-stretch-lite.img

Example output:

Creating new /etc/rc.local
rootfs: 39795/107072 files (0.1% non-contiguous), 239386/428032 blocks
resize2fs 1.45.0 (6-Mar-2019)
resize2fs 1.45.0 (6-Mar-2019)
Resizing the filesystem on /dev/loop1 to 280763 (4k) blocks.
Begin pass 3 (max = 14)
Begin pass 4 (max = 3728)
The filesystem on /dev/loop1 is now 280763 (4k) blocks long.
Shrunk 2019-04-08-raspbian-stretch-lite.img from 1.7G to 1.2G

As you can see in the output above, the size of the Raspberry Pi image has been reduced to 1.2G.

You can also use the -s flag to skip the auto-expansion part of the process.

sudo pishrink.sh -s 2019-04-08-raspbian-stretch-lite.img newpi.img

This will create a copy of the source image file (i.e. 2019-04-08-raspbian-stretch-lite.img) into a new image file (newpi.img) and process it. For more details, check out the official GitHub page.

Last modified: February 15, 2020



Leave a Reply