Creating and Shrinking Raspberry Pi Image using SD Card

Photo by Harrison Broadbent on Unsplash

Suppose you are working with Rasbian OS on Raspberry Pi and you make some changes to the OS by installing some packages, installing your services, and adding custom boot scripts. Once you are done with your changes, you might want to package everything and share/distribute your work as a img file. So that if anyone burns the img on their SD card, they will get the same environment as the one you created.

Initially, this problem seems quite simple, as you can simply put your SD card in a card reader and use some software to generate a img file. But the issue with this approach is that the generated img file’s size will be equal to the size of the SD card. We need to shrink the img to remove all the empty space so that the resulting img file is smaller.

Let us get started and see how we can do it.

Step 1: Create a img using the SD card

Pull out the SD card from your Raspberry Pi and put it in a card reader attached to your laptop.

MacOS

If you are using a Mac, use diskutility to check the attached SD card. Run the following command to list the attached devices.

diskutil list
SD card

Based on the size, you might be able to identify the SD card. Take note of its mount path. For me it was /dev/disk7 .

Next, run the following command to create a img from the bootable SD card.

sudo dd if=/dev/disk7 of=/Users/vivekmaskara/Downloads/Rpi.img

The above command doesn’t display any progress details and it might take 10–20 minutes to finish.

Once the image is generated, you will see an output similar to the one shown below.

Other OS

Refer to this tutorial for instructions on how to generate a img .

Step 2: Shrink Image

Next, we will shrink the generated image. We will use PiShrink to shrink the image.

Prerequisite

You might run into issues installing PiShrink on your system if you are not a Linux user. So I recommend using a dockerized version of PiShrink. So install Docker on your system if it is not already installed. Follow these instructions to install docker.

Shrinking image using PiShrink

Finally, we can shrink the generated img using a dockerized version of PiShrink. First, navigate to the directory containing the img file and then run the following command.

docker run --privileged=true --rm \
--volume $(pwd):/workdir \
mgomesborges/pishrink \
pishrink -pZv GramsRpi.img GramsRpiShrinked.img

Once the shrinking process completes, you will see a output similar to this:

PiShrink output

Step 3: Test your Shrunk image

Finally, test your shrunk image by burning it to an SD card using any image-burning software you prefer. I used Etcher to burn the image back to the SD card.

That's it for this post. Please leave a clap if you found this article useful. Consider subscribing to Medium to read more of my stories.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store