Linux C 编程:如何获取设备的分区信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8663643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux C programming: How to get a device's partition infomation?
提问by bright
I'm new to Linux c programming, is there any API that can get a device's partition information?
我是 Linux c 编程的新手,有没有可以获取设备分区信息的 API?
回答by holygeek
I'm not aware of any API for that.
我不知道有任何 API。
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.
我做了ldd `which fdisk`
,但它没有显示任何与读取分区表有关的库,所以我认为它是自行完成的。
A quick glance at the source code of fdisk
program further convinces me that it doesn't use any external library.
快速浏览一下fdisk
程序的源代码,进一步让我相信它不使用任何外部库。
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.
- 阅读MBR上的维基百科页面。
- 获取源
fdisk
代码并阅读代码。
回答by Giuseppe Guerrini
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:
(伪)文件“/proc/partitions”至少包含在物理设备上找到的分区列表。不幸的是,它没有说明分区类型(特别是,没有任何简单的方法可以猜测分区是否实际上是扩展分区)。这是我在机器的 /proc/partitions 中找到的内容:
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:
sda 是我的高清,sdb 是 USB 加密狗。注意sda3是一个扩展分区,但是不直接读取磁盘的分区表是很难猜到的。您在此文件中找到的信息可能是否足够,这取决于我们的特定需求。恕我直言,更安全的方法是:
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.
我希望这可以帮助你。
回答by glglgl
You could have a look at /sys/block/sd?/
where there are many pseudo-files telling you about some partition parameters.
你可以看看/sys/block/sd?/
哪里有很多伪文件告诉你一些分区参数。
回答by Akash Rawal
libblkid could be a nice API. blkid (present in util-linux package) uses it.
libblkid 可能是一个不错的 API。blkid(存在于 util-linux 包中)使用它。
blkid lists all partitions along with label, filesystem type and UUID. Here's an output on my system.
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"
#
Here's reference manual: http://www.kernel.org/pub/linux/utils/util-linux/v2.21/libblkid-docs/
这是参考手册: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.
我刚刚找到了这个图书馆,我自己也在寻找同样问题的答案。
回答by Peter
Akash Rawal's answer is pretty close. A good way is to use libblkid.
Akash Rawal 的回答非常接近。一个好方法是使用 libblkid。
This is an example to get uuid of a partition: Using libblkid to find UUID of a partition.
这是获取分区 uuid 的示例: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:
我结合了上面显示的代码和 libblkid 参考菜单中的示例,并生成了以下工作程序:
#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