如何使用C ++在Linux中获取硬件信息 [英] Howto get hardware information in Linux using C++

查看:818
本文介绍了如何使用C ++在Linux中获取硬件信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取Win和* nix机器上的硬盘规格.我在Linux上这样使用<hdreg.h>:

I need to get specifications of hard disk on both Win and *nix machines. I used <hdreg.h> on Linux like this:

   static struct hd_driveid hd;
   int device;
   if ((device = open("/dev/sda", O_RDONLY | O_NONBLOCK)) < 0)
   {
      cerr << "ERROR: Cannot open device /dev/sda \n";
      exit(1);
   }

   if (!ioctl(device, HDIO_GET_IDENTITY, &hd))
   {
      cout << hd.model << endl;
      cout << hd.serial_no << endl;
      cout << hd.heads << endl;
   }

我需要hd_driveid来告诉我有关磁盘的更多信息.我想知道:

I need hd_driveid to tell me some more information about disk. I want to know:

  • 分区数
  • 每个分区的规范(格式,标签,标志,大小,起点,磁道数量等)
  • 每个气缸的轨道数
  • 总曲目数
  • 最大块大小
  • 最小块大小
  • 默认块大小
  • 设备总大小

我的问题是:

  1. 有没有共同点 (与平台无关)的方式 连接硬件?我想用 win和* nix的相同代码. (即使 除了没有别的办法 将汇编代码嵌入到cpp中)
  2. 如果没有,如何在* nix中获取以上信息?
  1. Is there a common (platform-independent) way to connect hardware? I would like use same code for win and *nix. (even if there was no way other than embedding assembly code into cpp)
  2. If there isn't, how do I get above information in *nix?

推荐答案

列表中的几乎所有内容都与硬盘规格"无关:

Nearly everything in your list has nothing to do with "specifications of hard disk":

  • 分区数取决于读取分区表,如果您有任何扩展分区,则取决于这些分区的分区表.加载设备驱动程序后,操作系统通常会为您执行此操作.
  • 分区信息(即卷标)通常在分区表中不可用.您需要猜测文件系统类型并解析文件系统头.分区表中唯一的东西是类型"字节,它不能告诉您太多信息,还有开始/大小.
  • 硬盘驱动器不会为您提供真实的" CHS信息.此外,从BIOS的角度来看,驱动器提供的CHS信息是错误的"(BIOS会自己进行篡改).
  • 硬盘驱动器具有固定的扇区大小,您可以使用hd_driveid.sector_bytes来获得(通常为512,但是某些现代驱动器使用4096).我不知道最大的块大小",它是文件系统的属性.我也不确定为什么这很有用.
  • 以扇区为单位的总大小在hd_driveid.lba_capacity_2中.另外,可以使用类似的东西获得以字节为单位的大小

  • The number of partitions depends on reading the partition table, and if you have any extended partitions, the partition tables of those partitions. The OS will usually do this bit for you when the device driver loads.
  • Partition information (namely the volume label) typically isn't available in the partition table. You need to guess the file system type and parse the file system header. The only thing in the partition table is the "type" byte, which doesn't tell you all that much, and the start/size.
  • Hard drives won't give you "real" CHS information. Additionally, the CHS information that the drive provides is "wrong" from the point of view of the BIOS (the BIOS does its own fudging).
  • Hard drives have a fixed sector size, which you can get with hd_driveid.sector_bytes (usually 512, but some modern drives use 4096). I'm not aware of a maximum "block size", which is a property of the filesystem. I'm also not sure why this is useful.
  • The total size in sectors is in hd_driveid.lba_capacity_2. Additionally, the size in bytes can probably be obtained with something like

#define _FILE_OFFSET_BITS 64
#include <sys/types.h>
#include <unistd.h>

...
off_t size_in_bytes = lseek(device, 0, SEEK_END);
if (size_in_bytes == (off_t)-1) { ... error, error code in ERRNO ... }

请注意,在这两种情况下,它的大小可能都比C× H× S所计算的大小大几兆字节.

Note that in both cases, it'll probably be a few megabytes bigger than sizes calculated by C×H×S.

如果您告诉我们为什么需要此信息,这可能会有所帮助...

It might help if you told us why you wanted this information...

这篇关于如何使用C ++在Linux中获取硬件信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆