如何以编程方式获取 Linux 中设备/分区的 uuid?
我对 Linux 编程非常陌生。
我的问题是:
- 有没有办法以编程方式读取 Linux 中设备或分区的 UUID?
- 是否有用于用户空间应用程序的 C/C++ API?
我发现了一些命令 sudo vol_id --uuid /dev/sda1
、sudo blkid
和 ls -l /dev/disk/by-uuid/
。但所有这些都是需要在终端中运行的命令。但我需要通过 C/C++ 程序来实现这一点。
有人可以帮我解决这个问题吗? (仅供参考:我需要读取安装了 Linux 的根文件系统(“/”)的 UUID。)
提前谢谢您。
I am very much new to Linux programming.
My questions are:
- Is there any way to read the UUID of a device or partition in Linux programmatically?
- Is there any C/C++ API for user-space applications?
I found some commands sudo vol_id --uuid /dev/sda1
, sudo blkid
and ls -l /dev/disk/by-uuid/
. But all of them are commands which need to run in a terminal. But I need to achieve this from a C/C++ program.
Can some one help me with this problem. (FYI: I need to read UUID of the root filesystem ("/") where Linux has been installed.)
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般方法是:
/etc/mtab
找出您的 / 所在的设备/dev/disks/by-uuid
目录(使用opendir/readdir/linedir
)并找到哪个指向该设备。请参阅 readlink 函数以获取符号链接的目标。您将在此站点上或使用您最喜欢的搜索引擎找到大量用于解析文本文件的代码示例。
The general approach would be:
/etc/mtab
for example/dev/disks/by-uuid
directory (usingopendir/readdir/closedir
) and find which one points to that device.See the
readlink
function for getting the target of a symbolic link. You'll find plenty of code examples for parsing text files on this site or with your favorite search engine.由于
blkid
已经做到了,您也可以看看它是如何工作的并窃取解决方案,如果您遵守 util-linux 的许可证 (GPLv2)。Since
blkid
already does it, you could also just see how it works and pilfer the solution, if you abide by util-linux's license (GPLv2).