diectory device

How can I get the device name (e.g. /dev/hda, /dev/sda) where a directory (e.g. /) is placed? Thnx
[105 byte] By [agafa] at [2007-11-26 21:12:06]
# 1

Hello.

In /etc/mnttab you will find a list of all mounted directories and corresponding devices. You may create a list like this from the information found there:

/ -> /dev/dsk/c0d0s0 (example for Solaris/x86)

/export/home -> /dev/dsk/c0d0s7

/mnt -> /dev/floppy

To find out which disk contains a directory "/mydir/mysubdir/anydir" there are two methods:

1) The harder method (but you can do it "by hand"):

Resolve all symbolic links. /mydir may be a symlink to /export/home/mydir so you'll get /export/home/mydir/mysubdir/anydir. Then look in the /etc/mnttab file for the parent directory. The same line contains the name of the device.

2) The easier (and better) method (you can do in a C program only):

Call "stat()" for the directory you are interested in. The "st_dev" member of the stat structure identifies the device. You now have to call "stat()" for all mounted directories in /etc/mnttab. The directory that has the same "st_dev" value is on the same device as the directory desired. The same line in /etc/mnttab contains the device.

Martin

-- EDIT --

The following C program does the job:

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

void whatdevice(const char *fname)

{

struct stat st;

dev_t dev;

FILE *f;

char a[1000];

int i,j;

if(stat(fname,&st)<0)

{

fprintf(stderr,"%s could not be found.\n",fname);

return;

}

dev=st.st_dev;

f=fopen("/etc/mnttab","r");

if(!f)

{

fprintf(stderr,"An internal error occurred.\n");

return;

}

while(fgets(a,sizeof(a),f))

{

for(i=0;a[ i ] && a[ i ]!=9;i++);

if(!a[i]) continue;

a[i++]=0;

for(j=i;a[ j ] && a[ j ]!=9;j++);

a[ j ]=0;

if(stat(&(a[ i ]),&st)>=0) if(st.st_dev==dev)

{

fclose(f);

printf("%s: %s\n",fname,a);

return;

}

}

fclose(f);

printf("%s: <unknown>.\n",fname);

}

int main(int argc,char **argv)

{

int i;

if(argc<2)

{

printf("Syntax: %s files...\n",argv[0]);

return 1;

}

for(i=1;i<argc;i++) whatdevice(argv[ i ]);

return 0;

}

Example:

bash-2.05 ~$ ./whatdevice /usr /export/home . /proc /etc/mnttab

/usr: /dev/dsk/c1t1d0s0

/export/home: /dev/dsk/c4t0d0s7

.: /dev/dsk/c4t0d0s7

/proc: /proc

/etc/mnttab: mnttab

bash-2.05 ~$

>

Martin_Rosenaua at 2007-7-10 2:49:43 > top of Java-index,Solaris Operating System,Solaris 10 Features...