Linux – Determine What Device a Directory Is Located On

block-devicedirectorymount

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?

Best Answer

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.

Related Question