How to get uuid device / partition in Linux programmatically?

I am very new to Linux programming.

My questions:

  • Is there a way to read the UUID of a device or partition in Linux programmatically?
  • Is there any C / C ++ API for user space applications?

I found several sudo vol_id --uuid /dev/sda1 , sudo blkid and ls -l /dev/disk/by-uuid/ . But all of them are commands that need to be run in the terminal. But I need to achieve this from a C / C ++ program.

Can someone help me with this problem. (FYI: I need to read the UUID of the root file system ("/") where Linux was installed.)

Thanks in advance.

+6
c ++ c linux uuid
source share
2 answers

General approach:

  • find out which device is on / on, for example, using parsing /etc/mtab
  • Go to the /dev/disks/by-uuid opendir/readdir/closedir (using opendir/readdir/closedir ) and find which one points to this device.

See the readlink function for the purpose of a symbolic link. You will find many sample code for analyzing text files on this site or using your favorite search engine.

+1
source share

Since blkid already does this, you can also just see how it works and steal the solution if you use the util-linux license (GPLv2).

0
source share

All Articles