I'm new to Linux c programming, is there any API that can get a device's partition information?
-
You want to get the list of partitions available on a given block device (presumably a physical disk)? – Brian Cain Dec 29 '11 at 03:58
-
Could be a possible duplicate of http://stackoverflow.com/questions/5288034/how-get-uuid-of-a-device-partition-in-linux-programatically – Sangeeth Saravanaraj Dec 29 '11 at 04:17
5 Answers
Akash Rawal's answer is pretty close. A good way is to use libblkid.
This is an example to get uuid of a partition: Using libblkid to find UUID of a partition.
I combined the code shown above and examples in libblkid reference menu and generate below working program:
#include <stdio.h>
#include <string.h>
#include <err.h>
#include <blkid/blkid.h>
int main (int argc, char *argv[]) {
blkid_probe pr = blkid_new_probe_from_filename(argv[1]);
if (!pr) {
err(1, "Failed to open %s", argv[1]);
}
// Get number of partitions
blkid_partlist ls;
int nparts, i;
ls = blkid_probe_get_partitions(pr);
nparts = blkid_partlist_numof_partitions(ls);
printf("Number of partitions:%d\n", nparts);
if (nparts <= 0){
printf("Please enter correct device name! e.g. \"/dev/sdc\"\n");
return;
}
// Get UUID, label and type
const char *uuid;
const char *label;
const char *type;
for (i = 0; i < nparts; i++) {
char dev_name[20];
sprintf(dev_name, "%s%d", argv[1], (i+1));
pr = blkid_new_probe_from_filename(dev_name);
blkid_do_probe(pr);
blkid_probe_lookup_value(pr, "UUID", &uuid, NULL);
blkid_probe_lookup_value(pr, "LABEL", &label, NULL);
blkid_probe_lookup_value(pr, "TYPE", &type, NULL);
printf("Name=%s, UUID=%s, LABEL=%s, TYPE=%s\n", dev_name, uuid, label, type);
}
blkid_free_probe(pr);
return 0;
}
Usage:
gcc -o getuuid getuuid.c -lblkid
sudo ./getuuid /dev/sdc
Number of partitions:1
Name=/dev/sdc1, UUID=754A-CE25, LABEL=KINGSTON, TYPE=vfat
You could have a look at /sys/block/sd?/
where there are many pseudo-files telling you about some partition parameters.

- 89,107
- 13
- 149
- 217
libblkid could be a nice API. blkid (present in util-linux package) uses it.
blkid lists all partitions along with label, filesystem type and UUID. Here's an output on my system.
# blkid
/dev/sda1: LABEL="grub" UUID="a760119d-916a-492c-8ec1-50f81dbf4e26" TYPE="ext2"
/dev/sda3: LABEL="Library" UUID="248D72BD2E5CF009" TYPE="ntfs"
/dev/sda5: LABEL="WinXP" UUID="545CC7085CC6E438" TYPE="ntfs"
/dev/sda6: LABEL="Win7" UUID="22F009B2F0098CEB" TYPE="ntfs"
/dev/sda7: LABEL="Puppy" UUID="fe1dc425-ad17-4773-971a-435d91690883" SEC_TYPE="ext2" TYPE="ext3"
/dev/sda8: LABEL="Linux Mint" UUID="0c61a114-9353-499b-a1cd-ba19722b1e43" TYPE="ext4"
/dev/sda9: LABEL="Ubuntu L" UUID="5f6923e1-92f4-4b3f-b613-9e78839e1987" TYPE="ext4"
/dev/loop0: TYPE="squashfs"
/dev/loop1: UUID="16e3a75d-74dc-4391-a9e2-4305c08c5707" TYPE="ext3"
/dev/loop4: TYPE="squashfs"
#
Here's reference manual: http://www.kernel.org/pub/linux/utils/util-linux/v2.21/libblkid-docs/
I just now found this library, myself looking for answer for the same question.

- 21
- 2
The (pseudo)file "/proc/partitions" contains at least a list of the partitions found on physical devices. Unfortunately it doesn't say anything about partition types (in particular, there isn't any simple way to guess if a partition is actually an extended one). Here's what I find in /proc/partitions of my machine:
major minor #blocks name
8 0 488386584 sda
8 1 13631488 sda1
8 2 237375488 sda2
8 3 1 sda3
8 4 3650560 sda4
8 5 3413781 sda5
8 6 29294496 sda6
8 7 14651248 sda7
8 8 9767488 sda8
8 9 176586448 sda9
8 16 7815168 sdb
8 17 7811136 sdb1
sda is my hd, sdb is an USB dongle. Note that sda3 is an extended partition, but it's hard to guess it without reading directly the disk's partition table. The information you find in this file may be enough or not, it depends on our particular need. IMHO a more safe way is to:
1) Create a couple of pipes
2) fork a process an redirect the pipes to child's stdin and sdtout (fd 0 and 1)
3) execl "fdisk /dev/sdXXX"
4) send a "p\n" command to the child process
5) read the lines containg the complete partition information
I hope that this can help you.

- 4,274
- 17
- 32
I'm not aware of any API for that.
I did ldd `which fdisk`
and it didn't show any library that pertains to reading partition table so I assumed that it does it on its own.
A quick glance at the source code of fdisk
program further convinces me that it doesn't use any external library.
If you're keen on figuring things out yourself and learning on your own here's what I suggest you to do:
- Read the wikipedia page on MBR.
- Get the source to
fdisk
and read the code.

- 15,653
- 1
- 40
- 50