2

I routinely copy disk images to SD cards for use on a Raspberry Pi. The usual way I do this is by dd if=/tmp/filesystem.img of=/dev/sdb, which is only a minor typo away from over-writing my computer's boot drive on /dev/sda.

Is there a safer way to do this, eg. by removing permission to do raw writes to the boot drive?

Mark
  • 4,244

2 Answers2

3

I think simplest solution is not to have do it as root and force SD card to always get the same letter. You can use udev to achieve that.

First use udevinfo to get enough atributes to uniquely identify your card (or cards) then create rule that will assign specific letter and access rights to device created by kernel for your SD card. One of the first links from google

Now if for whatever reason this fails write wrapper script for dd that will check if device's attributes match SD card and only if they do run dd.

0

There's nothing stopping you using a script with the target device built in to it.

#!/bin/bash
#
# Usage:  <script>  <image>
#
img="$1"
dev=/dev/sdb
echo -n "Copy image $img to $dev..."
sleep 5

echo -n " writing..."
cat "$img" >"$dev"
ss=$?

echo " done"
exit $ss

Put that in your $PATH somewhere and ensure it's executable.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287