在Linux下如何获得一个设备/分区的UUID编程? [英] How get uuid of a device/partition in linux programatically?
问题描述
我很新的Linux编程。
I am very much new to Linux programming.
我的问题是:
- 有没有办法读取Linux的编程设备或分区的UUID?
- 有没有用户空间应用程序的任何C / C ++ API?
我发现了一些命令须藤vol_id --uuid的/ dev / sda1的
,须藤的BLKID-
和 ls -l命令的/ dev /磁盘/副UUID /
。但是,所有这些都是需要在终端运行命令。但我需要从C / C ++程序实现这一目标。
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.
有人可以帮我解决这个问题。 (FYI:我需要阅读的根文件系统的UUID(/),其中的Linux已经安装)
Can some one help me with this problem. (FYI: I need to read UUID of the root filesystem ("/") where Linux has been installed.)
感谢您提前。
推荐答案
一般的做法是:
- 找到你的/是什么设备上通过解析
的/ etc / MTAB
榜样 - 经过
的/ dev /磁盘/副UUID
目录(使用执行opendir / READDIR / closedir
)和发现其中一个指向设备。
- find out what device your / is on by parsing
/etc/mtab
for example - go through the
/dev/disks/by-uuid
directory (usingopendir/readdir/closedir
) and find which one points to that device.
请参阅获取一个符号链接的目标的readlink
功能。你会发现很多的code示例在本网站或与您喜爱的搜索引擎解析文本文件。
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.
这篇关于在Linux下如何获得一个设备/分区的UUID编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!