svth
svth

Reputation: 1310

Get device filesystem path from dev_t on macOS

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")?

Upvotes: 1

Views: 588

Answers (3)

John_K
John_K

Reputation: 21

The devname api will consume a dev_t and a mode_t and return the name of the device for you.

See man 3 devname or this documentation.

Upvotes: 0

wildplasser
wildplasser

Reputation: 44210

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.

Upvotes: 2

Ken Thomases
Ken Thomases

Reputation: 90521

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.

Upvotes: 3

Related Questions