Skip to content

problem getting total disk size in GB #436

Closed

Description

@sify21

I used following code to get total disk size:

/**
     * @return disk size, in GB
     */
    public long getTotalDisk() {
        long total=0;
        for(HWDiskStore d : info.getHardware().getDiskStores()){
            total += d.getSize();
        }
        return Math.round((double)total/1024/1024/1024);
    }

'info' is an instance of SystemInfo. It returns the correct size in GB on windows(windows 7), but on linux(centos 7) it needs to be divided by 1000 to get the correct size. Is that a hard driver vendor's problem? How do I convert disk size to GB correctly?

Activity

  1. dbwiddis commented on Dec 18, 2017

    @dbwiddis
    Member

    OSHI gets its disk size for Linux from the udev system, specifically: udev_device_get_sysattr_value(device, "size")

    This returns the size in sectors, which is multiplied by a sector size constant of 512.

    So if it's wrong, either udev is returning the value in 1000s of sectors, or the sector size is 512000. I'm not familiar with CentOS but if you can take a look at either of those outputs you can help us narrow this down.

  2. added
    bug?
    on Dec 18, 2017
  3. sify21 commented on Dec 20, 2017

    @sify21
    Author

    Hi, my hard drive type is "WDC WD10EZEX". Here is my disk info using "fdisk -l":
    1
    2
    I have a dual system. sda6~8 are centos partitions, formated as ext4. Others are windows ntfs partitions.
    In the "getTotalDisk" method, total equals 1000204886016. If divided by 1000, it matches the disk size returned by fdisk.
    Right now I don't know how to get the value returned by udev. I'll search for some udev cmd, or write some c code (which I'm not very familiar with) using the udev_device_get_sysattr_value method.

  4. dbwiddis commented on Dec 20, 2017

    @dbwiddis
    Member

    I'm a bit confused here. You said getTotalDisk returns 1000204886016 bytes. That is precisely the number shown in the fdisk -l printout above for /dev/sda.

  5. sify21 commented on Dec 21, 2017

    @sify21
    Author

    Oh, I didn't see the bytes, I just focused on the "1000.2 GB". So oshi works correctly. Maybe that's a problem relating to fdisk? I just noticed that the size of sda1~11 added manually = 1000204886016/1024/1024/1024

  6. added
    question
    and removed
    bug?
    on Dec 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions