If I have a 32-bit integer BSD device number dev_t (e.g. 0x1000004) on macOS (Darwin), how can I get the corresponding filesystem path for this device (e.g. "/dev/disk1s4")?
Asked
Active
Viewed 446 times
1
-
The `/dev/diskxxx`is just a directory entry pointing to a special inode. `mknod /tmp/mydisk b 4096 4` (or similar) will probably create (an other)one for you. – wildplasser Feb 20 '19 at 16:24
-
1So if you want to find an existing entry, you'll have to list the directory and look for it. (Note that it might not exist at all.) You can list the directory by invoking the `ls -l` command, or by calling `opendir`/`readdir` and then `stat`, whichever you find more convenient. – Steve Summit Feb 20 '19 at 16:52
2 Answers
3
You have to enumerate the mounted file systems and look for one who's device ID matches. You can use getfsstat()
for the enumeration. That fills in struct statfs
structures. Compare the field f_fsid.val[0]
of each structure to the dev_t
you're looking for. If they match, then that struct statfs
is the one for the device you're looking for and you can examine its other fields for the info you're looking for. In particular, the f_mntfromname
is the device path.

Ken Thomases
- 88,520
- 7
- 116
- 154
2
find /dev -type b -ls
, And check the output for major/minor == {0x1000,4}
Or: find / -type b -ls
if need to search the entire file system.
BTW: there could be more entries, referring to the same {major,minor} combination.

wildplasser
- 43,142
- 8
- 66
- 109