4

I’m using QDir::drives() to get the list of drives. It works great on Windows, but on Linux and Mac it only returns a single item “/”, i. e. root. It is expected behavior, but how can I get a list of drives on Mac and Linux?

Non-Qt, native API solutions are also welcome.

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.

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335

7 Answers7

5

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). 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).

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. 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.

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.


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.

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.

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):

$ 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.

syam
  • 14,701
  • 3
  • 41
  • 65
  • Edited my question to indicate which mount points I'm interested in. The ideal solution would be an appropriate system API that returns a list of file system paths, but so far I can't find it for neither system. – Violet Giraffe Sep 03 '13 at 20:07
  • MacOSX is essentially BSD in this respect, and behaves as such - but with much more automation of automounting than is normally found on *NIX. – marko Sep 03 '13 at 20:08
  • @VioletGiraffe Added some additional information, might be useful even if it's still a bit vague. – syam Sep 03 '13 at 20:35
  • Thanks! `getmntent` gives some trash as well as desired points, but it works. – Violet Giraffe Sep 03 '13 at 20:51
  • `cat /sys/block/sdd/removable/` returns 0 for my external hard drive. I have found that checking `/dev/by-path/...` entries for the text `usb` in them to be more reliable. My external hard-drive mounted at `/dev/sdb` has `by-path` id of `pci-0000:00:10.0-usb-0:1:1.0-scsi-0:0:0:0`, which as opposed to the internal hard-drive, designated as `pci-0000:00:11.0-ata-1` – Marcus Jun 15 '16 at 20:09
5

For OS X, the Disk Arbitration framework can be used to list and monitor drives and mount points

Nore
  • 83
  • 5
  • Link is dead. New one is https://developer.apple.com/library/mac/documentation/Darwin/Reference/DiscArbitrationFramework/ – prewett Feb 06 '16 at 20:31
1

Scraping the output of mount shell command is certainly one option on either platform - although, what is your definition of a drive here? 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. 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. /mnt/ is the traditional, but there are others.

marko
  • 9,029
  • 4
  • 30
  • 46
  • I'd like to get a list of mount points that are visble as "drives" in Finder or Linux file built-in file manager. – Violet Giraffe Sep 03 '13 at 20:05
  • I think that is the case with `/Volumes' - although you can hide them in the side-bar of Finder – marko Sep 03 '13 at 20:06
  • @VioletGiraffe There is no built-in file manager on Linux, unless `ls` and `cd` count as one (and they certainly don't have any concept of "drives"). – syam Sep 03 '13 at 20:07
  • @syam: maybe not Linux itself, KDE and GNOME then. – Violet Giraffe Sep 03 '13 at 20:08
  • @VioletGiraffe Well, I'm on KDE and Dolphin only shows me removable media (typically, USB or CD-ROM). Is that what you're after? My various partitions (a few internal disk drives) and network shares don't appear. – syam Sep 03 '13 at 20:10
  • @syam: that + I have NTFS partition mounted with `ntfsmount` and Dolphin also shows it. – Violet Giraffe Sep 03 '13 at 20:11
  • @marko "...is always under `/Volumes/`" Do you have any citations for that? I'm trying to ensure that that's always the case for OSX, but not finding any hard evidence. – mphair Jan 15 '14 at 12:26
1

In linux, the way to get information about drives currently mounted is to parse the mtab file. glibc provides a macro _PATH_MNTTAB to locate this file. See http://www.gnu.org/software/libc/manual/html_node/Mount-Information.html#Mount-Information

jhauris
  • 1,153
  • 11
  • 20
1

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. For example, to find all the ext4 drives:

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.

Not sure if this will work on OSX or not, but it does work on my Ubuntu 12.04 distribution.

Brian
  • 3,264
  • 4
  • 30
  • 43
1

Another way is to check for "Volumes"

df -H | grep "/Volumes"

Paul Cav
  • 50
  • 3
1

I know that this is old, but it failed to mention getfsstat which I ended up using in macos. You can get a list of mounts (which will include most disks) using getfsstat. See man 2 getfsstat for further information.

Don
  • 3,876
  • 10
  • 47
  • 76
Bob
  • 11
  • 1