Since stat
returns a "Device" field, I was curious to see how the underlying stat()
library call could be used to get this information programmatically in a POSIX compliant way.
This snippet of C code:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int main (int argc, const char *argv[]) {
struct stat info;
stat(argv[1], &info);
printf("min: %d maj: %d\n",
minor(info.st_dev),
major(info.st_dev)
);
return 0;
}
Will give the major and minor devices ID's for the device containing the file listed on the command line (argv[1]
). Unfortunately, major()
and minor()
aren't POSIX, although the man page claims they are "present on many other systems " besides GNU/linux.
You can then get a correspondence between the device major/minor number and the device node from, e.g., /proc/diskstats
, and map that to mount points from /proc/mounts
, aka. /etc/mtab
.
So a command-line utility to do this would be pretty simple.
tr
call by usingawk -F'% '...
– Joseph R. Sep 11 '13 at 13:56GetVolumePathName
. – Solomon Ucko May 08 '23 at 11:58