Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Given a device file, say /dev/sdb, is it possible to determine what driver is behind it?

Specifically, I want to determine what driver my storage devices are using. fdisk -l lists 2 devices: /dev/sda and /dev/sdb. One is a SATA hard drive and the other is a USB Mass Storage device - actually an SD card.

How do I determine, programmatically, which is which?

I am writing a piece of software, and I want to protect the beginner from obliterating their hard drives, whilst allowing them to obliterate their SD cards.

share|improve this question
    
I assume you're running Linux, based on your choice of device name. –  Gilles Aug 11 '12 at 15:28
    
@Gilles Yeah, sorry that's probably quite an important piece of information... Thanks! –  Alex Chamberlain Aug 11 '12 at 16:07
add comment

1 Answer

up vote 11 down vote accepted

Run udevadm info -a -n /dev/sda and parse the output. You'll see lines like

DRIVERS=="ahci"

for a SATA disk using the ahci driver, or

DRIVERS=="usb-storage"

for an USB-connected device. You'll also be able to display vendor and model names for confirmation. Also,

ATTR{removable}=="1"

is present on removable devices.

All of this information can also be obtained through /sys (in fact, that's where udevadm goes to look), but the /sys interface changes from time to time, so parsing udevadm is more robust in the long term.

share|improve this answer
    
@AlexChamberlain Sorry, yes, -n expects a path to a block device (something under /dev) whereas -p expects a path under /sys (e.g. -p /sys/block/sda). –  Gilles Aug 12 '12 at 18:11
    
Thanks @Gilles. Much appreciated. –  Alex Chamberlain Aug 12 '12 at 19:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.