Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ func findDisks(disk, syspath string) (map[string][]partitionData, error) {
if err != nil {
return nil, err
}
end := size - start + 1
// sysfs reports `size` as the partition's length in sectors
// (not its last LBA), so the inclusive last sector is
// start + size - 1. The disk-image branch above uses the
// same formula.
end := start + size - 1
// read from uevent to get name
ueventPath := filepath.Join(sysClassBlockPath, candidate.Name(), name, "uevent")
ueventData, err := os.ReadFile(ueventPath)
Expand Down
8 changes: 7 additions & 1 deletion discover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,17 @@ func TestFindDisks(t *testing.T) {
if pd.label != "foo" {
t.Errorf("pd.label = %q, want foo", pd.label)
}
// start and size in bytes (blockSize=512)
// start, size, and end in bytes (blockSize=512). End is the
// inclusive last byte of the partition, i.e.
// (start_sector + size_sectors - 1) * blockSize.
if pd.start != 2*512 || pd.size != 4*512 {
t.Errorf("(start,size) = (%d,%d), want (%d,%d)",
pd.start, pd.size, 2*512, 4*512)
}
expectedEnd := int64((2+4-1) * 512)
if pd.end != expectedEnd {
t.Errorf("pd.end = %d, want %d", pd.end, expectedEnd)
}
})
t.Run("single", func(t *testing.T) {
// restrict to explicit disk
Expand Down