incanus86 incanus86 - 1 year ago 194
Linux Question

Linux Programmatically find UUID's of Partitions

So I'm working on a fairly stripped down custom linux build at my job. I'm trying to discover a way to get the UUID's of the harddrive and zram partitions using C.

isn't available,
doesn't exist,
only provides the filesystem name and type.
is empty.
isn't available either.
doesn't exist.

is available but even on normal linux systems doesn't provide a UUID.

provides only information on the actual disk, no UUID, and nothing about the flash.

I've looked at the source for
and it seems to be reading
unless another file has been provided as it's source of information through an environment variable. I'm trying to find out if there is some other way that I might be able to open the drives and find the UUID or if linux stores them in some other way that I'm unaware of.

Answer Source

First of all, are you aware that there are UUIDs for both partitions and for filesystems? Neither is always present. Linux itself does not know about UUIDs, the entire concept is handled in user-space by programs installed on your system, and these programs all use libblkid, as far as I know.

You say that you want partition UUIDs, but you probably actually want filesystem UUIDs.


Sorry, but when you say that "libblkid isn't available", what does that even mean? I am a bit skeptical. Libblkid is open source and free software, after all, so if you don't have a copy of libblkid it is trivial to get one. Libblkid is also written in C, and you're already writing code in C, so you must have a C compiler around to compile libblkid. You can make a static version of it if necessary.

The main way libblkid works is by reading data directly from block devices. For disks, this means reading the partition table. For filesystems, this means reading from the filesystem superblock. Theoretically, if you can't use libblkid (which is highly unusual, even for stripped-down systems) you can parse the partition table or superblock yourself.

Libblkid does use a cache file at /etc/ but this is just a cache and it is not necessary.

Other interfaces to UUIDs, like /dev/disk/by-uuid, are created in user-space by programs which use libblkid.

Filesystem UUIDs

Normally, if you are talking about the UUIDs you see in /dev/disk/by-uuid, which are the same UUIDs you use in /etc/fstab, you are talking about the filesystem UUID. The libblkid library does the hard work of decoding the superblocks for a wide array of filesystems. You can see, for example, the UUID for Ext2 filesystems appear in libblkid src/superblocks/ext.c line 48. Libblkid supports many filesystem types and you can see them in the src/superblocks directory.

Not all filesystem types have UUIDs. Ext2 has a UUID, FAT32 does not, but it has a different ID which serves the same purpose.

Partition UUIDs

If you really need the unique partition UUID, you can also get that. Libblkid reads this out of the partition table. Only certain types of partition tables have UUIDs for partitions at all. GPT does, but DOS partition tables do not.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download