Home » Determine what device a directory is located on

Determine what device a directory is located on


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.

On Linux we have findmnt from util-linux exactly made for this

findmnt -n -o SOURCE --target /path/to/FILE

The advantage about other solutions is that it still works if paths are obscured by symlinks or duplicate bind mounts.

The most accurate method I am aware of is to use the output of the lstat() system call. Specifically, the st_dev field. There is a command line utility, stat(1) that can be used to see this information. For example, the output of “stat /etc/issue” on my laptop:

File: ‘/etc/issue’
  Size: 65          Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d  Inode: 1610916043  Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Notice the third line, first field, “Device”. Here it lists 801h. That value can be separated into two bytes, 8 and 1. The first byte is known as the major number, the second byte is the minor number. So, the next step is to figure out what device major 8, minor 1 is.

I find consulting /proc/partitions to be the fastest. In my case, /proc/partitions has the contents:

major minor  #blocks  name

   8       16  234431064 sdb
   8       17   33554432 sdb1
   8       18  200875608 sdb2
   8        0  500107608 sda
   8        1  500106584 sda1

It’s rather clear from that output that major 8, minor 1 is sda1. We can confirm this with an ls -l /dev/sda1

brw-rw---- 1 root disk 8, 1 May  8 05:33 /dev/sda1

Notice the 8, 1 before the datestamp.

It’s important to understand/remember that the name of a device file like /dev/sda1 is only a label. The major and minor numbers are the significant, important values of the device file. If you get curious, check out the mknod(1) utility used to create device files. I could create a new /dev entry called aardvark with major 8, minor 18 with the following syntax:

mknod /dev/aardvark b 8 18

Then, I could easily mount it:

mount /dev/aardvark /mnt

and, if we look at the output of the mount command or the contents of /proc/mounts and we see:

/dev/aardvark on /mnt type xfs (rw,relatime,attr2,inode64,noquota)

df -h shows:

/dev/aardvark   192G  154G   38G  81% /mnt

… Anyways, the point of all of this is to illustrate that the important details for identifying a block device are the major and minor numbers – not the device file label – and that using the lstat() system call is the best way to query those values.

As a last comment, I just reread your question to make sure I was answering it and I realized you were asking what source device label would show up in /proc/mounts for a bind mount. That would be the same source device label as was used in the original mount(2) call for the filesystem mountpoint source for the bind mount. Perhaps an example would help:

I have /dev/sdb2 and /dev/aardvark (the same as above). They are both major 8, minor 18. Note, I will be mounting the same filesystem twice. I do the following:

mkdir /mnt1 /mnt2 /foo

mount /dev/aardvark /mnt1
mount /dev/sdb2 /mnt2

Notice that I make the directory somedir in /mnt1. But since /mnt1 and /mnt2 have the same filesystem mounted, somedir will also be reachable through /mnt2.

mkdir /mnt1/somedir

mkdir /foo/left /foo/right

mount -o bind /mnt1/somedir /foo/left
mount -o bind /mnt2/somedir /foo/right

Now, if we check /proc/mounts, we see:

/dev/aardvark /mnt1 xfs rw,relatime,attr2,inode64,noquota 0 0
/dev/sdb2 /mnt2 xfs rw,relatime,attr2,inode64,noquota 0 0
/dev/aardvark /foo/left xfs rw,relatime,attr2,inode64,noquota 0 0
/dev/sdb2 /foo/right xfs rw,relatime,attr2,inode64,noquota 0 0

The source device label on the /foo/… bind mounts is the same as the value originally supplied in the filesystem mount(2) call. Remember, /dev/aardvark and /dev/sdb2 in my example are the same device.

I realize I just typed up a novel and the first half doesn’t really answer your question at all, but it seemed like such a waste to delete it. Perhaps it’ll help someone else.

Good Luck.

P.S. Do keep in mind that some filesystems are network based – like NFS or CIFS – or are virtual – like procfs or sysfs and don’t have a source block device. I don’t know what will be returned as the device in the stat output, just for what it’s worth.

Related Solutions

Joining bash arguments into single string with spaces

[*] I believe that this does what you want. It will put all the arguments in one string, separated by spaces, with single quotes around all: str="'$*'" $* produces all the scripts arguments separated by the first character of $IFS which, by default, is a space....

AddTransient, AddScoped and AddSingleton Services Differences

TL;DR Transient objects are always different; a new instance is provided to every controller and every service. Scoped objects are the same within a request, but different across different requests. Singleton objects are the same for every object and every...

How to download package not install it with apt-get command?

Use --download-only: sudo apt-get install --download-only pppoe This will download pppoe and any dependencies you need, and place them in /var/cache/apt/archives. That way a subsequent apt-get install pppoe will be able to complete without any extra downloads....

What defines the maximum size for a command single argument?

Answers Definitely not a bug. The parameter which defines the maximum size for one argument is MAX_ARG_STRLEN. There is no documentation for this parameter other than the comments in binfmts.h: /* * These are the maximum length and maximum number of strings...

Bulk rename, change prefix

I'd say the simplest it to just use the rename command which is common on many Linux distributions. There are two common versions of this command so check its man page to find which one you have: ## rename from Perl (common in Debian systems -- Ubuntu, Mint,...

Output from ls has newlines but displays on a single line. Why?

When you pipe the output, ls acts differently. This fact is hidden away in the info documentation: If standard output is a terminal, the output is in columns (sorted vertically) and control characters are output as question marks; otherwise, the output is...

mv: Move file only if destination does not exist

mv -vn file1 file2. This command will do what you want. You can skip -v if you want. -v makes it verbose - mv will tell you that it moved file if it moves it(useful, since there is possibility that file will not be moved) -n moves only if file2 does not exist....

Is it possible to store and query JSON in SQLite?

SQLite 3.9 introduced a new extension (JSON1) that allows you to easily work with JSON data . Also, it introduced support for indexes on expressions, which (in my understanding) should allow you to define indexes on your JSON data as well. PostgreSQL has some...

Combining tail && journalctl

You could use: journalctl -u service-name -f -f, --follow Show only the most recent journal entries, and continuously print new entries as they are appended to the journal. Here I've added "service-name" to distinguish this answer from others; you substitute...

how can shellshock be exploited over SSH?

One example where this can be exploited is on servers with an authorized_keys forced command. When adding an entry to ~/.ssh/authorized_keys, you can prefix the line with command="foo" to force foo to be run any time that ssh public key is used. With this...

Why doesn’t the tilde (~) expand inside double quotes?

The reason, because inside double quotes, tilde ~ has no special meaning, it's treated as literal. POSIX defines Double-Quotes as: Enclosing characters in double-quotes ( "" ) shall preserve the literal value of all characters within the double-quotes, with the...

What is GNU Info for?

GNU Info was designed to offer documentation that was comprehensive, hyperlinked, and possible to output to multiple formats. Man pages were available, and they were great at providing printed output. However, they were designed such that each man page had a...

Set systemd service to execute after fstab mount

a CIFS network location is mounted via /etc/fstab to /mnt/ on boot-up. No, it is not. Get this right, and the rest falls into place naturally. The mount is handled by a (generated) systemd mount unit that will be named something like mnt-wibble.mount. You can...

Merge two video clips into one, placing them next to each other

To be honest, using the accepted answer resulted in a lot of dropped frames for me. However, using the hstack filter_complex produced perfectly fluid output: ffmpeg -i left.mp4 -i right.mp4 -filter_complex hstack output.mp4 ffmpeg -i input1.mp4 -i input2.mp4...

How portable are /dev/stdin, /dev/stdout and /dev/stderr?

It's been available on Linux back into its prehistory. It is not POSIX, although many actual shells (including AT&T ksh and bash) will simulate it if it's not present in the OS; note that this simulation only works at the shell level (i.e. redirection or...

How can I increase the number of inodes in an ext4 filesystem?

It seems that you have a lot more files than normal expectation. I don't know whether there is a solution to change the inode table size dynamically. I'm afraid that you need to back-up your data, and create new filesystem, and restore your data. To create new...

Why doesn’t cp have a progress bar like wget?

The tradition in unix tools is to display messages only if something goes wrong. I think this is both for design and practical reasons. The design is intended to make it obvious when something goes wrong: you get an error message, and it's not drowned in...