3

In Windows, you can get GetDriveType return value to determine USB Hard Disk Drive or USB flash drive.

DRIVE_REMOVABLE ==> USB flash drive

DRIVE_FIXED ==> USB Hard Disk Drive

How should I do it in linux?

how should I do it from a developer's point of view ?

kangear
  • 2,493
  • 2
  • 31
  • 44

2 Answers2

3

You need to install lshw command using apt-get or yum command. To display all disks and storage controllers in the system, enter:

# lshw -class disk -class storage

later you can also try the following, it will give complete details for particular...

# hdparm -I /dev/sda

OR

$ sudo hdparm -I /dev/sda
Imran
  • 5,376
  • 2
  • 26
  • 45
  • 1
    If you want to see the C instructions (ioctl) necessary you may use "strace" to see all ioctl()s used by "hdparm": "strace hdparm -I /dev/sda" – Martin Rosenau Nov 11 '13 at 10:22
  • BTW: I doubt that all flash drives identify themselfes correctly so it may be possible that some flash drives identify themselfs as hard drives. Background: You cannot remove the flash drive (USB stick) from the SCSI controller so the device is not removable! An USB SD card reader - in contrast - allows detaching the media from the SCSI controller. – Martin Rosenau Nov 11 '13 at 10:24
  • I understand what you mean, I'll see you said content. But having said that, it is not the same as in Windows API do? This is dependent on the specific implementation SCSI drives do? In the programming time can not get it right directly accurate ? just like Windows "GetDriveType" API. – kangear Nov 12 '13 at 01:16
2

Well, there is a "removable" flag implemented as a file underneath `/sys but....

For example, on my system, this file underneath /sys represents disk /dev/sde implemented by an attached USB hard disk (the USB device found on "bus 1 -> port 2 -> config 1 -> interface 0")

/sys/devices/pci0000:00/0000:00:02.1/usb1/1-2/1-2:1.0/host9/target9:0:0/9:0:0:0/block/sde

The subpath

/sys/devices/pci0000:00/0000:00:02.1/usb1/1-2

which is the USB device found on "bus 1 -> port 2" seems to represent the USB device itself.

Let's look for the "removable" files using find and print their contents using cat:

$ cd /sys/devices/pci0000:00/0000:00:02.1/usb1/1-2

$ find . -name removable -exec echo -n '{}: ' ";" -exec cat '{}' ";"

./1-2:1.0/host9/target9:0:0/9:0:0:0/block/sde/removable: 0
./removable: unknown

So, the "device" is not known to be removable, and the "disk" is not removable (assuming '0' means 'no'). Not very helpful.

David Tonhofer
  • 14,559
  • 5
  • 55
  • 51