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
/dev/sda2 /mnt ext4 rw,noatime,data=ordered 0 0
which is the device that is mounted to
/homeand is not easily deducible from
/test. How can I determine which device (i.e., /dev/sda2) is going to show up in
/proc/mountsin general given that the bind mount may be to a directory/file that is potentially "obscured" by symlinks, other bind mounts, etc?
If I understand your question you want to know which device was used for a given mount. For this you can use the
$ 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 | 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
/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 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.
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?