简体   繁体   English

在Linux和Mac OS X上检测已安装的驱动器

[英]Detecting mounted drives on Linux and Mac OS X

I'm using QDir::drives() to get the list of drives. 我正在使用QDir::drives()来获取驱动器列表。 It works great on Windows, but on Linux and Mac it only returns a single item “/”, ie root. 它在Windows上运行良好,但在Linux和Mac上它只返回单个项目“/”,即root。 It is expected behavior, but how can I get a list of drives on Mac and Linux? 这是预期的行为,但我怎样才能获得Mac和Linux上的驱动器列表?

Non-Qt, native API solutions are also welcome. 非Qt原生API解决方案也受到欢迎。

Clarification on "drive" definition: I'd like to get a list of mount points that are visble as "drives" in Finder or Linux built-in file manager. 关于“驱动器”定义的澄清:我想得到一个在Finder或Linux内置文件管理器中作为“驱动器”可见的挂载点列表。

As far as the filesystem is concerned, there is no concept of drives in Unix/Linux (I can't vouch for MacOSX but I'd say it's the same). 就文件系统而言,Unix / Linux中没有驱动器的概念(我不能担保MacOSX,但我会说它是相同的)。 The closest thing would probably be mount points, but a normal application shouldn't bother about them since all is already available under the filesystem root / (hence the behaviour of QDir::drives() that you observe). 最接近的事可能会被挂载点,但一个正常的应用程序不应该理会他们,因为所有文件系统的根目录下已经可用/ (的,因此行为QDir::drives()你观察)。

If you really want to see which mount points are in use, you could parse the output of the mount command (without any arguments) or, at least on Linux, the contents of the /etc/mtab file. 如果您真的想要查看正在使用的挂载点,则可以解析mount命令的输出(不带任何参数),或者至少在Linux上解析/etc/mtab文件的内容。 Beware though, mount points can get pretty hairy real quick (loop devices, FUSE filesystems, network shares, ...) so, again, I wouldn't recommend making use of them unless your application is designed to administer them. 请注意,挂载点可以快速实现(循环设备,FUSE文件系统,网络共享......),因此,除非您的应用程序旨在管理它们,否则我不建议使用它们。

Keep in mind that on Unix-y OSes, mount points are normally a matter for system administrators, not end-users, unless we're speaking of removable media or transient network shares. 请记住,在Unix-y操作系统上,挂载点通常是系统管理员而不是最终用户的问题,除非我们谈到可移动媒体或瞬态网络共享。


Edit: Following your clarifications in the comments, on Linux you should use getmntent or getmntent_r to parse the contents of the /etc/mtab file and thus get a list of all mount points and the corresponding devices. 编辑:在评论中澄清之后,在Linux上,您应该使用getmntentgetmntent_r来解析/etc/mtab文件的内容,从而获得所有挂载点和相应设备的列表。

The trick after that is to determine which ones you want to display (removable? network share?). 之后的诀窍是确定要显示哪些(可移动?网络共享?)。 I know that /sys/block/... can help with that, but I don't know all the details so you'll have to dig a bit more. 我知道/sys/block/...可以帮助解决这个问题,但我不知道所有细节,所以你需要多挖一点。

For example, to check whether /dev/sdd1 (a USB key) mounted on /media/usb0/ is a removable device, you could do (note how I use the device name sdd , not the partition name sdd1 ): 例如,要检查/media/usb0/上安装的/dev/sdd1 (USB密钥)是否是可移动设备,您可以这样做(注意我如何使用设备名称sdd ,而不是分区名称sdd1 ):

$ cat /sys/block/sdd/removable
1

As opposed to my main hard drive: 与我的主硬盘相反:

$ cat /sys/block/sda/removable
0

Hope this puts you on the right track. 希望这能让你走上正轨。

对于OS X, 磁盘仲裁框架可用于列出和监视驱动器和挂载点

Scraping the output of mount shell command is certainly one option on either platform - although, what is your definition of a drive here? 刮掉mount shell命令的输出肯定是任何一个平台上的一个选项 - 尽管你在这里定义了什么驱动器? Physical media, removable drivers, network volumes? 物理媒体,可移动驱动程序,网络卷? You'll need to do a lot of filtering. 你需要做很多过滤。

On MacOSX, the mount point for removable media, network volumes, and secondary hard-drives is always under /Volumes/ , so simply enumerating items in this directory will do the trick if your definition of a drive is broad. 在MacOSX上,可移动媒体,网络卷和辅助硬盘驱动器的安装点始终位于/Volumes/之下,因此,如果您对驱动器的定义很宽泛,只需枚举此目录中的项目即可。 This ought to be fairly safe as they're all automounted . 这应该是相当安全的,因为它们都是自动安装的。

On Linux, there are a variety of locations depending on the particular distro in use. 在Linux上,根据使用的特定发行版,有多种位置。 /mnt/ is the traditional, but there are others. /mnt/是传统的,但还有其他的。

In linux, the way to get information about drives currently mounted is to parse the mtab file. 在linux中,获取当前挂载的驱动器信息的方法是解析mtab文件。 glibc provides a macro _PATH_MNTTAB to locate this file. glibc提供了一个宏_PATH_MNTTAB来定位这个文件。 See http://www.gnu.org/software/libc/manual/html_node/Mount-Information.html#Mount-Information 请参阅http://www.gnu.org/software/libc/manual/html_node/Mount-Information.html#Mount-Information

If you know the format of the drive/drives in question, you can use the df command to output the list of drives from the console or programatically as a system command. 如果您知道有问题的驱动器/驱动器的格式,则可以使用df命令从控制台输出驱动器列表,或者以编程方式输出系统命令。 For example, to find all the ext4 drives: 例如,要查找所有ext4驱动器:

df -t ext4

You can simply add additional formats onto the same command if you are interested in more than one type: 如果您对多种类型感兴趣,可以在同一命令中添加其他格式:

df -t ext4 -t tmpfs

This is going to return to you the physical location of the drive, the amount of memory it has, the amount of memory used, the amount of memory free, the use% and where it is mounted on the filesystem. 这将返回驱动器的物理位置,它具有的内存量,使用的内存量,可用内存量,使用%以及它在文件系统上的安装位置。

df will show you all of the drives mounted on the system, but some are going to be things that aren't really what you are looking for like temporary file systems, etc. df将向您显示系统上安装的所有驱动器,但有些驱动器将是您正在寻找的不是临时文件系统等的东西。

Not sure if this will work on OSX or not, but it does work on my Ubuntu 12.04 distribution. 不确定这是否适用于OSX,但它确实适用于我的Ubuntu 12.04发行版。

Another way is to check for "Volumes" 另一种方法是检查“卷”

df -H | df -H | grep "/Volumes" grep“/ Volumes”

I know that this is old, but it failed to mention getfsstat which I ended up using in macos. 我知道这是旧的,但它没有提到我最终在macos中使用的getfsstat You can get a list of mounts (which will include most disks) using getfsstat . 您可以使用getfsstat获取安装列表(包括大多数磁盘)。 See man 2 getfsstat for further information. 有关详细信息,请参阅man 2 getfsstat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM