Posts: 1
Joined: Wed Mar 13, 2013 9:29 pm

Use Pi to automatically program Sd cards

Wed Mar 13, 2013 9:33 pm

hi all, I would like to program a large quantity of SD cards with a wheezy image I have set up. I have about 50 SD cards to program and was wondering if there was a way I could automate this process using a Pi to write to the cards.

I am looking to have the Pi format and then write the image to the card automatically once the card is inserted into a USB SD adapter. Then possibly use a GPIO pin to flash an LED once the card is successfully programmed.

Ideally this would work so I can run the Pi headless and just insert an SD card, have it written then automatically unmounted once done with the LED flashing so I know it is ready to program the next card.

How hard would a script like this be to write? Currently I am using Rdisk on my macbook to do this but would like to automate the process to make it easier to program large amounts of cards.


Posts: 1395
Joined: Fri Mar 16, 2012 5:20 am
Location: California
Contact: Website

Re: Use Pi to automatically program Sd cards

Thu Mar 14, 2013 2:25 pm

You can probably write a script with all the commands needed to flash the image without too much difficulty, do you know how to write bash scripts? And you put a loop in the script that watches for when you have reinserted another card, and then cycles through the commands to flash another image. The thing that may not be so easy is to have the LEDs flash. Would it work for you to have alsaplayer or your audio player of choice (I would use mplayer) just play a sound clip letting you know when the dd has been completed? Playing a sound file from within a script is pretty simple, if you already have a sound player set up and any sort of speakers. Or a headphone might do.

I am realizing the scripting is easier, but may not be so great if you do not have a headset, speakers, or something else to use to hear the sounds.

I hope someone else can give some examples of how to write a loop that would do this.

I am running a headless server, but it does have speakers attached, which is why I am thinking in these terms.

I think if all your SD cards are the same model, then I would grep for their Manufacturer or ID in the output of lsusb, and test for when the next card shows up. No, it can be done, but not super simple or I would be posting more details. :o) I could do it, but it would take at least a half hour or more.
FORUM TIP: To view someone's posting history, sign in, click on their user name, then on "Search User's Posts." || Running ArchLinuxArm on Model 2B and 512MB Model B

User avatar
Posts: 16983
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Use Pi to automatically program Sd cards

Thu Mar 14, 2013 3:40 pm

I'd be tempted to use udev rules to trigger a bash script that did something like:

Turn LED off (if on)
Write image: dd if=ImageFile of=/dev/sda bs=1m
Verify Image: dd if=/dev/sda bs=1m count=Size | sha1sum
Eject sdcard: eject /dev/sda
Turn LED on

You could also add stuff to expand the root partition to the card size.

Caveat: This would overwrite ANY USB storage device that gets plugged in to that Pi, unless you add additional checks - perhaps for the usb adaptor.

Searching on "writing udev rules" may help, they can be a bit of a minefield at first (I still need to experiment for a while before I get one to work)

Posts: 1132
Joined: Thu Mar 08, 2012 3:32 pm

Re: Use Pi to automatically program Sd cards

Thu Mar 14, 2013 3:46 pm

Will creating a script make that much difference? I use W32diskimager and have to perform two single mouseclicks if I want to write a second card. With 50 cards I'd probably do it while browsing the web or something.

Return to “Advanced users”