Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2019-01

Get "disk" ID

4 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bart

Hi,

I need some function to get a unique ID for a disk. I need this to identify if my program has accessed this disk previously.

On Windows I can retreive the VolumeSerialNr, but how do I do something like that in Linux?
Mind you: the "ID" retrieved does NOT have to be the same for each platform. (I'ld prefer if the "ID" was some kind of integer.)

So if a disk on Windows returns 1234, it is not a problem if on Linux the same disk will return 4567.
The "ID" retrieved however must remeain the same if the disk is removed and inserted at a later point in time (regardless of a reboot of the system in the mean time).
If the disk was formatted before inserting a next time, it will not matter if the "ID" changes.

Preferrably the code for linux would also work on MacOSX.

Bart

Reply by email

Giuliano Colla
to Bart

Il 05/01/2019 17:03, Bart ha scritto:

I need some function to get a unique ID for a disk. I need this to identify if my program has accessed this disk previously.

For that purpose on Linux, out of laziness, I usually just parse the output of command-line utilities, such as blkid or lsblk. blkid /devs/sd/xy/ provides an output such as:

blkid /dev/sda1
/dev/sda1: LABEL="SYSTEM" UUID="980C041F0C03F6D2" TYPE="ntfs"

or

blkid /dev/sda3
/dev/sda3: UUID="ce9d5b2f-232f-4a87-a75e-70e1386dd134" TYPE="ext4"

while lsblk can provide either a single partition UUID (/dev/sd/xy/) or the UUIDS for all the disk partitions (/dev/sd/x/):

sudo lsblk -f /dev/sda3
NAME FSTYPE LABEL UUID MOUNTPOINT
sda3 ext4         ce9d5b2f-232f-4a87-a75e-70e1386dd134 /boot

or

sudo lsblk -f /dev/sda
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT sda
├─sda1 ntfs   SYSTEM      980C041F0C03F6D2 ├─sda2 ntfs   Windows 5E1043661043446D                     /media/Windows ├─sda3 ext4 ce9d5b2f-232f-4a87-a75e-70e1386dd134 /boot ├─sda4
├─sda5 ntfs   HP_RECOVERY 2CF47035F470037E
├─sda6 vfat   HP_TOOLS    7E18-51AB
├─sda7 swap 68647ac3-2d0b-4f71-92dd-65220092bab6 [SWAP] └─sda8 ext4 4ccb4264-9920-4d2b-a568-bfc5024eafcd /

Those utilities are based on the libblkid library, which does the real job. If you prefer an approach which doesn't require launching an executable and parsing its output, you may give a look to the C code of the sources, find the appropriate API's, and then bind what required to your Pascal program.

On Mac OSX you should be able to get the UUID by using diskutil, but I never tested.

Giuliano

Do not do to others as you would have them do to you.They might have different tastes.

Reply by email

Sven Barth
to Bart: I need some function to get a unique ID for a disk. I need this to identify if my program…

Am Sa., 5. Jan. 2019, 17:05 hat Bart geschrieben:

Show quoted text 21 lines
Hi, I need some function to get a unique ID for a disk. I need this to identify if my program has accessed this disk previously. On Windows I can retreive the VolumeSerialNr, but how do I do something like that in Linux? Mind you: the "ID" retrieved does NOT have to be the same for each platform. (I'ld prefer if the "ID" was some kind of integer.) So if a disk on Windows returns 1234, it is not a problem if on Linux the same disk will return 4567. The "ID" retrieved however must remeain the same if the disk is removed and inserted at a later point in time (regardless of a reboot of the system in the mean time). If the disk was formatted before inserting a next time, it will not matter if the "ID" changes. Preferrably the code for linux would also work on MacOSX.

Best of course would be to query the serial of the disk itself, we do that in our software at work, but I don't know right now how exactly that needs to be done (I think on Linux we go through SysFS).

Alternatively you can use the Disk ID that is located inside the MBR of both MBR and GPT formatted disks at address 0x1B8 with a size of 4 Byte. Please note that you need to read from the disk, not the partition for that and that this won't work with SuperFloppy formatted disks (e.g. common USB drives are often not partitioned with MBR/GPT, but have a file system starting at sector zero).
See also: https://en.m.wikipedia.org/wiki/Master_boot_record

Regards,
Sven

Reply by email

Graeme Geldenhuys
to Bart: I need some function to get a unique ID for a disk. I need this to identify if my program…
Bart wrote
I need some function to get a unique ID for a disk.

Take a look at OnGuard - which is software copy projection software written for Delphi and FPC (originally by TurboPower Software Co). The latest software changes I made to it can be found on Github:

https://www.github.com/graemeg/onguard/

See the ogutils.pas unit. If I remember correctly the hard drive detection code worked for Windows, Linux, FreeBSD and OSX (? - not 100% sure about this one).

Hope that helps.

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email