Duplicate Micro SD Cards with Your Raspberry Pi
I've written separate instructions for duplicating SD cards with a Windows computer.
A $150 microSD Card Duplicator
You can buy SD card duplicating machines at prices from several hundred dollars to several thousand. Or, you can duplicate ten or more cards at a time using $150 worth of parts and your Raspberry Pi running Raspbian. (It was $99 in the fall of 2018, but I now recommend USB 3 equipment, which is pricier.) Here's what you need:
- A powered USB 3.0 hub with enough ports for the number of cards you want to duplicate at once. Your Raspberry Pi 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. (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.) About $40.
- 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 $90. Beware: Not all "SD card readers" will also write reliably; be sure you get reader/writer adapters. You need USB 3 for best speed with the Raspberry Pi 4's USB 3 ports. I've tested the one in the link,
- A big microSD card. You will need one micro SD card with enough space to hold the Raspbian operating system and one or more operating system image files. I suggest one that's four times the size of the cards you're working with, so if you want to duplicate 16 GB cards, get a 64 GB card for about $12. (The USB adapter in the previous link is limited to 256 GB. so don't get a really huge card.)
Even after you add a few bucks for shipping you can probably get the parts you need to turn your Raspberry Pi into an SD card duplicator for less than $150.
This process has been tested using a Raspberry Pi Model 3 B+ and Raspbian Stretch 9.4. However, it will run much faster if you have a Raspberry Pi 4 and use a USB 3 port. Please report problems or errors with these instructions to Bob.Brown@Kennesaw.edu.
Overview: Duplicating SD Cards
Thanks very much to Richard Hayler, who revealed the secret of multiple output devices with the dcfldd program.
Note: if you have a laptop or desktop computer running a Debian-like operating system, you can use that and it will possibly be faster than using a Raspberry Pi. You will have to substitute appropriate device names. In particular, /dev/sda is probably your system disk. I have been politic and you have been warned.
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.
- Make an image on the big card from your master SD card.
- Shrink the image and make it auto-resize on first boot.
- Flash your image onto other SD cards using dcfldd, up to ten or more at a time!
It's time to say a word about shrinking the image. There are two reasons for doing that. The first is that it will make a smaller image, and so things will run faster. If you work with 16 GB cards, you can probably shrink the image from 16 GB down to around 6 or 7 GB, so operations will run more than twice as fast!
The more serious problem is that "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. By shrinking the image, you make sure your image will fit when you flash it onto cards that are nominally 16 GB. The auto resize function will give you back the full capacity of the SD card upon the first boot.
The image shrinking process given is for Raspbian only, not NOOBS.
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 Raspbian terminal environment.
0. Install a Little Software
You only have to do this once. Install Raspbian on your big microSD card and boot from it.
There are two bits of software you need that aren't a part of the standard Raspbian
installation. Both software packages go on your large SD card. One is dcfldd, an extension of the dd command that performs the magic of writing multiple devices
at ones. The other is PiShrink, which packages the fiddly operations of shrinking the image file and setting up
the auto resize operation. Install dcfldd by typing:
sudo apt-get install dcfldd
in a terminal window.
Also in a terminal window, install PiShrink by Drewsif by typing these commands:
chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin
1. Make the Perfect SD 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. Remember: Raspbian only, not NOOBS.
2. Make an Image of Your "Master" Card
Boot from the big microSD card. Make sure you have at least as much free space on
the big card as the full size of your "master" SD card. (Erase old image files as
needed.) In a terminal window, type the following:
sudo umount /dev/sd*
sudo dd bs=4096 if=/dev/sda of=myimage.img
Replace "myimage.img" with whatever you want to name your image file. I like to use something with a date, e.g. workshop_20180906.img. For a 16 GB card, this will take about 18 minutes on a Model 3 B+.
Use PiShrink to shrink the image and set up auto resize typing in a terminal window:
sudo pishrink.sh myimage.img
Replace "myimage.img" with whatever you called your image file. This will take only a couple of minutes. PiShrink has options for making a copy of the image file and suppressing the auto-resize function; you don't need them.
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 Raspberry Pi.
To make four copies, type:
sudo dcfldd bs=64k if=myimage.img of=/dev/sda of=/dev/sdb of=/dev/sdc of=/dev/sdd
Change if= to the name of the image you want to flash. To make more than four co pies, add more of=/dev/sdX operands, where X is the next sequential letter.
This takes about eight minutes for one card. It may take as much as 24 minutes for 10 cards and USB 2. Relax and watch the blinking lights.
Last update: 2020-04-09 08:08
Originally published: 2018-09-25