Skip to content

Commit a9c6787

Browse files
author
Ma Shimiao
committed
generate: support blkio related options
Signed-off-by: Ma Shimiao <[email protected]>
1 parent 0f24b50 commit a9c6787

File tree

5 files changed

+379
-4
lines changed

5 files changed

+379
-4
lines changed

cmd/oci-runtime-tool/generate.go

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ var generateFlags = []cli.Flag{
2626
cli.StringFlag{Name: "hostname", Usage: "hostname value for the container"},
2727
cli.StringSliceFlag{Name: "label", Usage: "add annotations to the configuration e.g. key=value"},
2828
cli.StringFlag{Name: "linux-apparmor", Usage: "specifies the the apparmor profile for the container"},
29+
cli.IntFlag{Name: "linux-blkio-leaf-weight", Usage: "Block IO (relative leaf weight), the range is from 10 to 1000"},
30+
cli.StringSliceFlag{Name: "linux-blkio-leaf-weight-device", Usage: "Block IO (relative device leaf weight), e.g. major:minor:leaf-weight"},
31+
cli.StringSliceFlag{Name: "linux-blkio-read-bps-device", Usage: "Limit read rate (bytes per second) from a device"},
32+
cli.StringSliceFlag{Name: "linux-blkio-read-iops-device", Usage: "Limit read rate (IO per second) from a device"},
33+
cli.IntFlag{Name: "linux-blkio-weight", Usage: "Block IO (relative weight), the range is from 10 to 1000"},
34+
cli.StringSliceFlag{Name: "linux-blkio-weight-device", Usage: "Block IO (relative device weight), e.g. major:minor:weight"},
35+
cli.StringSliceFlag{Name: "linux-blkio-write-bps-device", Usage: "Limit write rate (bytes per second) to a device"},
36+
cli.StringSliceFlag{Name: "linux-blkio-write-iops-device", Usage: "Limit write rate (IO per second) to a device"},
2937
cli.StringFlag{Name: "linux-cgroups-path", Usage: "specify the path to the cgroups"},
3038
cli.Uint64Flag{Name: "linux-cpu-period", Usage: "the CPU period to be used for hardcapping (in usecs)"},
3139
cli.Uint64Flag{Name: "linux-cpu-quota", Usage: "the allowed CPU time in a given period (in usecs)"},
32-
cli.Uint64Flag{Name: "linux-cpu-shares", Usage: "the relative share of CPU time available to the tasks in a cgroup"},
3340
cli.StringFlag{Name: "linux-cpus", Usage: "CPUs to use within the cpuset (default is to use any CPU available)"},
41+
cli.Uint64Flag{Name: "linux-cpu-shares", Usage: "the relative share of CPU time available to the tasks in a cgroup"},
3442
cli.StringSliceFlag{Name: "linux-device-add", Usage: "add a device which must be made available in the container"},
3543
cli.StringSliceFlag{Name: "linux-device-remove", Usage: "remove a device which must be made available in the container"},
3644
cli.BoolFlag{Name: "linux-device-remove-all", Usage: "remove all devices which must be made available in the container"},
@@ -388,6 +396,104 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
388396
g.SetProcessOOMScoreAdj(context.Int("linux-oom-score-adj"))
389397
}
390398

399+
if context.IsSet("linux-blkio-leaf-weight") {
400+
g.SetLinuxResourcesBlockIOLeafWeight(uint16(context.Uint64("linux-blkio-leaf-weight")))
401+
}
402+
403+
if context.IsSet("linux-blkio-leaf-weight-device") {
404+
devLeafWeight := context.StringSlice("linux-blkio-leaf-weight-device")
405+
for _, v := range devLeafWeight {
406+
major, minor, leafWeight, err := parseDeviceWeight(v)
407+
if err != nil {
408+
return err
409+
}
410+
if leafWeight == -1 {
411+
g.DropLinuxResourcesBlockIOLeafWeightDevice(major, minor)
412+
} else {
413+
g.AddLinuxResourcesBlockIOLeafWeightDevice(major, minor, uint16(leafWeight))
414+
}
415+
}
416+
}
417+
418+
if context.IsSet("linux-blkio-read-bps-device") {
419+
throttleDevices := context.StringSlice("linux-blkio-read-bps-device")
420+
for _, v := range throttleDevices {
421+
major, minor, rate, err := parseThrottleDevice(v)
422+
if err != nil {
423+
return err
424+
}
425+
if rate == -1 {
426+
g.DropLinuxResourcesBlockIOThrottleReadBpsDevice(major, minor)
427+
} else {
428+
g.AddLinuxResourcesBlockIOThrottleReadBpsDevice(major, minor, uint64(rate))
429+
}
430+
}
431+
}
432+
433+
if context.IsSet("linux-blkio-read-iops-device") {
434+
throttleDevices := context.StringSlice("linux-blkio-read-iops-device")
435+
for _, v := range throttleDevices {
436+
major, minor, rate, err := parseThrottleDevice(v)
437+
if err != nil {
438+
return err
439+
}
440+
if rate == -1 {
441+
g.DropLinuxResourcesBlockIOThrottleReadIOPSDevice(major, minor)
442+
} else {
443+
g.AddLinuxResourcesBlockIOThrottleReadIOPSDevice(major, minor, uint64(rate))
444+
}
445+
}
446+
}
447+
448+
if context.IsSet("linux-blkio-weight") {
449+
g.SetLinuxResourcesBlockIOWeight(uint16(context.Uint64("linux-blkio-weight")))
450+
}
451+
452+
if context.IsSet("linux-blkio-weight-device") {
453+
devWeight := context.StringSlice("linux-blkio-weight-device")
454+
for _, v := range devWeight {
455+
major, minor, weight, err := parseDeviceWeight(v)
456+
if err != nil {
457+
return err
458+
}
459+
if weight == -1 {
460+
g.DropLinuxResourcesBlockIOWeightDevice(major, minor)
461+
} else {
462+
g.AddLinuxResourcesBlockIOWeightDevice(major, minor, uint16(weight))
463+
}
464+
}
465+
}
466+
467+
if context.IsSet("linux-blkio-write-bps-device") {
468+
throttleDevices := context.StringSlice("linux-blkio-write-bps-device")
469+
for _, v := range throttleDevices {
470+
major, minor, rate, err := parseThrottleDevice(v)
471+
if err != nil {
472+
return err
473+
}
474+
if rate == -1 {
475+
g.DropLinuxResourcesBlockIOThrottleWriteBpsDevice(major, minor)
476+
} else {
477+
g.AddLinuxResourcesBlockIOThrottleWriteBpsDevice(major, minor, uint64(rate))
478+
}
479+
}
480+
}
481+
482+
if context.IsSet("linux-blkio-write-iops-device") {
483+
throttleDevices := context.StringSlice("linux-blkio-write-iops-device")
484+
for _, v := range throttleDevices {
485+
major, minor, rate, err := parseThrottleDevice(v)
486+
if err != nil {
487+
return err
488+
}
489+
if rate == -1 {
490+
g.DropLinuxResourcesBlockIOThrottleWriteIOPSDevice(major, minor)
491+
} else {
492+
g.AddLinuxResourcesBlockIOThrottleWriteIOPSDevice(major, minor, uint64(rate))
493+
}
494+
}
495+
}
496+
391497
if context.IsSet("linux-cpu-shares") {
392498
g.SetLinuxResourcesCPUShares(context.Uint64("linux-cpu-shares"))
393499
}
@@ -1003,3 +1109,51 @@ type ErrBadEnvVariable struct {
10031109
func (e ErrBadEnvVariable) Error() string {
10041110
return fmt.Sprintf("poorly formatted environment: %s", e.msg)
10051111
}
1112+
1113+
func parseDeviceWeight(weightDevice string) (int64, int64, int16, error) {
1114+
list := strings.Split(weightDevice, ":")
1115+
if len(list) != 3 {
1116+
return 0, 0, 0, fmt.Errorf("invalid format: %s", weightDevice)
1117+
}
1118+
1119+
major, err := strconv.Atoi(list[0])
1120+
if err != nil {
1121+
return 0, 0, 0, err
1122+
}
1123+
1124+
minor, err := strconv.Atoi(list[1])
1125+
if err != nil {
1126+
return 0, 0, 0, err
1127+
}
1128+
1129+
weight, err := strconv.Atoi(list[2])
1130+
if err != nil {
1131+
return 0, 0, 0, err
1132+
}
1133+
1134+
return int64(major), int64(minor), int16(weight), nil
1135+
}
1136+
1137+
func parseThrottleDevice(throttleDevice string) (int64, int64, int64, error) {
1138+
list := strings.Split(throttleDevice, ":")
1139+
if len(list) != 3 {
1140+
return 0, 0, 0, fmt.Errorf("invalid format: %s", throttleDevice)
1141+
}
1142+
1143+
major, err := strconv.Atoi(list[0])
1144+
if err != nil {
1145+
return 0, 0, 0, err
1146+
}
1147+
1148+
minor, err := strconv.Atoi(list[1])
1149+
if err != nil {
1150+
return 0, 0, 0, err
1151+
}
1152+
1153+
rate, err := strconv.Atoi(list[2])
1154+
if err != nil {
1155+
return 0, 0, 0, err
1156+
}
1157+
1158+
return int64(major), int64(minor), int64(rate), nil
1159+
}

completions/bash/oci-runtime-tool

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,14 @@ _oci-runtime-tool_generate() {
297297
--hostname
298298
--label
299299
--linux-apparmor
300+
--linux-blkio-leaf-weight
301+
--linux-blkio-leaf-weight-device
302+
--linux-blkio-read-bps-device
303+
--linux-blkio-read-iops-device
304+
--linux-blkio-weight
305+
--linux-blkio-weight-device
306+
--linux-blkio-write-bps-device
307+
--linux-blkio-write-iops-device
300308
--linux-cgroups-path
301309
--linux-cpu-period
302310
--linux-cpu-quota

generate/generate.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,144 @@ func (g *Generator) SetProcessOOMScoreAdj(adj int) {
490490
g.spec.Process.OOMScoreAdj = &adj
491491
}
492492

493+
// SetLinuxResourcesBlockIOLeafWeight sets g.spec.Linux.Resources.BlockIO.LeafWeight.
494+
func (g *Generator) SetLinuxResourcesBlockIOLeafWeight(weight uint16) {
495+
g.initSpecLinuxResourcesBlockIO()
496+
g.spec.Linux.Resources.BlockIO.LeafWeight = &weight
497+
}
498+
499+
// AddLinuxResourcesBlockIOLeafWeightDevice adds or sets g.spec.Linux.Resources.BlockIO.WeightDevice.LeafWeight.
500+
func (g *Generator) AddLinuxResourcesBlockIOLeafWeightDevice(major int64, minor int64, weight uint16) {
501+
g.initSpecLinuxResourcesBlockIO()
502+
for i, weightDevice := range g.spec.Linux.Resources.BlockIO.WeightDevice {
503+
if weightDevice.Major == major && weightDevice.Minor == minor {
504+
g.spec.Linux.Resources.BlockIO.WeightDevice[i].LeafWeight = &weight
505+
return
506+
}
507+
}
508+
weightDevice := new(rspec.LinuxWeightDevice)
509+
weightDevice.Major = major
510+
weightDevice.Minor = minor
511+
weightDevice.LeafWeight = &weight
512+
g.spec.Linux.Resources.BlockIO.WeightDevice = append(g.spec.Linux.Resources.BlockIO.WeightDevice, *weightDevice)
513+
}
514+
515+
// DropLinuxResourcesBlockIOLeafWeightDevice drops a item form g.spec.Linux.Resources.BlockIO.WeightDevice.LeafWeight
516+
func (g *Generator) DropLinuxResourcesBlockIOLeafWeightDevice(major int64, minor int64) {
517+
g.initSpecLinuxResourcesBlockIO()
518+
for i, weightDevice := range g.spec.Linux.Resources.BlockIO.WeightDevice {
519+
if weightDevice.Major == major && weightDevice.Minor == minor {
520+
if weightDevice.Weight != nil {
521+
newWeightDevice := new(rspec.LinuxWeightDevice)
522+
newWeightDevice.Major = major
523+
newWeightDevice.Minor = minor
524+
newWeightDevice.Weight = weightDevice.Weight
525+
g.spec.Linux.Resources.BlockIO.WeightDevice[i] = *newWeightDevice
526+
} else {
527+
g.spec.Linux.Resources.BlockIO.WeightDevice = append(g.spec.Linux.Resources.BlockIO.WeightDevice[:i], g.spec.Linux.Resources.BlockIO.WeightDevice[i+1:]...)
528+
}
529+
return
530+
}
531+
}
532+
}
533+
534+
// SetLinuxResourcesBlockIOWeight sets g.spec.Linux.Resources.BlockIO.Weight.
535+
func (g *Generator) SetLinuxResourcesBlockIOWeight(weight uint16) {
536+
g.initSpecLinuxResourcesBlockIO()
537+
g.spec.Linux.Resources.BlockIO.Weight = &weight
538+
}
539+
540+
// AddLinuxResourcesBlockIOWeightDevice adds or sets g.spec.Linux.Resources.BlockIO.WeightDevice.Weight.
541+
func (g *Generator) AddLinuxResourcesBlockIOWeightDevice(major int64, minor int64, weight uint16) {
542+
g.initSpecLinuxResourcesBlockIO()
543+
for i, weightDevice := range g.spec.Linux.Resources.BlockIO.WeightDevice {
544+
if weightDevice.Major == major && weightDevice.Minor == minor {
545+
g.spec.Linux.Resources.BlockIO.WeightDevice[i].Weight = &weight
546+
return
547+
}
548+
}
549+
weightDevice := new(rspec.LinuxWeightDevice)
550+
weightDevice.Major = major
551+
weightDevice.Minor = minor
552+
weightDevice.Weight = &weight
553+
g.spec.Linux.Resources.BlockIO.WeightDevice = append(g.spec.Linux.Resources.BlockIO.WeightDevice, *weightDevice)
554+
}
555+
556+
// DropLinuxResourcesBlockIOWeightDevice drops a item form g.spec.Linux.Resources.BlockIO.WeightDevice.Weight
557+
func (g *Generator) DropLinuxResourcesBlockIOWeightDevice(major int64, minor int64) {
558+
g.initSpecLinuxResourcesBlockIO()
559+
for i, weightDevice := range g.spec.Linux.Resources.BlockIO.WeightDevice {
560+
if weightDevice.Major == major && weightDevice.Minor == minor {
561+
if weightDevice.LeafWeight != nil {
562+
newWeightDevice := new(rspec.LinuxWeightDevice)
563+
newWeightDevice.Major = major
564+
newWeightDevice.Minor = minor
565+
newWeightDevice.LeafWeight = weightDevice.LeafWeight
566+
g.spec.Linux.Resources.BlockIO.WeightDevice[i] = *newWeightDevice
567+
} else {
568+
g.spec.Linux.Resources.BlockIO.WeightDevice = append(g.spec.Linux.Resources.BlockIO.WeightDevice[:i], g.spec.Linux.Resources.BlockIO.WeightDevice[i+1:]...)
569+
}
570+
return
571+
}
572+
}
573+
}
574+
575+
// AddLinuxResourcesBlockIOThrottleReadBpsDevice adds or sets g.spec.Linux.Resources.BlockIO.ThrottleReadBpsDevice.
576+
func (g *Generator) AddLinuxResourcesBlockIOThrottleReadBpsDevice(major int64, minor int64, rate uint64) {
577+
g.initSpecLinuxResourcesBlockIO()
578+
throttleDevices := addOrReplaceBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleReadBpsDevice, major, minor, rate)
579+
g.spec.Linux.Resources.BlockIO.ThrottleReadBpsDevice = throttleDevices
580+
}
581+
582+
// DropLinuxResourcesBlockIOThrottleReadBpsDevice drops a item from g.spec.Linux.Resources.BlockIO.ThrottleReadBpsDevice.
583+
func (g *Generator) DropLinuxResourcesBlockIOThrottleReadBpsDevice(major int64, minor int64) {
584+
g.initSpecLinuxResourcesBlockIO()
585+
throttleDevices := dropBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleReadBpsDevice, major, minor)
586+
g.spec.Linux.Resources.BlockIO.ThrottleReadBpsDevice = throttleDevices
587+
}
588+
589+
// AddLinuxResourcesBlockIOThrottleReadIOPSDevice adds or sets g.spec.Linux.Resources.BlockIO.ThrottleReadIOPSDevice.
590+
func (g *Generator) AddLinuxResourcesBlockIOThrottleReadIOPSDevice(major int64, minor int64, rate uint64) {
591+
g.initSpecLinuxResourcesBlockIO()
592+
throttleDevices := addOrReplaceBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleReadIOPSDevice, major, minor, rate)
593+
g.spec.Linux.Resources.BlockIO.ThrottleReadIOPSDevice = throttleDevices
594+
}
595+
596+
// DropLinuxResourcesBlockIOThrottleReadIOPSDevice drops a item from g.spec.Linux.Resources.BlockIO.ThrottleReadIOPSDevice.
597+
func (g *Generator) DropLinuxResourcesBlockIOThrottleReadIOPSDevice(major int64, minor int64) {
598+
g.initSpecLinuxResourcesBlockIO()
599+
throttleDevices := dropBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleReadIOPSDevice, major, minor)
600+
g.spec.Linux.Resources.BlockIO.ThrottleReadIOPSDevice = throttleDevices
601+
}
602+
603+
// AddLinuxResourcesBlockIOThrottleWriteBpsDevice adds or sets g.spec.Linux.Resources.BlockIO.ThrottleWriteBpsDevice.
604+
func (g *Generator) AddLinuxResourcesBlockIOThrottleWriteBpsDevice(major int64, minor int64, rate uint64) {
605+
g.initSpecLinuxResourcesBlockIO()
606+
throttleDevices := addOrReplaceBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleWriteBpsDevice, major, minor, rate)
607+
g.spec.Linux.Resources.BlockIO.ThrottleWriteBpsDevice = throttleDevices
608+
}
609+
610+
// DropLinuxResourcesBlockIOThrottleWriteBpsDevice drops a item from g.spec.Linux.Resources.BlockIO.ThrottleWriteBpsDevice.
611+
func (g *Generator) DropLinuxResourcesBlockIOThrottleWriteBpsDevice(major int64, minor int64) {
612+
g.initSpecLinuxResourcesBlockIO()
613+
throttleDevices := dropBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleWriteBpsDevice, major, minor)
614+
g.spec.Linux.Resources.BlockIO.ThrottleWriteBpsDevice = throttleDevices
615+
}
616+
617+
// AddLinuxResourcesBlockIOThrottleWriteIOPSDevice adds or sets g.spec.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice.
618+
func (g *Generator) AddLinuxResourcesBlockIOThrottleWriteIOPSDevice(major int64, minor int64, rate uint64) {
619+
g.initSpecLinuxResourcesBlockIO()
620+
throttleDevices := addOrReplaceBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice, major, minor, rate)
621+
g.spec.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice = throttleDevices
622+
}
623+
624+
// DropLinuxResourcesBlockIOThrottleWriteIOPSDevice drops a item from g.spec.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice.
625+
func (g *Generator) DropLinuxResourcesBlockIOThrottleWriteIOPSDevice(major int64, minor int64) {
626+
g.initSpecLinuxResourcesBlockIO()
627+
throttleDevices := dropBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice, major, minor)
628+
g.spec.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice = throttleDevices
629+
}
630+
493631
// SetLinuxResourcesCPUShares sets g.spec.Linux.Resources.CPU.Shares.
494632
func (g *Generator) SetLinuxResourcesCPUShares(shares uint64) {
495633
g.initSpecLinuxResourcesCPU()
@@ -1124,3 +1262,32 @@ func (g *Generator) AddLinuxReadonlyPaths(path string) {
11241262
g.initSpecLinux()
11251263
g.spec.Linux.ReadonlyPaths = append(g.spec.Linux.ReadonlyPaths, path)
11261264
}
1265+
1266+
func addOrReplaceBlockIOThrottleDevice(tmpList []rspec.LinuxThrottleDevice, major int64, minor int64, rate uint64) []rspec.LinuxThrottleDevice {
1267+
throttleDevices := tmpList
1268+
for i, throttleDevice := range throttleDevices {
1269+
if throttleDevice.Major == major && throttleDevice.Minor == minor {
1270+
throttleDevices[i].Rate = rate
1271+
return throttleDevices
1272+
}
1273+
}
1274+
throttleDevice := new(rspec.LinuxThrottleDevice)
1275+
throttleDevice.Major = major
1276+
throttleDevice.Minor = minor
1277+
throttleDevice.Rate = rate
1278+
throttleDevices = append(throttleDevices, *throttleDevice)
1279+
1280+
return throttleDevices
1281+
}
1282+
1283+
func dropBlockIOThrottleDevice(tmpList []rspec.LinuxThrottleDevice, major int64, minor int64) []rspec.LinuxThrottleDevice {
1284+
throttleDevices := tmpList
1285+
for i, throttleDevice := range throttleDevices {
1286+
if throttleDevice.Major == major && throttleDevice.Minor == minor {
1287+
throttleDevices = append(throttleDevices[:i], throttleDevices[i+1:]...)
1288+
return throttleDevices
1289+
}
1290+
}
1291+
1292+
return throttleDevices
1293+
}

generate/spec.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ func (g *Generator) initSpecLinuxResources() {
7373
}
7474
}
7575

76+
func (g *Generator) initSpecLinuxResourcesBlockIO() {
77+
g.initSpecLinuxResources()
78+
if g.spec.Linux.Resources.BlockIO == nil {
79+
g.spec.Linux.Resources.BlockIO = &rspec.LinuxBlockIO{}
80+
}
81+
}
82+
7683
func (g *Generator) initSpecLinuxResourcesCPU() {
7784
g.initSpecLinuxResources()
7885
if g.spec.Linux.Resources.CPU == nil {

0 commit comments

Comments
 (0)