问题描述
从绝对文件路径开始,我想获得以下信息:
Starting with an absolute file path, I want to obtain the following information:
- 存储文件的文件系统的挂载点(以计算相对于挂载点的路径)
- 文件系统的UUID和标签
- 包含分区的硬盘驱动器的类型(或供应商名称)和序列号
我知道在许多情况下(例如,对于环回,ramfs,加密设备),2和3可能是未定义的,这完全没问题. 我还知道如何使用外壳程序和系统工具(如df
和/sys
或/proc
文件系统)获取该信息.请参见以供参考.
I am aware that 2 and 3 may be undefined in many cases (e.g. for loopback, ramfs, encyrpted devices), which is totally fine. I also know how to obtain that information using a shell and system tools like df
and the /sys
or /proc
filesystem. See this question for reference.
但是,我正在寻找使用Python 3.5以编程方式做到这一点的最简单的方法.这意味着:
However, I am searching for the least cumbersone method to do that programmatically with Python 3.5. That means:
- 首选系统调用,而不是解析
/proc
或/sys
的内容(可能会更改还是取决于内核配置?) - 避免调用子流程并解析其输出(繁琐的定义)
- Prefer system calls instead of parsing contents of
/proc
or/sys
(which may be subject to change or depend on kernel configuration?) - Avoid calling subprocesses and parsing their output (the definition of cumbersome)
到目前为止,我在路径上使用os.stat()
从stat_result.st_dev
获取块设备的主要和次要编号.但是正确的进行方式是什么?
So far, I am using os.stat()
on the path to get the block device's major and minor number from stat_result.st_dev
. But what's the proper way to proceed?
例如有
-
/proc/mounts
-
/proc/partitions
-
/sys/dev/block/<major>:<minor>
/proc/mounts
/proc/partitions
/sys/dev/block/<major>:<minor>
注释:关于已安装的块设备的分区,/proc/mounts
和/proc/partitions
似乎是规范信息源(可以).对于UUID,标签,序列号等,我目前使用udevadm
并解析输出:
Notes:Regarding mounted block devices an partitions, /proc/mounts
and /proc/partitions
seem to be the canonical information source (which is OK). For UUIDs, labels, serials etc. I currently use udevadm
and parse the output:
def get_udev_properties(dev_name):
cmd = ["udevadm", "info", "--query=property", "--name", dev_name]
result = subprocess.run(cmd, stdout=subprocess.PIPE)
return parse_properties(result.stdout)
进一步的说明:从我的粗俗问题中提炼出来的人可以问一个更笼统的问题:
- 相对于linux系统调用和内核文件系统,块设备的规范标识或表示形式是什么?
- 以大号和小号获得代表的正确方法是什么?
- 获取有关块设备的详细信息的正确方法是什么?
推荐答案
这是GitHub上的脚本今天早些时候,我遇到了使用Python来获取有关驱动器品牌和型号(以及许多其他信息)的信息.
This is a script on GitHub I came across earlier today that uses Python to fetch information about drive make and model (and lots of others).
/proc/partitions
保存有关分区的信息;有关更多详细信息,您将必须像现在一样运行子流程,或者例如对于GPT 自己解析.
/proc/partitions
holds info on partitions; for more detailed information you'll have either to run a subprocess as you do now, or for example for GPT do the parsing yourself.
这篇关于如何从Linux上的文件路径获取详细的设备/分区信息(如UUID,硬盘驱动器序列等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!