5

I need to test in a script, whether a file is from a network mount or truly local. To be more clear: I need to test, whether parsing the contents will be fast or slow, but for my case local vs network is a reliable indicator.

The best attempt I've come up with is stat -c %m [path] to get the mount point for the path, which gives / for files on my local disk and the mounting point for a CIFS mount.

But I suspect that this isn't a reliable diagnostic beyond my very simple config (one local drive, one big partition, a couple of network mounts) so I'd like a robust/canonical approach. Searching hasn't given any useful leads; terms like “network” and “path” are just too overloaded.

  • 1
    Not where I can post a full answer but you might try using df to get the device it's coming from and then grep for it in mtab to see what filesystem it's mounted as. You have to control for double mounts somehow though. – Bratchley Jul 2 '14 at 11:42
  • Also need to control for iscsi and fibre channel since "ext3" still doesn't necessarily mean local. – Bratchley Jul 2 '14 at 11:44
  • Although I guess double mounts don't matter for this. Just pick the first one. – Bratchley Jul 2 '14 at 11:47
  • Thanks - those are exactly the sort of real-world oddities I'm wary of being inadvertently dependent on. – Tom Goodfellow Jul 2 '14 at 14:11
  • Looking at network traffic it's clear that the poor network performance comes from the enormous amount of seeking and re-reading (processing a 1.6MB file generates 3.2GB of SMB reads). So plausibly better (any?) caching of the network mount would largely eliminate the need to distinguish local and network locations, though that's a separate topic. – Tom Goodfellow Jul 2 '14 at 14:15
1

You can output the type of the filesystem that contains a given file or directory using

 stat -f --format="%T" /path/to/file

and take action based on that. Some possible outputs are cifs, nfs, afs, … (presumed remote) and ufs, ext2/ext3 (sic - ext2, ext3, and ext4 have the same filesystem magic number), btrfs, tmpfs, … (presumed local).

One thing that can help you decide if a filesystem type is local or remote: GNU coreutils, which includes stat and df, has a notion of "local" and "remote" filesystem for a few dozen different filesystem types.

df -T -l | awk 'NR > 1 { print $2 }' | sort -u

will output the filesystem types of all mounted filesystems that df believes are local.

As pointed out in the comments on the original question, it is difficult to tell whether a storage device is local; an ext3 filesystem on /dev/sdb5 could be on a fibre channel device, which could be directly attached or could be several network switches away, which you are unlikely to be able to discern using standard user-level utilities.

  • @Gilles, what version of stat do you have that will output styles ext3 or ext4? I looked at the coreutils source again and do not see them in human_fstype(). My tests on an ext4 filesystem shows an fstype of ext2/ext3. – Mark Plotnick Jul 2 '14 at 22:10
  • Oh, sorry, you're right. I wanted to show a few more examples in each category and got carried away. It's ext2/ext3 for ext2, ext3 and ext4. And, unhelpfully, fuseblk for anything that uses FUSE, it doesn't try to dig further. – Gilles Jul 2 '14 at 22:12
  • @all - thanks very much for the answers: I don't imagine the util will be meeting a remote fibre channel device any time soon so it's probably robust enough now. – Tom Goodfellow Jul 3 '14 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.