Shrinking and expanding the partition size of an SD card

Posted by:

|

On:

|

When I began my internship at a company based in Hong Kong, my first assignment was to streamline the process of copying code and Ubuntu from a 128GB SD card to 100 new SD cards for the front-end engineers. We were using Win32diskimager on a Windows computer to create a raw image file of the SD card and then write that image to the new SD cards. However, it took approximately 45 minutes to write a 128GB image to a new SD card, which meant it would take a whopping 75 hours to transfer all 100 SD cards. It was an absolute nightmare!

Solution

The solution that came to mind was to shrink the image size to expedite the process. I researched various software tools like MiniTool Partition Wizard (a Windows application) and GParted (a Linux application). Unfortunately, MiniTool couldn’t handle shrinking the ext4 file type, so I turned to GParted. With GParted, I successfully reduced the SD card partition to 3GB before creating a 3GB image backup. This improvement significantly accelerated the speed, allowing me to copy the image to a new SD card in less than a minute.

New problem

However, a new issue emerged—the active partition on the new SD card remained at 3GB, which fell far short of the nearly 100GB storage requirement for the Orange Pi. Expanding the active partition to its full size using GParted was technically possible but cumbersome, necessitating constant switching between the Windows and Linux systems. Moreover, the task of performing this operation on 100 SD cards seemed overwhelmingly daunting.

Task accomplished finally

Fortunately, I eventually stumbled upon the “resize2fs” tool within Ubuntu. This tool proved invaluable as it allowed me to adjust the ext4 file system size on the SD card to match its full capacity. To streamline the process, I created a bash script incorporating the necessary Ubuntu commands.

The related post below provides a detailed procedure on how to accomplish it.

Posted by

in