Results 1 to 6 of 6
Hi all!
I want to make a program that runs some tests, and one of them is "hdparm -tT /dev/hda". But it can be /dev/hdb or /dev/sda or ... and ...
- 03-05-2006 #1Just Joined!
- Join Date
- Mar 2006
- Posts
- 5
A command that tells the name of the current HDD
Hi all!

I want to make a program that runs some tests, and one of them is "hdparm -tT /dev/hda". But it can be /dev/hdb or /dev/sda or ... and so on. I want to run this benchmark on the current HDD, so I need a command that returns the name of the hard this, it is ran on. For example, when I run "whoami", it returns "shiro" (current user). I want to run "this_command" and I want it to return "/dev/hda" (current drive). I will use popen() to catch the string, that the command returns and everything's OK.
If there isn't such command, please say some alternatives. 10x in advance!
- 03-05-2006 #2
On my system, this pipeline will work:
Code:mount | grep ' / ' | cut -f1 -d' '
- 03-05-2006 #3Just Joined!
- Join Date
- Mar 2006
- Posts
- 5
No! This always returns the root drive wherever I am. When I'm in "/" it returns "dev/hda5" and when I'm in "/c", which is "/dev/hda1", it returns again "/dev/hda5".
Originally Posted by lakerdonald
By the way, for hdparm is needed only the drive, not the partition. "/dev/hda" for example. It might be easier to catch only the drive.
- 03-05-2006 #4Just Joined!
- Join Date
- Mar 2006
- Posts
- 34
Try this shell script:
Code:dir=`pwd` while true ; do hdd=`grep " $dir " /etc/mtab` if [ -n "$hdd" ] ; then echo `echo $hdd | cut -f 1 -d ' '` break fi dir=`dirname $dir` done
- 03-05-2006 #5Just Joined!
- Join Date
- Mar 2006
- Posts
- 5
@jesuswaffle: yeah, good! But I think it's a little bit strange to solve such a task on such ... unnatural way

Have a look at that:In the string variable "s", I take the name of the current direcotry. Then in fd I take it's file descriptor (the directory is a file anyway). Then I use fstat() to take all the information about the current directory in the struct "status".Code:struct stat status; int fd; char s[MAX_LENGTH]; FILE *out; out = popen("pwd", "r"); fgets(s, MAX_LENGTH, out); s[strlen(s) - 1] = 0; pclose(out); fd = open(s, O_RDONLY); fstat(fd, &status);
Now in status.st_dev I have the major/minor code of the current drive. 773 for example. And now here's the new question: how can I convert the code of the drive to a string, that looks like "/dev/hda5"? Is there such a function, or I am supposed to write one myself? Because if there isn't, then jesuswaffle's way is certainly better.
- 03-06-2006 #6Just Joined!
- Join Date
- Mar 2006
- Posts
- 34
The difficulty is that there is no one-to-one mapping between devices nodes and device numbers. If you're using static device nodes (made by mknod), then you can call them whatever you want, and put them wherever you want. You can also have zero, or more than one, device node per device. If you're using udev instead of static device nodes, you can still mount the udev filesystem wherever you want, and possibly at multiple places. Therefore, I would hazard to guess that the function you desire does not exist.


Reply With Quote
