使用 libblkid 查找分区的 UUID
我正在查看 libblkid 并对文档。有人能给我提供一个示例,说明如何使用这个库找到 Linux 根分区的 UUID 吗?
I was looking at libblkid and was confused about the documentation. Could someone provide me with an example of how I could find the UUID of a root linux partition using this library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它与手册看起来一样简单:创建一个探针结构,初始化它,询问它一些信息,然后释放它。您可以将前两个步骤合并为一个。这是一个工作程序:
blkid_probe_lookup_value
将uuid
设置为指向属于pr
结构的字符串,这就是参数为类型的原因const char *
。如果需要,您可以将其复制到您自己管理的char *
中,但如果只是传递给printf
,则不需要这样做。blkid_probe_lookup_value
的第四个参数可让您获取返回值的长度,以防您也需要。blkid_do_probe
、blkid_do_safeprobe
和blkid_do_fullprobe
之间存在一些细微差别,但在设备具有已知文件系统并且您只想拉取的情况下从中取出 UUID,从 blkid_do_probe 中获取第一个结果就可以了。It's pretty much as simple as the manual makes it look: you create a probe structure, initialize it, ask it for some information, and then free it. And you can combine the first two steps into one. This is a working program:
blkid_probe_lookup_value
setsuuid
to point to a string that belongs to thepr
structure, which is why the argument is of typeconst char *
. If you needed to, you could copy it to achar *
that you manage on your own, but for just passing toprintf
, that's not needed. The fourth argument toblkid_probe_lookup_value
lets you get the length of the returned value in case you need that as well. There are some subtle differences betweenblkid_do_probe
,blkid_do_safeprobe
, andblkid_do_fullprobe
, but in cases where the device has a known filesystem and you just want to pull the UUID out of it, taking the first result fromblkid_do_probe
should do.首先,您需要找到以 root 身份安装的设备。参见 man getmntent (3)。一旦您知道了设备,请按照 hobbs 的描述使用 blkid_new_probe_from_filename 。
First you need to find the device mounted as as root. See man getmntent (3). Once you know the device, use blkid_new_probe_from_filename as described by hobbs.