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?

5
  • 3
    Fear is the best protection. Commented Dec 5, 2017 at 2: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 Commented Dec 5, 2017 at 2:49
  • cat will be far faster than that dd command. Commented Dec 5, 2017 at 5: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. Commented Dec 5, 2017 at 9:06
  • @Mark take a look at unix.stackexchange.com/questions/12532/… - quite interesting, I think Commented Dec 5, 2017 at 11:24

2 Answers 2

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.