Question

How can I programmatically enumerate all the USB flash drives using libudev under Linux? I want to get the strings such as '/dev/sdb4' so that I can then use 'mount()' to mount them.

I was following the libudev example at http://www.signal11.us/oss/udev/. The example code works well for 'hidraw'; I then tried 'usb', 'scsi_disk', 'scsi_generic', etc, but didn't work - it didn't list the 'device path' like '/dev/sdb4', it said 'Device Node Path: (null)'.

Was it helpful?

Solution

Instead of the scsi_generic search for the block sub-node. There you should find the block device.

scsi_generic is a different interface.

I'll add a bit of explanation, but note that I'm no expert in the subject, just an observer...

SCSI devices are quite complex and have different several interfaces to access different feature sets.

The scsi_generic device is a character device (usually named /dev/sg<n>) that gives access to SCSI features. For example, CD/DVD recorders are (were?) usually managed through this device. But access to the blocks of data is used through the standard block devices, so each SCSI device that looks like a disk (remember, there are also SCSI printers...) will create also a block sub-node.

But a USB flash drive is not a real SCSI device, it just uses the same protocol, quite simplified. So, to avoid rewriting the same protocol for USB, the kernel creates a virtual SCSI device and connects it to the USB device.

The thing with this virtual SCSI devices is that they have the minimum necessary to present themselves as a block device. So the lack many of the SCSI specific features, particularly the scsi_generic interface. That's why you get a (null) there. Not that it could be useful to you, as you cannot mount a scsi_generic device, as it is not a block device at all!.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top