Determine what device a directory is located on

  • If I do

    # cd /
    # ln -s /home test
    # cd test
    # mount --bind $PWD /mnt
    

    the entry in /proc/mounts is

    /dev/sda2 /mnt ext4 rw,noatime,data=ordered 0 0
    

    which is the device that is mounted to /home and is not easily deducible from $PWD which is /test. How can I determine which device (i.e., /dev/sda2) is going to show up in /proc/mounts in general given that the bind mount may be to a directory/file that is potentially "obscured" by symlinks, other bind mounts, etc?

  • slm

    slm Correct answer

    7 years ago

    If I understand your question you want to know which device was used for a given mount. For this you can use the df command:

    $ df -h 
    Filesystem                         Size  Used Avail Use% Mounted on
    /dev/mapper/fedora_greeneggs-root   50G   21G   27G  44% /
    devtmpfs                           3.8G     0  3.8G   0% /dev
    tmpfs                              3.8G   14M  3.8G   1% /dev/shm
    tmpfs                              3.8G  984K  3.8G   1% /run
    tmpfs                              3.8G     0  3.8G   0% /sys/fs/cgroup
    tmpfs                              3.8G  3.4M  3.8G   1% /tmp
    /dev/sda1                          477M   99M  349M  23% /boot
    /dev/mapper/fedora_greeneggs-home  402G  184G  198G  49% /home
    

    To find which device a particular file/directory is found on, give the file as an argument to df. Using your example:

    $ df -h /mnt
    Filesystem                         Size  Used Avail Use% Mounted on
    /dev/sda1                          477M   99M  349M  23% /
    

    You can also use the mount command:

    $ mount | grep '^/dev'
    /dev/mapper/fedora_greeneggs-root on / type ext4 (rw,relatime,seclabel,data=ordered)
    /dev/sda1 on /boot type ext4 (rw,relatime,seclabel,data=ordered)
    /dev/mapper/fedora_greeneggs-home on /home type ext4 (rw,relatime,seclabel,data=ordered)
    

    The directory mounted for each device is the 3rd argument in the output above. So for device /dev/sda1 would be /boot. The other devices are making use of LVM (Logical Volume Management) and would need to be further queried to know which actual device is being used by LVM.

    If `$PWD` (which is what I am mounting) is buried in a series of symlinks, bind mounts, etc then I would need to recursively examine the path for mount points.

    With bind mounts despite what shows up in `/proc/mounts` the "thing" that is mounted, at least in my mind, is not the device it is the directory/file.

    @StrongBad - what does `readlink -f /mnt` show?

    @StrongBad if you have to deal with determining the mount point/device when obscured by symlinks, you should put that in your question. It'll make it much easier to get the right answer.

    `readlink -f /mnt` gives `/mnt`

    I think you're relegated to walking it recursively then. But just so we're clear can you please update your Q with what Patrick mentioned?

    @Patrick done, I think

    @StrongBad if it's a link, you could get the device name with `df $(readlink -f /path/to/file) | tail -n 1 | cut -d ' ' -f 1`.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM