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.
Best Answer
Run
udevadm info -a -n /dev/sda
and parse the output. You'll see lines likefor a SATA disk using the
ahci
driver, orfor an USB-connected device. You'll also be able to display vendor and model names for confirmation. Also,
is present on removable devices.
All of this information can also be obtained through
/sys
(in fact, that's whereudevadm
goes to look), but the/sys
interface changes from time to time, so parsingudevadm
is more robust in the long term.