Skip to content

Commit

Permalink
fix: return proper disk size from GetDisks function
Browse files Browse the repository at this point in the history
Actually `size` file in `/sys/block/sd*/size` is amount of blocks, not
size in bytes.
Get block size from another place and multiply two values to get
correct size in bytes.

Signed-off-by: Artem Chernyshev <[email protected]>
  • Loading branch information
Unix4ever authored and talos-bot committed Nov 18, 2020
1 parent 8742133 commit 8076344
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion blockdevice/util/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"))
Expand All @@ -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 = ""
Expand Down

0 comments on commit 8076344

Please sign in to comment.