Make a Clone of Customized Raspbian OS + OpenCV

Victor Fernandez
3 min readApr 19, 2022
Photo by Viktor Talashuk on Unsplash

The Problem:

To run OpenCV on my Raspberry Pi, I need to build and compile it on the Pi (a very time-consuming task). So if the SDCard fails, I will lose the instance and the time spends on the build and compilation.
I need a way to “preserver” the instance.

The Solution:

Create a copy of the Raspbian containing the build and compiled software.

The Implementation:

I will use a Linux machine, GParted, and a Bash script to clone, shrink and compress the instance of Raspbian containing OpenCV.

GParted is a partition-editing application, used for creating, deleting, resizing, moving, checking, and copying disk partitions and their file systems.

The final result will be a compressed file like this:

The steps

  1. Pre-Requisites.
  2. Prepare the environment.
  3. Making a Clone of the custom Raspbian OS.
  4. (optional) compress the image further.
Photo by Dayne Topkin on Unsplash

1. Pre-Requisites

  1. A Linux machine, not the Raspberry.
  2. A working instance of the Raspbian OS.

2. Prepare the environment

  1. Install gparted
sudo apt-get install gparted

2. Using a USB adapter connect the SDcard to the Linux machine.

3. Making a Clone of the custom Raspbian OS

1. Find the USB with the Raspbian OS.

sudo fdisk -l

2. Clone the Raspbian OS.

sudo dd if=/dev/sdb of=/your/path/to/clone.img

where:

if Stand for the Input file, this is the path to the USB with the Raspbian OS.

of Stand for the Output file, where we will save the clone OS

🔥 This process might take between 20 and 40 minutes.

3. Shrinking the image

The clone can be shrunk further ( in some cases the clone can be 16GB), so it is a good idea to shrink it.

For the process, one can use the script pishrink by Drewsif

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

to execute the script

sudo pishrink.sh /your/path/to/clone.img /your/path/to/shrink_image.img

4. (optional) Compress the image further

gzip -9 /your/path/to/shrink_image.img
Compress copy of Raspbian with OpenCV.

Final Notes

  • I’m looking for ways to make the clone image smaller, so I can store more versions in a USB, sort of an archive.
  • I use a Raspbian version with a desktop environment, but the process for instances without the desktop is the same and the resulting image might be smaller.

Victor Fernandez

--

--

Victor Fernandez

I’m Victor, I’m a Field Application Engineer for a CCTV manufacturer. I love Raspberry Pi, Python, and Microcontrollers and I write about my personal projects.