I'm automating SD card imaging from an existing dd factory image. The SD card is always connected through an external USB card reader and thus appears in the system as a SCSI block device /dev/sd*.

Currently the syntax of my command is: write-image DEVICE where DEVICE is the SD card block device, eg. /dev/sdd.


I'm already doing a basic check on DEVICE to verify it is of the form /dev/sd* but this is not enough: I fear the users (production people not used to Linux) make a mistake and specify another seemingly valid device, eg. /dev/sda. You can see the looming catastrophe, especially since my imaging script needs root privileges (not to write the image itself, mind you, but to modify the SD card afterwards, including adjusting a partition's size depending on the SD card's real size)...


I would like to verify that the specified device actually is some USB mass storage (or at the very least a removable device) so that I can protect the system disks from being trashed accidentally. How can I do that?

I found nothing relevant in /proc or on the web, I'm quite at loss now.

1 Answer 1


Have a look under the /sys/ directory. In particular, /sys/block/ contains symlinks to block devices in /sys/devices/.

/sys/block/sdX/removable looks like it will read as 1 for a removable device, and 0 otherwise. This gives you a basic check for removability.

I'm not sure if there's a better way to check if it's a USB device, but readlink /sys/block/sde will spit out something like ../devices/pci0000:00/0000:00:1d.0/usb6/6-1/6-1.2/6-1.2:1.0/host7/target7:0:0/7:0:0:0/block/sde. Checking if that contains a usb* folder might work as a simple check.

You can get other device details like vendor and model from /sys/block/sdX/device/, which might also come in handy.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .