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,054
  • 3
  • 26
  • 39
  • 3
    Fear is the best protection. – jasonwryan Dec 05 '17 at 02:27
  • You could write a udev rule such that any usb drive is somewhere like `/dev/usbhdd1` or `/dev/usbhdd%d` ... or you could use [etcher](https://etcher.io/) – RubberStamp Dec 05 '17 at 02:49
  • `cat` will be far faster than that `dd` command. – roaima Dec 05 '17 at 05:29
  • @roaima, the actual `dd` command I use has a blocksize of 4M, which goes about as fast as the card can handle. I simplified things for the purpose of asking this question. – Mark Dec 05 '17 at 09:06
  • @Mark take a look at https://unix.stackexchange.com/questions/12532/dd-vs-cat-is-dd-still-relevant-these-days - quite interesting, I think – roaima Dec 05 '17 at 11:24

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.

roaima
  • 107,089
  • 14
  • 139
  • 261