2

Let's say that /var/log is /dev/sda2, and /var/spool is part of /var which is part of / which is /dev/sda1.

Now I'd like to issue a command like

$ sudo get_mountpoint_of /var/spool

and get the correct answer:

/var/spool is part of / and is mounted on /dev/sda1
Carsten
  • 21

3 Answers3

2

on a linux system you can do:

findmnt -nvoSOURCE -T /*some/path

...to print only the source-device for whatever file is referred to by /*some/path.


options breakdown:


  • -n

    • omits column headings
  • -oSOURCE

    • selects only the SOURCE device output column, though there are many others.

    • append ,TARGET to list the mount-point column as well.

  • -v

    • omits the [...] portion of any possible /dev/device[/bindmnt] result
  • -T

    • instructs findmnt to work backwards through its --target argument until it finds a valid mount-point before reporting.

for more information:

man findmnt
mikeserv
  • 58,310
1

You do not need sudo to do this. You could write a script which did "df" for the starting point, and read the mount point from the output. If the mount point is not "/", then your script would recur, asking where that was mounted.

For instance, on the machine at hand, I have

$ df /var/log
Filesystem                                             1K-blocks     Used Available Use% Mounted on
/dev/disk/by-uuid/8a1efcd1-0d32-4674-aa7a-c24c2cd924fd  19751804 12437492   6310948  67% /

For generality, your script would have to recognize that (for this example), the disk information may be managed by LVM or udev, and getting details for that likely requires sudo).

The case posed by OP is simpler of course. For the same machine, I have a filesystem on a disk device:

$ df /users
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sdb1        8255928 5548304   2288248  71% /users

and again, the script would only have to look at the output of "df" to see whether to finish, or recur, asking where that was mounted.

Thomas Dickey
  • 76,765
1

For mountpoint info

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

For "part of" info

df /mountpointFromTheLastCommand | tail -1 | awk '{print $6}'

Example :

[root@xxx ~]# df -P /root/download/ | tail -1 | cut -d' ' -f 1
/dev/sda2
[root@xxx ~]# df /dev/sda2 | tail -1 | awk '{print $6}'
/

And with these informations, you can say /root/download is part of / and is mounted on /dev/sda2 =]

This is the idea, you can optimize it and make it in one line with your output you want.

Tata2
  • 305