Linux C programming: how to get device partition information?

I'm new to Linux c programming, is there any API that can get device partition information?

+5
source share
5 answers

The answer of Akash Rawal is pretty close. A good way is to use libblkid.

This is an example of getting a uuid section: Using libblkid to find the UUID of a section .

I combined the above code and examples in the libblkid help menu and generated the working program below:

#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;
}

Using:

gcc -o getuuid getuuid.c -lblkid
sudo ./getuuid /dev/sdc
Number of partitions:1
Name=/dev/sdc1, UUID=754A-CE25, LABEL=KINGSTON, TYPE=vfat
+7
source

You can see /sys/block/sd?/where there are many pseudo-files telling you about some section parameters.

+2
source

libblkid API. blkid ( util-linux) .

blkid , UUID. .

# 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" 
# 

: http://www.kernel.org/pub/linux/utils/util-linux/v2.21/libblkid-docs/

, .

+2

API .

ldd `which fdisk`, , , , .

fdisk , .

, , :

  • wikipedia MBR.
  • fdisk .
0

() "/proc/partition" , , , . , ( , , ). /proc/ :

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 - hd, sdb - USB-. , sda3 - , , . , , , . :

1) Create a couple of pipes
2) fork a process an redirect the pipes to child 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

, .

0

All Articles