How to Find the Mountpoint a File Resides On

How to find the mountpoint a file resides on?

You may either call the mount command and parse its output to find the longest common prefix with your path, or use the stat system call to get the device a file resides on and go up the tree until you get to a different device.

In Python, stat may be used as follows (untested and may have to be extended to handle symlinks and exotic stuff like union mounts):

def find_mount_point(path):
path = os.path.abspath(path)
orig_dev = os.stat(path).st_dev

while path != '/':
dir = os.path.dirname(path)
if os.stat(dir).st_dev != orig_dev:
# we crossed the device border
break
path = dir
return path

Edit: I didn't know about os.path.ismount until just now. This simplifies things greatly.

def find_mount_point(path):
path = os.path.abspath(path)
while not os.path.ismount(path):
path = os.path.dirname(path)
return path

With Powershell, how to find mount point drive given a file full name

Use LinkType and Target properties of a file/directory object and work your way to the root:

function Get-ReparsePoints($path) {
try { $file = Get-Item -literal $path } catch {}
while ($file) {
if ($file.LinkType) {
[PSCustomObject]@{
path = $file.FullName
target = $file.Target
type = $file.LinkType
}
}
$file = if ($file.Parent) { $file.Parent } else { $file.Directory }
}
}

Tested in PowerShell 5:

Get-ReparsePoints 'D:\lost\kill\!shared\mv\Lindsey Stirling\Radioactive.mp4'

path target type
---- ------ ----
D:\lost\kill\!shared\mv {D:\lost\kill\mv} Junction
D:\lost {Volume{b2cd25bc-98e0-4cb1-bf51-f090f8dfda43}\} Junction

Given the directory name, how to find the Filesystem on which it resides in C?

df `pwd`

...Super simple, works, and also tells you how much space is there...

[stackuser@rhel62 ~]$ pwd
/home/stackuser
[stackuser@rhel62 ~]$ df `pwd`
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda7 250056240 196130640 41223408 83% /
[stackuser@rhel62 ~]$ cd isos
[stackuser@rhel62 isos]$ pwd
/home/stackuser/isos
[stackuser@rhel62 isos]$ df `pwd`
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda5 103216920 90417960 11750704 89% /mnt/sda5
[stackuser@rhel62 isos]$ df $(pwd)
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda5 103216920 90417960 11750704 89% /mnt/sda5

...which is the likely cause of the mount point query in the first place.

Note those are backticks, and the alternate (modern) method, providing further control over slashes and expansion is df $(pwd). Tested and traverses symlinks correctly on bash, dash, busybox, zsh. Note that tcsh won't like the $(...), so stick to the older backtick style in csh-variants.

There are also extra switches in pwd and df for further enjoyment.

How can I determine the mount path of a given file on Linux in C?

You can do it with a combination of getfsent to iterate through the list of devices, and stat to check if your file is on that device.

#include <fstab.h>    /* for getfsent() */
#include <sys/stat.h> /* for stat() */

struct fstab *getfssearch(const char *path) {
/* stat the file in question */
struct stat path_stat;
stat(path, &path_stat);

/* iterate through the list of devices */
struct fstab *fs = NULL;
while( (fs = getfsent()) ) {
/* stat the mount point */
struct stat dev_stat;
stat(fs->fs_file, &dev_stat);

/* check if our file and the mount point are on the same device */
if( dev_stat.st_dev == path_stat.st_dev ) {
break;
}
}

return fs;
}

Note, for brevity there's no error checking there. Also getfsent is not a POSIX function, but it is a very widely used convention. It works here on OS X which doesn't even use /etc/fstab. It is also not thread safe.

Where is a file mounted?

This is what I've come up with. It turns out there's usually no need to iterate through the parent directories. All you have to do is get the file's device number and then find the corresponding mount entry with the same device number.

struct mntent *mountpoint(char *filename, struct mntent *mnt, char *buf, size_t buflen)
{
struct stat s;
FILE * fp;
dev_t dev;

if (stat(filename, &s) != 0) {
return NULL;
}

dev = s.st_dev;

if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
return NULL;
}

while (getmntent_r(fp, mnt, buf, buflen)) {
if (stat(mnt->mnt_dir, &s) != 0) {
continue;
}

if (s.st_dev == dev) {
endmntent(fp);
return mnt;
}
}

endmntent(fp);

// Should never reach here.
errno = EINVAL;
return NULL;
}

Thanks to @RichardPennington for the heads up on realpath(), and on comparing device numbers instead of inode numbers.

How to find out mount/partition a directory or file is on? (Linux Server)

df -P file/goes/here | tail -1 | cut -d' ' -f 1

How to check the given folder is a mount point

These functions can be used for data structure access the mtab file:

FILE * setmntent(const char *file, const char *mode)

int endmntent (FILE *stream)

struct mntent * getmntent (FILE *stream)

struct mntent * getmntent_r (FILE *stream, struct mentent *result, char *buffer, int bufsize)

int addmntent (FILE *stream, const struct mntent *mnt)

char * hasmntopt (const struct mntent *mnt, const char *opt)

For more details about these functions refer the man page.



Related Topics



Leave a reply



Submit