Philip Couling
Philip Couling

Reputation: 14924

Is there a way to get a meaningful mount point for st_dev on MacOS?

Note this is a MacOS question not a Linux Question - They are different operating systems

I'd like to get a meaningful mount point out of python's os.stat("foo").st_dev. At the moment this is just a number and I can't find anywhere to cross reference it.

All my searches so far have come up with answers that work on Linux by interrogating /proc/... but /proc doesn't exist in MacOS so any such answer will not work.

Upvotes: 3

Views: 571

Answers (2)

tripleee
tripleee

Reputation: 189789

You can loop over local disks, and compare the disk's st_rdev to the st_dev you got earlier.

for mnt in glob.glob("/dev/disk?s*"):
    t = os.stat(mnt)
    if t.st_rdev == s.st_dev:
      print(mnt)

Get device filesystem path from dev_t on macOS basically confirms this information, but of course, isn't strictly a duplicate.

For what it's worth, diskutil list -plist prints a machine-readable XML property list of all disk identifiers, and diskutil info -plist disk1s1 (for example) prints information for a particular disk; but this does not include the dev_t information that you get from os.stat().st_rdev

Annoyingly, diskutil does not have a manual page on my system, but here is a (possibly old) on-line version.

The getdevinfo package on PyPI appears to provide a Python binding to most of this information, though https://www.hamishmb.com/html/Docs/developer/getdevinfo/macos.html looks rather spare.

Upvotes: 0

VPfB
VPfB

Reputation: 17342

I'm a Linux guy, but if I were not allowed to use /proc, I would search the /dev directory for an entry (i.e. filename) which has following stat data:

  • st_mode indicates that it is a block device (helper: stat.S_ISBLK)
  • st_rdev matches the given st_dev value

Upvotes: 3

Related Questions