Find the answer to your Linux question:
Results 1 to 6 of 6
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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!

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    St. Petersburg, FL
    On my system, this pipeline will work:
    mount | grep ' / ' | cut -f1 -d' '

  3. #3
    Quote Originally Posted by lakerdonald
    On my system, this pipeline will work:
    mount | grep ' / ' | cut -f1 -d' '
    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".

    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.

  4. $spacer_open
  5. #4
    Try this shell script:
    while true ; do
        hdd=`grep " $dir " /etc/mtab`
        if [ -n "$hdd" ] ; then
            echo `echo $hdd | cut -f 1 -d ' '`
        dir=`dirname $dir`

  6. #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:
    	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;
    	fd = open(s, O_RDONLY);
    	fstat(fd, &status);
    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".
    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.

  7. #6
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts