LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-29-2010, 11:46 PM   #1
anu_123
LQ Newbie
 
Registered: Apr 2010
Posts: 10

Rep: Reputation: 0
Question How to get Hard Disk serial number using C++ program in linux


i need to get the hard disk serial number of my machine using a C++ program,can anybody help with a sample program or similar solutions

Thanks in advance.
 
Old 04-30-2010, 03:11 AM   #2
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2016 CentOS6.9 CentOS7.3 + 50+ other Linux OS, for test only.
Posts: 16,635

Rep: Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268
Welcome to LQ.

You can just use the command : # hdparm -i /dev/sda
Or : # hdparm -i /dev/sda | grep -i serial
http://nixcraft.com/centos-rhel-fedo...l-no-info.html
..
 
1 members found this post helpful.
Old 05-01-2010, 12:28 AM   #3
fpmurphy
Member
 
Registered: Jan 2009
Location: /dev/ph
Distribution: Fedora, Ubuntu, Redhat, Centos
Posts: 299

Rep: Reputation: 62
Here is a short C utility which will show you how to do it
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>

int 
main(int argc, 
     char *argv[])
{
    static struct hd_driveid hd;
    int fd;

    if (geteuid() >  0) {
        printf("ERROR: Must be root to use\n");
        exit(1);
    }

    if ((fd = open(argv[1], O_RDONLY|O_NONBLOCK)) < 0) {
        printf("ERROR: Cannot open device %s\n", argv[1]);
        exit(1);
    }

    if (!ioctl(fd, HDIO_GET_IDENTITY, &hd)) {
        printf("Hard Disk Model: %.40s\n", hd.model);
        printf("  Serial Number: %.20s\n", hd.serial_no);
    } else if (errno == -ENOMSG) {
        printf("No hard disk identification information available\n");
    } else {
        perror("ERROR: HDIO_GET_IDENTITY");
        exit(1);
    }

    exit(0);
}
 
1 members found this post helpful.
Old 05-03-2010, 08:46 AM   #4
anu_123
LQ Newbie
 
Registered: Apr 2010
Posts: 10

Original Poster
Rep: Reputation: 0
thank you for replying

@fpmurphy:
Every time i compile and run this program it gives me the error as follows
ERROR: Cannot open device (null)

thanks

@knudfl;

i used the command it worked..but i want this in the form of a c or cpp code..


thanks

Last edited by anu_123; 05-03-2010 at 08:51 AM.
 
Old 05-03-2010, 09:10 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,811

Rep: Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190
Quote:
Originally Posted by anu_123 View Post
thank you for replying
@fpmurphy:
Every time i compile and run this program it gives me the error as follows
ERROR: Cannot open device (null)
thanks

@knudfl;
i used the command it worked..but i want this in the form of a c or cpp code..
thanks
Ok...you do realize that you will actually have to modify that code, and do some work on your own, to make it function, right??? That was sample code, to show you how to to it yourself. We're not going to write code for you.
 
Old 05-03-2010, 09:46 AM   #6
anu_123
LQ Newbie
 
Registered: Apr 2010
Posts: 10

Original Poster
Rep: Reputation: 0
is there any way that i can write a code in cpp which will execute the command "hdparm -i /dev/hda" command and then i can get the hdd serial number ??


thanks
 
Old 05-03-2010, 12:49 PM   #7
broken
Member
 
Registered: Apr 2010
Location: Your mom's trailer
Distribution: NetBSD
Posts: 31

Rep: Reputation: 15
Quote:
Originally Posted by anu_123 View Post
Every time i compile and run this program it gives me the error as follows
ERROR: Cannot open device (null)
How exactly are you running the program?
 
Old 05-03-2010, 12:52 PM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,811

Rep: Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190
Quote:
Originally Posted by anu_123 View Post
is there any way that i can write a code in cpp which will execute the command "hdparm -i /dev/hda" command and then i can get the hdd serial number ??
thanks
Yes, there is, easily. You'd do it the same way you would run any other system command.

However, you haven't yet shown us any of the work you've done, or what you've tried.
 
Old 05-03-2010, 11:13 PM   #9
anu_123
LQ Newbie
 
Registered: Apr 2010
Posts: 10

Original Poster
Rep: Reputation: 0
i tried with the following code:

#include <stdlib.h>
#include <iostream>
#include <stdio.h>

int main()
{
system("hdparm -i /dev/hda | grep -i serial");

}

and i got the output as:

[root@localhost anuradha]# g++ hddid.cpp -o hddid
hddid.cpp:9:2: warning: no newline at end of file
[root@localhost anuradha]# ./hddid
Model=SAMSUNG SP0802N, FwRev=TK200-04, SerialNo=S00JJ20X897934
[root@localhost anuradha]#


what i need is.. i should be able to use the output further in my code...
i dont have any handle or variable where this output is stored..
so that i can use it further..

Last edited by anu_123; 05-03-2010 at 11:22 PM.
 
Old 05-04-2010, 08:04 AM   #10
fpmurphy
Member
 
Registered: Jan 2009
Location: /dev/ph
Distribution: Fedora, Ubuntu, Redhat, Centos
Posts: 299

Rep: Reputation: 62
Quote:
Every time i compile and run this program it gives me the error as follows
ERROR: Cannot open device (null)
That is because you are not providing an argument to the command such as "/dev/sda"
 
Old 05-04-2010, 08:23 AM   #11
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2016 CentOS6.9 CentOS7.3 + 50+ other Linux OS, for test only.
Posts: 16,635

Rep: Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268Reputation: 3268
Quote:
What i need is..
I should be able to use the output further in my code...
Well, I guess you in some way can redirect the result
and read it later.
....... Like you would do with a command:
hdparm -i /dev/hda | grep -i serial > /var/log/serial.no
 
  


Reply

Tags
serial


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Best/Right way to extract disk serial number? itnaa Linux - Hardware 4 08-21-2007 11:13 AM
edit volume serial number (VSN) on floppy disk nvbauer Linux - Hardware 11 08-12-2005 07:44 AM
Hard Drive Serial Number? daver Linux - Hardware 4 03-17-2004 09:57 AM
Serial ATA Hard Disk Drives under Linux teresa Linux - Hardware 1 02-23-2004 02:26 PM
help me in finding out the hard disk number in Linux OS balki_27 Programming 0 08-04-2001 08:34 AM


All times are GMT -5. The time now is 02:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration