7

I am very much new to Linux programming.

My questions are:

  • Is there any 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 some commands sudo vol_id --uuid /dev/sda1, sudo blkid and ls -l /dev/disk/by-uuid/. But all of them are commands which need to run in a terminal. But I need to achieve this from a C/C++ program.

Can some one help me with this problem. (FYI: I need to read UUID of the root filesystem ("/") where Linux has been installed.)

Thank you in advance.

CRM
  • 4,569
  • 4
  • 27
  • 33
user502814
  • 69
  • 1
  • 2
  • You could use `getfsfile("/")` as answered [here](http://stackoverflow.com/a/344656/2508277) – Unda May 26 '15 at 13:55

2 Answers2

2

The general approach would be:

  • find out what device your / is on by parsing /etc/mtab for example
  • go through the /dev/disks/by-uuid directory (using opendir/readdir/closedir) and find which one points to that device.

See the readlink function for getting the target of a symbolic link. You'll find plenty of code examples for parsing text files on this site or with your favorite search engine.

Mat
  • 202,337
  • 40
  • 393
  • 406
0

Since blkid already does it, you could also just see how it works and pilfer the solution, if you abide by util-linux's license (GPLv2).

Ilkka
  • 2,644
  • 1
  • 23
  • 27
  • 2
    You only need to abide by the license if you pilfer the actual code; copyright protects implementations but not ideas. Otherwise, you couldn't even use the `opendir/readdir/closedir` idea because there's a `ls` implementation doing that. – MSalters Dec 29 '11 at 09:35