This is an example on how to find which device corresponds to your root filesystem on linux. This code was inspired by Michael Opdenacker post and related questions on StackOverflow.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <assert.h> #include <stdio.h> int main(int argc, char *argv[]) { struct stat s; int maj, min, pmaj, pmin; char name[256]; int res; res = stat("/", &s); assert(0 == res); maj = major(s.st_dev); min = minor(s.st_dev); FILE * f = fopen("/proc/partitions", "r"); assert(f); // skip first two lines fscanf(f, "%*[^\n]"); fgetc(f); fscanf(f, "%*[^\n]"); fgetc(f); while (! feof(f)) { res = fscanf(f, " %d %d %*d %s\n", &pmaj, &pmin, name); assert(3 == res); printf("/dev/%s %d:%d%s\n", name, pmaj, pmin, (pmaj == maj && pmin == min? " *" : "")); } fclose(f); return 0; } |
The output will mark the root device partition with an asterisk.
This code has the advantage of not touching all the entries at /dev as done by rdev(8) nor recursing the files under /sys/block to find the exact device, which would be a bit more complex than parsing /proc/partitions.