3

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.

Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
Philip Couling
  • 13,581
  • 5
  • 53
  • 85
  • Would any of the methods in [this Q&A](https://stackoverflow.com/questions/4453602/how-to-find-the-mountpoint-a-file-resides-on) work for you? – Gordon Davisson Jan 24 '21 at 21:25

2 Answers2

3

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
VPfB
  • 14,927
  • 6
  • 41
  • 75
0

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • This is just background which basically confirms the other answer; you should probably accept that. – tripleee Jan 25 '21 at 06:36