Note: This article is about flashing SD or microSD cards several cards at a time in preparation
for a class or workshop. If what you want to do is flash an image onto one or two
cards, balenaEtcher is the right tool for the job. It's available for Windows, MacOS, and Linux, and
will read compressed images directly. If you need a bunch of copies, read on!
A $150 microSD Card Duplicator
Half a year ago I wrote an article about duplicating microSD cards with your Raspberry Pi. It had a lot of command-line stuff in it, and one person complained that there
was no verification step. (I think modern flash storage is sufficiently robust that
the verification step is redundant, but it does make some people feel better.) I've spent some more time on the problem of duplicating
microSD cards with the goal of doing most of the work on a Windows 10 PC and getting
the benefit of USB-3. Using the equipment in the links below, I can make 10 duplicate
microSD cards from a master image in less than 17 minutes. I can do enough for an
entire class in an hour, including setup time.
So, to use your PC for microSD card duplication, here's what you need:
A powered USB-3 hub with enough ports for the number of cards
Difference between parallel and in-line port arrangementyou want to duplicate at once. Your PC can power a couple of micro SD cards using
its own power supply. To duplicate many at one time, you'll need a hub with a source
of power. $40. (Most USB 3 hubs will do. However, the microSD cards stick out from
the side of the adapters recommended below, so you want a hub with the USB sockets
parallel to each other, like the top image in the picture at the right, not "in a
line," like the lower image.)
microSD card reader/writer USB adapters. You need one of these for each port on your hub. For a 10-port hub, you need ten.
About $80 for ten. Not all "SD card readers" will write reliably, and not all will
operate at USB-3 speeds. The ones in the link are Transcend TS-RDF5 adapters, and
they are the best I've found so far. (A correspondent from Eindhoven, The Netherlands
writes that there's a white version that makes it easier to see the blue activity
light.)
A 16 GB microSD card on which to build your master image. (This page previously recommended
an 8 GB card; the Raspberry Pi OS from the Raspberry Pi Foundation will no longer
fit on an 8 GB card, which makes this process a little more complex.)
A 64 GB microSD card with the Raspberry Pi OS installed. This is your "working" card
and will be needed to shrink an image. Details are below.
Even after you add a few bucks for shipping you can probably get the parts you need
to turn your Windows PC into an SD card duplicator for about $150, and the software
is all freeware.
All of this will run much faster if you can use a Raspberry Pi 4 Model B.
This process has been tested using a using a Lenovo ThinkPad with solid-state disk
and Windows 10. Please report problems or errors in these instructions to Bob.Brown@Kennesaw.edu.
Overview: Duplicating SD Cards
Here's what you're going to do:
Make an SD card that's perfect for your class or other purpose. This will become
your master image.
Set the perfect SD card to auto-resize on first boot. You'll do this on your Raspberry
Pi.
Get an image of your perfect card onto your PC.
Shrink and optionally compress the image.
Flash your image onto other SD cards using ImageUSB, up to ten or more at a time!
Because the Raspberry Pi OS will no longer fit in 8 GB, you will need to make a 16
GB image and shrink it. You have to have an image that's no larger than your target
cards, and "16 GB" cards from different manufacturers may be of slightly different
sizes. If you make your image using a card that's a little bit bigger than average,
then try to flash it onto one that's a little bit smaller, the operation will fail.
The Gory Details
In the material that follows, code, i.e. things you will type as commands or put into files is shown in bold. These directions assume a small amount of familiarity with working in the Raspberry
Pi OS terminal environment.
0. Get a Little Software
0.a. Software for Windows 10 or 11
You'll need a copy of the freeware ImageUSB software. ImageUSB does not need to be "installed," just plop it in a folder where
you'll be able to find it and double-click to run.
Optional: If you also want to make a compressed image you can use with balenaEtcher, you will need 7Zip, possibly the HDD Raw Copy Tool, and a copy of balenaEtcher itself. Use the portable version of the HDD Raw Copy Tool and plop it into the same
folder as ImageUSB. This is all free software.
0.b. Software for the Raspberry Pi
Download the Raspberry Pi OS image (either "wth desktop" or "with desktop and recommended software") and flash
it onto your 64 GB working card. Then install ZeroFree and PiShrink. Pro tip: On your 64 GB working card, open the Chromium browser and navigate to this page.
Then you can use copy/paste to copy the commands from here and paste them into the
terminal window.
To install ZeroFree, type the following commands (in bold) into a terminal window:
sudo apt-get update then sudo apt-get install zerofree
To install PiShrink, type the following into a terminal window: wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh chmod +x pishrink.sh sudo mv pishrink.sh /usr/local/bin
You only need to install ZeroFree and PiShrink once unless you re-flash your working
card.
1. Make the Perfect SD Card
This will be your "master" microSD card. "Perfect" is a matter of application, and
maybe even a matter of taste. For example, if you're going to use Scratch 2 in your
classes, you can decide whether to include Scratch (1) or leave it out. Do this on 16 GB card. You probably want to start with the "Raspberry Pi OS with desktop and recommended
software," then add and remove software until you have the perfect card.
2. Set Up Auto-Resize on Your "Master" Card
Do this only after you have completely customized your "master" card. You have to do this to get back
full use of your 16 GB cards. You'll do this on your Raspberry Pi, running the "master"
card using the command line in a terminal window. You can use copy-paste with the
commands below.
First edit /boot/cmdline.txt and append "init=/usr/lib/raspi-config/init_resize.sh"
to the first (and only) line. Here's a command to do that; you can use copy-paste
to paste it right into the Terminal command line: sudo sed -i '1{s@$@ init=/usr/lib/raspi-config/init_resize.sh@}' /boot/cmdline.txt
Create the "/etc/init.d/resize2fs_once" file. There are three commands in bold below;
the first one is a single, long line. Copy-paste them into Terminal one at a time. sudo wget -O /etc/init.d/resize2fs_once https://raw.githubusercontent.com/RPi-Distro/pi-gen/master/stage2/01-sys-tweaks/files/resize2fs_once
sudo chmod +x /etc/init.d/resize2fs_once
sudo systemctl enable resize2fs_once
Shut down your Pi and remove the microSD card.
Be careful not to boot the "master" card after you've set up auto-resize, or you'll have to do it again.
2.a. [Optional] Zero-fill the Free Space
This is worth doing only if you plan to make a compressed image for balenaEtcher as
described below. This step overwrites the free space on the master card with zeros
to improve compression. You will do this on the Raspberry Pi, booted from a your
64 GB working card.
Boot your Pi from the working SD card. If the program zerofree is not already installed
on the working SD card, install it with as described above. Insert the master SD
card in a USB adapter and plug it in to a USB port; if you have a Pi 4, use a USB
3 adapter and a USB 3 port on the Pi. Be sure there is no other USB storage attached.
Issue the commands: sudo umount /dev/sda* sudo zerofree /dev/sda2
When the zerofree program completes, your card image will be maximally compressible.
3. Make and Shrink an Image of Your "Master" Card using your Raspberry Pi.
You now need to make and shrink an image of the "master" SD card on your Raspberry
Pi. If you used ZeroFree, your Pi is already booted from your 64 GB working card
and the "master" SD card is in a USB adapter. If not, set things up that way.
With the master card in an adapter and plugged into a USB port, make and shrink an
image of it like this:
Replace "your_master" with whatever name you want for your image files. Pro tip: include the date in the file name, e.g. 20210515 to help you keep track of things.
The dd operation will take many minutes, the PiShrink part much less. When the process
is done, you will have an image file perhaps 8-9 GB or more on your working SD card.
You now need to get it onto a PC. Because of file system incompatibilities, the most
straightforward way to do that is to use VNC's file transfer, which is somewhat slow.
If you are handy with the command line, you might use dd to copy the image onto a different 16 GB card in USB adapter, then use HDDRawCopy
to read it onto the PC.
4. Make as Many Copies as You need!
Put the SD cards to receive the copies into USB adapters and plug the adapters into
your powered USB hub. Plug the USB hub into your Windows 10 PC. Be sure you don't have any USB devices you don't want to flash.
Start ImageUSB. Click "Select all" to select all the USB adapters in Step 1. Select
"Write image to USB Drive" in Step 2. Select your image file ifrom Step 3, and click
"Write."
This takes about twelve minutes for one card. It may take as much as 20 minutes for
10 cards. Relax and watch the blinking lights.
Making an Image for balenaEtcher
This part is optional; do it only if you need an image that will work with balenaEtcher.
You might want to do this yourself in case you need extra copies later on, or even
to make the image file available to students or others.
Navigate to the folder holding your image and right-click the file. Choose 7Zip from
the context menu, then "Add to archive." Choose "xz" for the archive type, then "OK."
This will take 45 minutes or more, so plan to take a break. (It will be much faster
if you have an SSD.) It will leave you with an XZ file that will be less than 2 GB.
The compressed file can be used directly as input to balenaEtcher.
When the compression task finishes, you can delete the .img file; you can recreate
the image file from your master microSD card.