Python获取硬盘信息

时间:2012-12-14 23:22:36

标签: python linux

有人可以了解我可以使用哪些命令从硬盘获取以下内容。最好是有一个内部Python模块,但可能没有。我有root访问权限以获取任何信息,但我希望远离必须下载额外的包,所以:

procinfoswaponhdparmhardinfo不在图片范围内,不是默认的Fedora 17,不确定其他发行版。

Index (Device 0, could be more)
Description (Full Name, Western Digital Black Edition.....)
InterfaceType (IDE, SCSI)
Manufacturer (Western Digital, Seagate, Maxtor)
Model (WDxxx)
Size (in KB, MB or GB)
Partitions (if Any)

3 个答案:

答案 0 :(得分:4)

查看sysfs。例如,对于我的Ubuntu 12.04系统上的第一个磁盘,大部分信息可以通过以下文件和目录的存在来读取或推断:

  • / SYS /块/ SDA的/ dev
  • / SYS /块/ SDA /设备/的scsi_device
  • / sys / block / sda / device / vendor(实际上是“ATA”,暗示SATA,而不是“西部数据”,可能是预期的)
  • / SYS /块/ SDA /设备/模型
  • / SYS /块/ SDA /大小
  • / SYS /块/ SDA / SDA1
  • / SYS /块/ SDA / SDA2
  • / SYS /块/ SDA / SDA5

答案 1 :(得分:2)

我想我找到了一个好的终端命令,似乎提供了我要求的很多信息,还有更多。

fdisk -l

现在问题仍然是如何解析这些数据!

fdisk-l

的输出
Disk /dev/sda: 750.2 GB, 750156374016 bytes
255 heads, 63 sectors/track, 91201 cylinders, total 1465149168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf4375e1b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      206847      102400    7  HPFS/NTFS/exFAT
/dev/sda2          206848   895822804   447807978+   7  HPFS/NTFS/exFAT
/dev/sda3      1151823870  1465147391   156661761    5  Extended
/dev/sda4       895823872  1100623871   102400000   83  Linux
/dev/sda5      1151823872  1435025407   141600768   83  Linux
/dev/sda6      1435027456  1465147391    15059968   82  Linux swap / Solaris

Partition table entries are not in disk order

Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders, total 1953525168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x3ac08bdc

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048  1953521663   976759808    7  HPFS/NTFS/exFAT

Disk /dev/sdc: 16.0 GB, 16008609792 bytes
255 heads, 63 sectors/track, 1946 cylinders, total 31266816 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1              32    31266815    15633392    7  HPFS/NTFS/exFAT

答案 2 :(得分:1)

这可能有点晚了,但是:

如果OP只是希望列出连接到系统的硬盘驱动器,请在我的系统上使用parted (in bash):输出:

[root@Bugs]#parted -m /dev/sda print devices
/dev/sda (80.0GB) 
/dev/sdb (128GB)    
/dev/sdc (6001GB)
/dev/sdd (4001GB)
/dev/sde (8002GB)
/dev/sdf (1500GB)
/dev/sdi (4001GB)
/dev/sdg (4001GB)
/dev/sdh (8002GB)
[root@Bugs]#

请注意,parted需要指定设备,但实际上并不需要。另外,对于某些驱动器,给出的实际大小为+ xGB。这些似乎是用于驱动器=> 4TB。

由于这是一个bash命令,因此python有多种实用程序来运行它们。 对我来说,这是最简单的答案/结果,对于OP,我猜是这样。希望这对以后的人有所帮助。

相关问题