How to check if dir is NFS mounted?

394 views
Skip to first unread message

Heiko

unread,
Apr 27, 2005, 7:26:35 AM4/27/05
to

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

unread,
Apr 27, 2005, 10:37:30 AM4/27/05
to
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

unread,
Apr 27, 2005, 12:01:32 PM4/27/05
to
Rich Teer wrote:

> 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).

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

unread,
Apr 27, 2005, 1:49:30 PM4/27/05
to
Heiko <heiko.noordhof_A_xs4all.nl> wrote:
>Rich Teer wrote:
>
>> 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).
>
>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) fl...@barrow.com

SM Ryan

unread,
Apr 27, 2005, 2:02:52 PM4/27/05
to
# 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

unread,
Apr 27, 2005, 2:24:04 PM4/27/05
to
Rich Teer <rich...@rite-group.com> wrote:
> 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.)

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

Reply all
Reply to author
Forward
0 new messages