Skip to content

Commit b236a1e

Browse files
authored
Merge pull request moby#223 from thaJeztah/18.09_backport_devno
[18.09 backport] bugfix: fetch the right device number which great than 255
2 parents 49a4899 + e4cf15b commit b236a1e

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

daemon/daemon_unix.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ func getBlkioWeightDevices(config containertypes.Resources) ([]specs.LinuxWeight
174174
}
175175
weight := weightDevice.Weight
176176
d := specs.LinuxWeightDevice{Weight: &weight}
177-
d.Major = int64(stat.Rdev / 256)
178-
d.Minor = int64(stat.Rdev % 256)
177+
d.Major = int64(unix.Major(stat.Rdev))
178+
d.Minor = int64(unix.Minor(stat.Rdev))
179179
blkioWeightDevices = append(blkioWeightDevices, d)
180180
}
181181

@@ -245,8 +245,8 @@ func getBlkioThrottleDevices(devs []*blkiodev.ThrottleDevice) ([]specs.LinuxThro
245245
return nil, err
246246
}
247247
d := specs.LinuxThrottleDevice{Rate: d.Rate}
248-
d.Major = int64(stat.Rdev / 256)
249-
d.Minor = int64(stat.Rdev % 256)
248+
d.Major = int64(unix.Major(stat.Rdev))
249+
d.Minor = int64(unix.Minor(stat.Rdev))
250250
throttleDevices = append(throttleDevices, d)
251251
}
252252

daemon/daemon_unix_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ import (
66
"errors"
77
"io/ioutil"
88
"os"
9+
"path/filepath"
910
"testing"
1011

12+
"github.com/docker/docker/api/types/blkiodev"
1113
containertypes "github.com/docker/docker/api/types/container"
1214
"github.com/docker/docker/container"
1315
"github.com/docker/docker/daemon/config"
16+
"golang.org/x/sys/unix"
17+
"gotest.tools/assert"
18+
is "gotest.tools/assert/cmp"
1419
)
1520

1621
type fakeContainerGetter struct {
@@ -266,3 +271,61 @@ func TestNetworkOptions(t *testing.T) {
266271
t.Fatal("Expected networkOptions error, got nil")
267272
}
268273
}
274+
275+
const (
276+
// prepare major 0x1FD(509 in decimal) and minor 0x130(304)
277+
DEVNO = 0x11FD30
278+
MAJOR = 509
279+
MINOR = 304
280+
WEIGHT = 1024
281+
)
282+
283+
func deviceTypeMock(t *testing.T, testAndCheck func(string)) {
284+
if os.Getuid() != 0 {
285+
t.Skip("root required") // for mknod
286+
}
287+
288+
t.Parallel()
289+
290+
tempDir, err := ioutil.TempDir("", "tempDevDir"+t.Name())
291+
assert.NilError(t, err, "create temp file")
292+
tempFile := filepath.Join(tempDir, "dev")
293+
294+
defer os.RemoveAll(tempDir)
295+
296+
if err = unix.Mknod(tempFile, unix.S_IFCHR, DEVNO); err != nil {
297+
t.Fatalf("mknod error %s(%x): %v", tempFile, DEVNO, err)
298+
}
299+
300+
testAndCheck(tempFile)
301+
}
302+
303+
func TestGetBlkioWeightDevices(t *testing.T) {
304+
deviceTypeMock(t, func(tempFile string) {
305+
mockResource := containertypes.Resources{
306+
BlkioWeightDevice: []*blkiodev.WeightDevice{{Path: tempFile, Weight: WEIGHT}},
307+
}
308+
309+
weightDevs, err := getBlkioWeightDevices(mockResource)
310+
311+
assert.NilError(t, err, "getBlkioWeightDevices")
312+
assert.Check(t, is.Len(weightDevs, 1), "getBlkioWeightDevices")
313+
assert.Check(t, weightDevs[0].Major == MAJOR, "get major device type")
314+
assert.Check(t, weightDevs[0].Minor == MINOR, "get minor device type")
315+
assert.Check(t, *weightDevs[0].Weight == WEIGHT, "get device weight")
316+
})
317+
}
318+
319+
func TestGetBlkioThrottleDevices(t *testing.T) {
320+
deviceTypeMock(t, func(tempFile string) {
321+
mockDevs := []*blkiodev.ThrottleDevice{{Path: tempFile, Rate: WEIGHT}}
322+
323+
retDevs, err := getBlkioThrottleDevices(mockDevs)
324+
325+
assert.NilError(t, err, "getBlkioThrottleDevices")
326+
assert.Check(t, is.Len(retDevs, 1), "getBlkioThrottleDevices")
327+
assert.Check(t, retDevs[0].Major == MAJOR, "get major device type")
328+
assert.Check(t, retDevs[0].Minor == MINOR, "get minor device type")
329+
assert.Check(t, retDevs[0].Rate == WEIGHT, "get device rate")
330+
})
331+
}

0 commit comments

Comments
 (0)