Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there a simple command that takes a disk's device node as input, and tells me where (and whether) that disk is mounted? Is it possible to get the mount point by itself, so I can pass it to another command?

I'm working on a Debian Squeeze live system with a minimal install (I can install extra packages if need be).

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

Under Linux, you can get mount point information directly from the kernel in /proc/mounts. The mount program records similar information in /etc/mtab. The paths and options may be different, as /etc/mtab represents what mount passed to the kernel whereas /proc/mounts shows the data as seen inside the kernel. /proc/mounts is always up-to-date whereas /etc/mtab might not be if /etc was read-only at some point that wasn't expected by the boot scripts. The format is similar to /etc/fstab.

In both files, the first whitespace-separated field contains the device path and the second field contains the mount point.

awk -v needle="$device_path" '$1==needle {print $2}' /proc/mounts

or if you don't have awk:

grep "^$device_path " /proc/mounts | cut -f 2

There are a number of edge cases where you might not get what you expect. If the device was mounted via a different path in /dev that designates the same device, you won't notice it this way. In /proc/mounts, bind mounts are indistinguishable from the original. There may be more than one match if a mount point shadows another (this is unusual).

In /proc/self or /proc/$pid, there is a per-process mounts file that mimics the global file. The mount information may vary between processes, for example due to chroot. There is an additional file called mountinfo that has a different format and includes more information, in particular the device major and minor numbers. From the documentation:

36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
(1)(2)(3)   (4)   (5)      (6)      (7)   (8) (9)   (10)         (11)

(1) mount ID:  unique identifier of the mount (may be reused after umount)
(2) parent ID:  ID of parent (or of self for the top of the mount tree)
(3) major:minor:  value of st_dev for files on filesystem
(4) root:  root of the mount within the filesystem
(5) mount point:  mount point relative to the process's root
(6) mount options:  per mount options
(7) optional fields:  zero or more fields of the form "tag[:value]"
(8) separator:  marks the end of the optional fields
(9) filesystem type:  name of filesystem of the form "type[.subtype]"
(10) mount source:  filesystem specific information or "none"
(11) super options:  per super block options

So if you're looking for a device by number, you can do it like this:

awk -v dev="$major:minor" '$3==dev {print $5}'
awk -v dev="$(stat -L -c %t:%T /dev/block/something)" '$3==dev {print $5}'
share|improve this answer
    
"If the device was mounted via a different path in /dev that designates the same device, you won't notice it this way." — That is actually the case: I have the udisks symlink (in /dev/disk/by-label/...). But I can resolve to a device node using readlink -f. I like the /proc/mounts idea, I think that might be a bit more reliable than /etc/mtab. –  detly Mar 22 '12 at 23:59
    
@detly Or you can use mountinfo. Call stat to get the device numbers (see my updated answer, I forgot to include the command earlier). –  Gilles Mar 23 '12 at 0:04
add comment

The mount command with no arguments will list all currently mounted filesystems; you can grep that for the disk you want (or grep /etc/mtab, which is the file mount reads the information from):

$ grep /dev/sda /etc/mtab
/dev/sda3 /boot ext2 rw,noatime 0 0
share|improve this answer
    
That has a lot of extraneous text though. I need to be able to pass the mount point to other commands. (Sorry, I've clarified that in the Q.) –  detly Mar 22 '12 at 7:31
    
Ah, grep ${NODE} /etc/mtab | cut -d ' ' -f 2 should do it. –  detly Mar 22 '12 at 7:33
    
Although grep ^"${NODE}[[:space:]]\+" /etc/mtab | cut -d ' ' -f 2" is much less fragile... –  detly Mar 22 '12 at 7:41
add comment

Look at this question. statseems to be a good choice for your needs.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.