Simple Code to get Hard Disk information in Linux

March 29, 2005

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <fcntl.h>
#include <errno.h>

int main (void) {
struct hd_driveid id;
int fd;

if (getgid()) {
   printf ("Run this with root permission]n");
   return -1;
if (!(fd = open ("/dev/hda", O_RDONLY|O_NONBLOCK))) {
   perror ("/dev/hda");
   return -1;
if(!ioctl(fd, HDIO_GET_IDENTITY, &id)) {
   printf("Serial Number=%s\n",id.serial_no);
   printf("Model Number=%s\n",id.model);
   printf("Firmware Revision=%.8s\n",id.fw_rev);
} else {
   perror ("HDIO_GET_IDENTITY");
if (!close(fd)) {
   perror ("/dev/hda");
   return -1;
return 0;

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: