Browse code

bugfix: fetch the right device number which great than 255

Signed-off-by: frankyang <yyb196@gmail.com>
(cherry picked from commit b9f31912deb511e732763e4fa5ecd0208b104eb2)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

frankyang authored on 2019/05/14 16:21:55
Showing 2 changed files
... ...
@@ -191,8 +191,8 @@ func getBlkioWeightDevices(config containertypes.Resources) ([]specs.LinuxWeight
191 191
 		}
192 192
 		weight := weightDevice.Weight
193 193
 		d := specs.LinuxWeightDevice{Weight: &weight}
194
-		d.Major = int64(stat.Rdev / 256)
195
-		d.Minor = int64(stat.Rdev % 256)
194
+		d.Major = int64(unix.Major(stat.Rdev))
195
+		d.Minor = int64(unix.Minor(stat.Rdev))
196 196
 		blkioWeightDevices = append(blkioWeightDevices, d)
197 197
 	}
198 198
 
... ...
@@ -262,8 +262,8 @@ func getBlkioThrottleDevices(devs []*blkiodev.ThrottleDevice) ([]specs.LinuxThro
262 262
 			return nil, err
263 263
 		}
264 264
 		d := specs.LinuxThrottleDevice{Rate: d.Rate}
265
-		d.Major = int64(stat.Rdev / 256)
266
-		d.Minor = int64(stat.Rdev % 256)
265
+		d.Major = int64(unix.Major(stat.Rdev))
266
+		d.Minor = int64(unix.Minor(stat.Rdev))
267 267
 		throttleDevices = append(throttleDevices, d)
268 268
 	}
269 269
 
... ...
@@ -6,12 +6,15 @@ import (
6 6
 	"errors"
7 7
 	"io/ioutil"
8 8
 	"os"
9
+	"path/filepath"
9 10
 	"testing"
10 11
 
12
+	"github.com/docker/docker/api/types/blkiodev"
11 13
 	containertypes "github.com/docker/docker/api/types/container"
12 14
 	"github.com/docker/docker/container"
13 15
 	"github.com/docker/docker/daemon/config"
14 16
 	"github.com/docker/docker/pkg/sysinfo"
17
+	"golang.org/x/sys/unix"
15 18
 	"gotest.tools/assert"
16 19
 	is "gotest.tools/assert/cmp"
17 20
 )
... ...
@@ -376,3 +379,61 @@ func sysInfo(t *testing.T, opts ...func(*sysinfo.SysInfo)) sysinfo.SysInfo {
376 376
 	}
377 377
 	return si
378 378
 }
379
+
380
+const (
381
+	// prepare major 0x1FD(509 in decimal) and minor 0x130(304)
382
+	DEVNO  = 0x11FD30
383
+	MAJOR  = 509
384
+	MINOR  = 304
385
+	WEIGHT = 1024
386
+)
387
+
388
+func deviceTypeMock(t *testing.T, testAndCheck func(string)) {
389
+	if os.Getuid() != 0 {
390
+		t.Skip("root required") // for mknod
391
+	}
392
+
393
+	t.Parallel()
394
+
395
+	tempDir, err := ioutil.TempDir("", "tempDevDir"+t.Name())
396
+	assert.NilError(t, err, "create temp file")
397
+	tempFile := filepath.Join(tempDir, "dev")
398
+
399
+	defer os.RemoveAll(tempDir)
400
+
401
+	if err = unix.Mknod(tempFile, unix.S_IFCHR, DEVNO); err != nil {
402
+		t.Fatalf("mknod error %s(%x): %v", tempFile, DEVNO, err)
403
+	}
404
+
405
+	testAndCheck(tempFile)
406
+}
407
+
408
+func TestGetBlkioWeightDevices(t *testing.T) {
409
+	deviceTypeMock(t, func(tempFile string) {
410
+		mockResource := containertypes.Resources{
411
+			BlkioWeightDevice: []*blkiodev.WeightDevice{{Path: tempFile, Weight: WEIGHT}},
412
+		}
413
+
414
+		weightDevs, err := getBlkioWeightDevices(mockResource)
415
+
416
+		assert.NilError(t, err, "getBlkioWeightDevices")
417
+		assert.Check(t, is.Len(weightDevs, 1), "getBlkioWeightDevices")
418
+		assert.Check(t, weightDevs[0].Major == MAJOR, "get major device type")
419
+		assert.Check(t, weightDevs[0].Minor == MINOR, "get minor device type")
420
+		assert.Check(t, *weightDevs[0].Weight == WEIGHT, "get device weight")
421
+	})
422
+}
423
+
424
+func TestGetBlkioThrottleDevices(t *testing.T) {
425
+	deviceTypeMock(t, func(tempFile string) {
426
+		mockDevs := []*blkiodev.ThrottleDevice{{Path: tempFile, Rate: WEIGHT}}
427
+
428
+		retDevs, err := getBlkioThrottleDevices(mockDevs)
429
+
430
+		assert.NilError(t, err, "getBlkioThrottleDevices")
431
+		assert.Check(t, is.Len(retDevs, 1), "getBlkioThrottleDevices")
432
+		assert.Check(t, retDevs[0].Major == MAJOR, "get major device type")
433
+		assert.Check(t, retDevs[0].Minor == MINOR, "get minor device type")
434
+		assert.Check(t, retDevs[0].Rate == WEIGHT, "get device rate")
435
+	})
436
+}