Skip to content

Using… libblkid

February 27, 2013

This is a very short post on how to use libblkid.

Two source code example are provided, then few reference links.

#include
#include
#include
#include <blkid/blkid.h>

int main (int argc, char *argv[]) {
	blkid_probe pr;
	const char* value;

	if (argc != 2) {
		fprintf(stderr, "Usage: %s devname\n", argv[0]);
		exit(1);
	}

	pr = blkid_new_probe_from_filename(argv[1]);
	if (!pr) {
		err(2, "Failed to open %s", argv[1]);
	}

	blkid_do_probe(pr);
	blkid_probe_lookup_value(pr, "TYPE", &value, NULL);

	printf("TYPE=%s\n", uuid);

	blkid_free_probe(pr);

	return 0;
}

Using “TYPE” in blkid_probe_lookup_value we get the device “type”.
Possible values are:

  • ext2
  • swap
  • LVM2_member
  • linux_raid_member
  • crypto_LUKS

The following source fragment will show all the valid tags for a device.

#include
#include
#include
#include <blkid/blkid.h>

int main (int argc, char *argv[]) {
	blkid_probe pr;

	if (argc != 2) {
		fprintf(stderr, "Usage: %s devname\n", argv[0]);
		exit(1);
	}

	pr = blkid_new_probe_from_filename(argv[1]);
	if (!pr) {
		err(2, "Failed to open %s", argv[1]);
	}

	while (blkid_do_probe(pr) == 0) {
		int nvals = blkid_probe_numof_values(pr);
		int n;
		const char* name;
		const char* data;
		int len= 0;
		for (n = 0; n < nvals; n++) {
        	if (blkid_probe_get_value(pr, n, &name, &data, &len) == 0)
			printf("%s = %s\n", name, data);
		}
	}
	blkid_free_probe(pr);

	return 0;
}

Here how to compile and build the sources above (assuming they are saved in file named libblkidtst.c):
gcc -c libblkidtst.c
gcc -o libblkidtst -lblkid libblkidtst.o

Here how to run:
[sudo] ./libblkidtst
Example:
sudo ./libblkidtst /dev/sda1

Here the reference documentation and useful information

Using libblkid to find UUID of a partition

libblkid(3) – Linux man page

libblkid Reference Manual

From → Technology

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: