jaypipes / ghw

Go HardWare discovery/inspection library
Apache License 2.0
1.64k stars 181 forks source link

Incorrect size shown for extended partition #126

Open chatenilesh opened 5 years ago

chatenilesh commented 5 years ago
nyc@nyc:~/go/src/github.com/chatenilesh/ghw$ go run cmd/ghwc/main.go 
block storage (3 disks, 2TB physical storage)
 /dev/sda HDD (100GB) SCSI [@pci-0000:00:0d.0-ata-1 (node #0)] vendor=ATA model=VBOX_HARDDISK serial=VB4d5f4f53-a3364e08
  /dev/sda1 (100GB) [ext4] mounted@/
 /dev/sdb SSD (1TB) SCSI [@pci-0000:00:0d.0-ata-2 (node #0)] vendor=ATA model=VBOX_HARDDISK serial=VB6386b7e6-97617c1b
  /dev/sdb1 (898GB) 
  /dev/sdb2 (1KB)
  /dev/sdb5 (49GB) 
  /dev/sdb6 (45GB) 
  /dev/sdb7 (24GB) 
  /dev/sdb8 (4GB)
nyc@nyc:~/Documents$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 1 TiB, 1099511627776 bytes, 2147483648 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
Disklabel type: dos
Disk identifier: 0x13ce1e72

Device     Boot      Start        End    Sectors  Size Id Type
/dev/sdb1  *          2048 1881196543 1881194496  897G 83 Linux
/dev/sdb2       1881196544 2147483647  266287104  127G  5 Extended
/dev/sdb5       1881198592 1983643647  102445056 48.9G 83 Linux
/dev/sdb6       1983645696 2076295167   92649472 44.2G 83 Linux
/dev/sdb7       2090141696 2139455487   49313792 23.5G 83 Linux
/dev/sdb8       2139457536 2147483647    8026112  3.8G  7 HPFS/NTFS/exFAT
nyc@nyc:~/Documents$ 

Of all the systems I checked, only /sys/block/sdb/size (disk size) was reliable, partition size was wrong for special partitions like extended, boot, swap, etc. I think getting size from /sys/block/sdb/sdb2/size is not correct?. I think partition size must be obtained from /proc/partitions or read MBR, any thoughts?

jaypipes commented 5 years ago

Hi again @chatenilesh :) It seems you've indeed stumbled on a bug here.

Are you interested in submitting a fix? Keep in mind that the fix should try as much as possible to read from non-privileged (pseudo)files like /proc/partitions (not the MBR itself which I'm pretty sure would require root privileges and not a shell out to fdisk since that also requires root privileges to execute).

I would be interested to see patches (and possibly separate github issues) that address the following apparent bugs:

1) extended partitions clearly are not being properly sized. As your output shows, the size shown by ghw is 1K but the actual size of the extended partition (sdb2) is the sum of all its component partitions (sdb5, sdb6, sdb7).

2) ghw isn't even picking up the HPFS/NTFS/exFAT partition (sdb8). I'm not sure why and would appreciate some investigation here into what exactly that partition contains on your system. Is that a swap partition of some sort? Is it a USB drive that has been temporarily inserted?

Can you do me a favor and show me what the output of the follow is?

go run cmd/ghwc/main.go block -f yaml

Thanks! -jay

chatenilesh commented 5 years ago

Hi, I too what a solution without parsing command outputs or reading MBR as it requires root privileges. So far I haven't come across any clean solution, so for now I am reading details from MBR. I would like to avoid reading MBR and submit a fix once I get a better solution.

  1. I have already posted a question on unix.stackexchange (https://unix.stackexchange.com/questions/502444/how-to-check-if-partition-is-extended-primary-in-linux) (https://unix.stackexchange.com/questions/502682/significance-of-sys-block-sd-sd-size) but haven't received any satisfactory comments. Will have to get in touch with some Linux filesystem expert :)
  2. Please ignore this. I missed the line while posting the output of command. I have updated the comment accordingly. BTW its a partition on secondary HDD and not USB drive.
nyc@nyc:~/go/src/github.com/chatenilesh/ghw$ go run cmd/ghwc/main.go block -f yaml
block:
  disks:
  - bus_path: pci-0000:00:0d.0-ata-1
    drive_type: hdd
    model: VBOX_HARDDISK
    name: sda
    partitions:
    - label: ""
      mount_point: /
      name: sda1
      read_only: false
      size_bytes: 107372085248
      type: ext4
    physical_block_size_bytes: 512
    serial_number: VB4d5f4f53-a3364e08
    size_bytes: 107374182400
    storage_controller: scsi
    vendor: ATA
    wwn: unknown
  - bus_path: pci-0000:00:0d.0-ata-2
    drive_type: ssd
    model: VBOX_HARDDISK
    name: sdb
    partitions:
    - label: ""
      mount_point: ""
      name: sdb1
      read_only: true
      size_bytes: 963171581952
      type: ""
    - label: ""
      mount_point: ""
      name: sdb2
      read_only: true
      size_bytes: 1024
      type: ""
    - label: ""
      mount_point: ""
      name: sdb5
      read_only: true
      size_bytes: 52451868672
      type: ""
    - label: ""
      mount_point: ""
      name: sdb6
      read_only: true
      size_bytes: 47436529664
      type: ""
    - label: ""
      mount_point: ""
      name: sdb7
      read_only: true
      size_bytes: 25248661504
      type: ""
    - label: ""
      mount_point: ""
      name: sdb8
      read_only: true
      size_bytes: 4109369344
      type: ""
    physical_block_size_bytes: 512
    serial_number: VB6386b7e6-97617c1b
    size_bytes: 1099511627776
    storage_controller: scsi
    vendor: ATA
    wwn: unknown
  - bus_path: pci-0000:00:01.1-ata-2
    drive_type: odd
    model: VBOX_CD-ROM
    name: sr0
    partitions: []
    physical_block_size_bytes: 2048
    serial_number: VB2-01700376
    size_bytes: 85917696
    storage_controller: scsi
    vendor: VBOX
    wwn: unknown
  total_size_bytes: 1206971727872