Skip to content

Commit 1a4509d

Browse files
committed
Use /proc/partitions to get device names
Signed-off-by: Alexey Ivanov <[email protected]>
1 parent b9de8a2 commit 1a4509d

2 files changed

Lines changed: 31 additions & 22 deletions

File tree

blkio.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (b *blkioController) Stat(path string, stats *v1.Metrics) error {
130130
}
131131
}
132132

133-
f, err := os.Open(filepath.Join(b.procRoot, "diskstats"))
133+
f, err := os.Open(filepath.Join(b.procRoot, "partitions"))
134134
if err != nil {
135135
return err
136136
}
@@ -335,7 +335,10 @@ func getDevices(r io.Reader) (map[deviceKey]string, error) {
335335
s = bufio.NewScanner(r)
336336
devices = make(map[deviceKey]string)
337337
)
338-
for s.Scan() {
338+
for i := 0; s.Scan(); i++ {
339+
if i < 2 {
340+
continue
341+
}
339342
fields := strings.Fields(s.Text())
340343
major, err := strconv.Atoi(fields[0])
341344
if err != nil {
@@ -352,7 +355,7 @@ func getDevices(r io.Reader) (map[deviceKey]string, error) {
352355
if _, ok := devices[key]; ok {
353356
continue
354357
}
355-
devices[key] = filepath.Join("/dev", fields[2])
358+
devices[key] = filepath.Join("/dev", fields[3])
356359
}
357360
return devices, s.Err()
358361
}

blkio_test.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,38 @@ import (
2424
v1 "github.com/containerd/cgroups/stats/v1"
2525
)
2626

27-
const data = ` 7 0 loop0 0 0 0 0 0 0 0 0 0 0 0
28-
7 1 loop1 0 0 0 0 0 0 0 0 0 0 0
29-
7 2 loop2 0 0 0 0 0 0 0 0 0 0 0
30-
7 3 loop3 0 0 0 0 0 0 0 0 0 0 0
31-
7 4 loop4 0 0 0 0 0 0 0 0 0 0 0
32-
7 5 loop5 0 0 0 0 0 0 0 0 0 0 0
33-
7 6 loop6 0 0 0 0 0 0 0 0 0 0 0
34-
7 7 loop7 0 0 0 0 0 0 0 0 0 0 0
35-
8 0 sda 1892042 187697 63489222 1246284 1389086 2887005 134903104 11390608 1 1068060 12692228
36-
8 1 sda1 1762875 37086 61241570 1200512 1270037 2444415 131214808 11152764 1 882624 12409308
37-
8 2 sda2 2 0 4 0 0 0 0 0 0 0 0
38-
8 5 sda5 129102 150611 2244440 45716 18447 442590 3688296 67268 0 62584 112984`
27+
const data = `major minor #blocks name
28+
29+
7 0 4 loop0
30+
7 1 163456 loop1
31+
7 2 149616 loop2
32+
7 3 147684 loop3
33+
7 4 122572 loop4
34+
7 5 8936 loop5
35+
7 6 31464 loop6
36+
7 7 182432 loop7
37+
259 0 937692504 nvme0n1
38+
259 1 31744 nvme0n1p1
39+
`
3940

4041
func TestGetDevices(t *testing.T) {
4142
r := strings.NewReader(data)
4243
devices, err := getDevices(r)
4344
if err != nil {
4445
t.Fatal(err)
4546
}
46-
name, ok := devices[deviceKey{8, 0}]
47-
if !ok {
48-
t.Fatal("no device found for 8,0")
49-
}
50-
const expected = "/dev/sda"
51-
if name != expected {
52-
t.Fatalf("expected device name %q but received %q", expected, name)
47+
for dev, expected := range map[deviceKey]string{
48+
deviceKey{7, 0}: "/dev/loop0",
49+
deviceKey{259, 0}: "/dev/nvme0n1",
50+
deviceKey{259, 1}: "/dev/nvme0n1p1",
51+
} {
52+
name, ok := devices[dev]
53+
if !ok {
54+
t.Fatalf("no device found for %d:%d", dev.major, dev.minor)
55+
}
56+
if name != expected {
57+
t.Fatalf("expected device name %q but received %q", expected, name)
58+
}
5359
}
5460
}
5561

0 commit comments

Comments
 (0)