diff --git a/blockdevice/util/disks.go b/blockdevice/util/disks.go index 9b5b9c7..be7639f 100644 --- a/blockdevice/util/disks.go +++ b/blockdevice/util/disks.go @@ -35,7 +35,11 @@ func GetDisks() ([]*Disk, error) { } readFile := func(path string) (string, error) { - f, e := os.Open(filepath.Join(sysblock, path)) + if !strings.HasPrefix(path, "/") { + path = filepath.Join(sysblock, path) + } + + f, e := os.Open(path) if e != nil { return "", fmt.Errorf("failed to open file %w", err) @@ -66,6 +70,13 @@ func GetDisks() ([]*Disk, error) { continue } + blockSizeString, err := readFile( + fmt.Sprintf("/sys/class/block/%s/queue/logical_block_size", deviceName), + ) + if err != nil { + blockSizeString = "512" + } + var size uint64 s, err := readFile(filepath.Join(dev.Name(), "size")) @@ -78,6 +89,10 @@ func GetDisks() ([]*Disk, error) { continue } + blockSize, _ := strconv.ParseUint(strings.TrimSpace(blockSizeString), 10, 64) //nolint:errcheck + + size *= blockSize + model, err := readFile(filepath.Join(dev.Name(), "device/model")) if err != nil { model = ""