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.

On linux, there is a /dev/root device node. This will be the same block device as another device node, like /dev/sdaX. How can I resolve /dev/root to the 'real' device node in this situation, so that I can show a user a sensible device name?

For example, I might encounter this situation when parsing /proc/mounts.

I'm looking for solutions that would work from a shell/python script but not C.

share|improve this question

migrated from stackoverflow.com Jul 28 '11 at 23:05

This question came from our site for professional and enthusiast programmers.

    
have you checked here ? linux-diag.sourceforge.net/Sysfsutils.html It recommends way to query the kernel about attached devices of all kinds, not sure, if its what you are looking for ! –  user702846 Jul 28 '11 at 23:02
add comment

4 Answers 4

up vote 8 down vote accepted

Parse the root= parameter from /proc/cmdline.

share|improve this answer
    
Wow, lightning reactions :-) –  kdt Jul 28 '11 at 23:00
    
So much safer than dereferencing a symbolic link. +1 :) –  Tim Post Jul 29 '11 at 2:38
    
This works on the three distros (fc14, rhel5, ubuntu 11.04) that I've looked at, with the slight caveat that there is an extra step needed to deal with root=UUID= type arguments. –  kdt Jul 29 '11 at 10:16
    
I'm interested in why this is safer than the readlink solution, could someone elaborate? –  opello Jun 26 '13 at 13:53
add comment

On the systems I've looked at, /dev/root is a symlink to the real device, so readlink /dev/root (or readlink -f /dev/root if you want the full path), will do it.

share|improve this answer
    
Or just ls -l /dev/root - shorter to type :) –  rozcietrzewiacz Jul 29 '11 at 6:32
    
@jankes, but then you have to parse the output of ls (he asked for something to use in a script). –  cjm Jul 29 '11 at 7:17
    
Ah, sorry - overlooked that. –  rozcietrzewiacz Jul 29 '11 at 7:46
    
Nope -- on my RHEL5 machine it's definitely not a symlink, although on an FC14 or ubuntu 11.04 machine it is. –  kdt Jul 29 '11 at 10:15
    
Does not work on archlinux. –  g33kz0r Jul 30 '13 at 18:31
show 2 more comments

Well /dev/root is just a symbolic link to the real device, so you can use readlink(2) to find out where it points from a program, or readlink(1) to do the same thing from a shell script.

share|improve this answer
    
No. Does not work on archlinux –  g33kz0r Jul 30 '13 at 18:31
add comment

Maybe I'm missing something, but what about:

mount|grep ' / '|cut -d' ' -f 1
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.