perelo
perelo

Reputation: 495

how to get the serial number from the USB device on Unix-like systems

The binary file is located on a USB key used as a dongle.

I need to get a unique identifier from the key. VendorID + DeviceID is out of the question because they may be the same if the USB key's models are the same.

Using Windows, I use a the function GetVolumeInformation() which gives me a serial number that is changed eachtime the device is formated (not a problem).

Using Unix, to get the same serial number, I need to read the corresponding mount file in the /dev/ directory. But this operation requieres a root access, or at least I need to be part of a specific group.

The unique identifier could be different than the serial number found on Win32 systems. But it must be different for each USB key I will use for delivery.

Any ideas ?

Upvotes: 1

Views: 4207

Answers (3)

perelo
perelo

Reputation: 495

I have managed to retrieve the serial number by getting the /dev file using df command

Than used this code and modified it a little bit :

USB-drive serial number under linux C++

3rd answer (Orwellophile's)

I'm not sure this will work on every Unix systems, but it is fine for now.

Upvotes: 0

ArtifTh
ArtifTh

Reputation: 11

You can look in /dev/disk/by-uuid/ directory. There are symlinks to device nodes, and symlinks names is the IDs of partitions.

Upvotes: 1

mistalee
mistalee

Reputation: 851

Here's some ideas:

  • Is the device auto-mounted on the system? If so, it might get mounted with its device name by the automounter, and the mount command will tell you the device name. E.g. something gets mounted to /media/ABCD-1234 then ABCD-1234 is the device name as provided in the partition table.
  • In the /sys filesystem exists some information regarding the device. One is the size, for each partition. You could put several partitions on the device and use one unformatted partition (which will not get mounted) with a specific size. Then the size of that partition would be your serial number, available e.g. by /sys/block/sdb/sdb1/size, which is world-readable.

Upvotes: 0

Related Questions