For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > April 2005 > How to check if dir is NFS mounted?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to check if dir is NFS mounted?
Heiko

2005-04-27, 3:59 pm


Hi,

What would be the best way to check from a C program whether a certain
directory is on a NFS mounted filesystem or on a local filesystem?
(using Debian GNU/Linux, if it matters)

Thanks in advance. Regards,

Heiko
Rich Teer

2005-04-27, 3:59 pm

On Wed, 27 Apr 2005, it was written:

> What would be the best way to check from a C program whether a certain
> directory is on a NFS mounted filesystem or on a local filesystem?
> (using Debian GNU/Linux, if it matters)


Assuming that Linux supports it, the statvfs function is probably
what you're after (see the f_basetype member). (I describe this
in more detail in my book, Solaris Systems Programming.)

HTH,

--
Rich Teer, SCNA, SCSA, OpenSolaris CAB member

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
Heiko

2005-04-27, 3:59 pm

Rich Teer wrote:

> On Wed, 27 Apr 2005, it was written:
>
>
>
> Assuming that Linux supports it, the statvfs function is probably
> what you're after (see the f_basetype member).


On Debian struct statvfs does not have a member "f_basetype". However
its man page refers to statfs(2). And struct statfs has a member f_type
for the filesystem type.

So your answer certainly got me on track. Thank you.

This raises a next question: How can I get the IP-address of the server?
/proc/mounts seems to contain that information. But is parsing /proc the
only way (on Linux)? I'm trying to find the fastest and most reliable
way, so I'd prefer some syscall?

Could anyone help me with this? Thanks.

Heiko
Floyd L. Davidson

2005-04-27, 3:59 pm

Heiko <heiko.noordhof_A_xs4all.nl> wrote:
>Rich Teer wrote:
>
>
>On Debian struct statvfs does not have a member
>"f_basetype". However its man page refers to statfs(2). And
>struct statfs has a member f_type for the filesystem type.


Linux provides a POSIX compliant statvfs(2), and POSIX does not
define a member f_basetype.

The f_fsid member (see the statfs(2) man page for values) will
tell you if it is an NFS mounted fs or not. (From the man page
it appears that this is not portable though.)

See /usr/include/bits/statvfs.h for the values of f_flags, which
relate to what options it is mounted with but don't indicate the
type of the fs.

>So your answer certainly got me on track. Thank you.
>
>This raises a next question: How can I get the IP-address of the server?
>/proc/mounts seems to contain that information. But is parsing
>/proc the only way (on Linux)? I'm trying to find the fastest
>and most reliable way, so I'd prefer some syscall?


It appears that it might be the easiest way, and possibly the
fastest too. First you need to stat the target directory to
determine which mounted filesystem it is part of, but once you
have that information everything else is located in
/proc/mounts, and it looks fairly easy to parse. If speed is a
problem, perhaps you could open(2), read the entire contents
with one call to read(2), and then close(2) it, thus making only
three slow system calls.

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com
SM Ryan

2005-04-27, 3:59 pm

# This raises a next question: How can I get the IP-address of the server?
# /proc/mounts seems to contain that information. But is parsing /proc the
# only way (on Linux)? I'm trying to find the fastest and most reliable
# way, so I'd prefer some syscall?

One the get* functions reads mtab or fstab. getmnt() or something like that.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I hope it feels so good to be right. There's nothing more
exhilirating pointing out the shortcomings of others, is there?
Thomas Dickey

2005-04-27, 3:59 pm

Rich Teer <rich.teer@rite-group.com> wrote:
> On Wed, 27 Apr 2005, it was written:


[color=darkred]
> Assuming that Linux supports it, the statvfs function is probably
> what you're after (see the f_basetype member). (I describe this
> in more detail in my book, Solaris Systems Programming.)


a moment's thought should have served to answer the assumption
(or google, if thought fails).

Linux supports statvfs(), but f_basetype appears to be SVr4-specific, Linux
documents its implementation as based on BSD (either conforms to X/Open).

Regarding your book - no, there's no more information in that section -
pages 439-440 - than is in Sun's manpage. (Providing more words is not
the same as providing more detail).

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com