How can we identify what partition a filesystem is mounted on in Linux when the partition is not directly referenced in df output?

For example, our system's fdisk output is:

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        2550    20478976   83  Linux
/dev/sda2            2550       60736   467378176   83  Linux
/dev/sda3           60736       60801      523264   82  Linux swap / Solaris

And the output from df is:

Filesystem      Size  Used Avail Use% Mounted on
rootfs           20G  881M   18G   5% /
/dev/root        20G  881M   18G   5% /
devtmpfs        989M  216K  989M   1% /dev
/dev/sda2       443G  199M  420G   1% /home
tmpfs           990M     0  990M   0% /dev/shm
/dev/root        20G  881M   18G   5% /var/named/chroot/etc/named
/dev/root        20G  881M   18G   5% /var/named/chroot/var/named
/dev/root        20G  881M   18G   5% /var/named/chroot/etc/named.conf
/dev/root        20G  881M   18G   5% /var/named/chroot/etc/named.rfc1912.zones
/dev/root        20G  881M   18G   5% /var/named/chroot/etc/rndc.key
/dev/root        20G  881M   18G   5% /var/named/chroot/usr/lib64/bind
/dev/root        20G  881M   18G   5% /var/named/chroot/etc/named.iscdlv.key
/dev/root        20G  881M   18G   5% /var/named/chroot/etc/named.root.key

From that list the only obvious partition is /dev/sda2; for the others we can't see which partition they reside on (we want to do this via a bash script).

For example, we know that /dev/root is mounted on /dev/sda1 as they both have the same 20GB size, but how can we identify this from a script?

Output from ls -l /dev/root /dev/sda1:

[root@server etc]# ls -l /dev/root /dev/sda1
ls: cannot access /dev/root: No such file or directory
brw-rw---- 1 root disk 8, 1 May  7 14:16 /dev/sda1

That output is misleading.
You actually have 2 different filesystems mounted at /.

rootfs is the filesystem created by an initramfs. When the bootloaders loads the kernel, it has the ability to load an initramfs which gets unpacked into a tmpfs filesystem. This filesystem cannot be unmounted. When the initramfs is done doing its thing, it simply mounts the real root filesystem on top of it.

The reason why df shows it as 20gb is because it can't get to that tmpfs filesystem. When df goes to show the filesystems, it first gets a list of current mounts from /etc/mtab. This gives it 2 mounts using /. It then makes a statfs() syscall on each mountpoint. But since the current root filesystem sits on top of the tmpfs initramfs, it ends up statting the current root filesystem twice.

Additionally /dev/root is actually a symlink. To find out where it really points using a shell script, you can use readlink -f /dev/root.

Update: Ok, so in this case readlink -f /dev/root does not work as /dev/root does not exist any more. Because the initramfs is what mounted your real root filesystem, /dev/root would have existed there. But since the initramfs is buried, /dev/root is not there any more.

In this case it becomes much much more difficult.
What you can do is to use stat to get the device number for the mountpoint, and then search through /dev for that.

For example, on my system this yields:

$ stat --format '%D' /

$ stat --format '%02t%02T' /dev/mapper/sys-root

$ for device in $(blkid -o device); do 
>   [ "$(stat --format '%04D' /)" = "$(stat --format '%02t%02T' "$device")" ] && \
>     echo "$device" && \
>     break
> done

Basically what this does is uses stat to get the device major and minor number of /. We then iterate through all known block devices looking for one with that same device major and minor. It's not pretty, but basically the /dev entry has a different name, and so we have to search for it.

This could be done more efficiently, but I wanted to keep the code simple

  • Our script iterates through the disk's partitions, so if it's looking at /dev/sda1 for example, how would we work out that the filesystem for that partition is /? – Chris May 12 '14 at 14:33
  • Output of readlink -f /dev/root is just /dev/root. – Chris May 12 '14 at 14:33
  • @Chris can you add ls -l /dev/root /dev/sda1 to your question? – Patrick May 12 '14 at 14:39
  • Output of that command added to question. – Chris May 12 '14 at 14:46
  • 1
    For what its worth, I think it'd be more reliable to search /proc/partition for the major and minor numbers. A /dev/ file for a mounted filesystem is not guaranteed to exist. – etherfish May 13 '14 at 7:52

/dev/root is a symlink to the real device. You should be able to get the location with either ls /dev/root, realink or looking for root= in /proc/cmdline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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