Skip to content

Instantly share code, notes, and snippets.

@Sebastian-Roth
Created October 24, 2017 21:07
Show Gist options
  • Save Sebastian-Roth/026b977b0006bce8f81acec5bc3425a4 to your computer and use it in GitHub Desktop.
Save Sebastian-Roth/026b977b0006bce8f81acec5bc3425a4 to your computer and use it in GitHub Desktop.
Get HDD infos like serial
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <unistd.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"); //===>> Must be root to use
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment