diff --git a/discover.go b/discover.go index 2f8a02f..99fd392 100644 --- a/discover.go +++ b/discover.go @@ -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) diff --git a/discover_test.go b/discover_test.go index 24fb180..edff615 100644 --- a/discover_test.go +++ b/discover_test.go @@ -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