[fpc-pascal] Get "disk" ID

Giuliano Colla giuliano.colla at fastwebnet.it
Sun Jan 6 11:10:36 CET 2019


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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20190106/940c9673/attachment.html>


More information about the fpc-pascal mailing list