gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/runsc/cmd/write_control.go (about) 1 // Copyright 2022 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cmd 16 17 import ( 18 "context" 19 "fmt" 20 21 "github.com/google/subcommands" 22 "gvisor.dev/gvisor/pkg/sentry/control" 23 "gvisor.dev/gvisor/runsc/cmd/util" 24 "gvisor.dev/gvisor/runsc/config" 25 "gvisor.dev/gvisor/runsc/container" 26 "gvisor.dev/gvisor/runsc/flag" 27 ) 28 29 // WriteControl implements subcommands.Command for the "write-control" command. 30 type WriteControl struct{} 31 32 // Name implements subcommands.Command.Name. 33 func (*WriteControl) Name() string { 34 return "write-control" 35 } 36 37 // Synopsis implements subcommands.Command.Synopsis. 38 func (*WriteControl) Synopsis() string { 39 return "write a cgroups control value inside the container" 40 } 41 42 // Usage implements subcommands.Command.Usage. 43 func (*WriteControl) Usage() string { 44 return `write-control <container-id> <controller> <cgroup-path> <control-value-name> <data-to-write> 45 46 Where "<container-id>" is the name for the instance of the container, 47 "<controller>" is the name of an active cgroupv1 controller, <cgroup-path> is 48 the path to the cgroup to write and <control-value-name> is the name of the 49 control file to write. 50 51 EXAMPLE: 52 # runsc write-control <container-id> memory / memory.limit_in_bytes 536870912 53 ` 54 } 55 56 // SetFlags implements subcommands.Command.SetFlags. 57 func (r *WriteControl) SetFlags(f *flag.FlagSet) {} 58 59 // Execute implements subcommands.Command.Execute. 60 func (r *WriteControl) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus { 61 if f.NArg() < 5 { 62 f.Usage() 63 return subcommands.ExitUsageError 64 } 65 66 id := f.Arg(0) 67 conf := args[0].(*config.Config) 68 69 c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{SkipCheck: true}) 70 if err != nil { 71 util.Fatalf("loading sandbox: %v", err) 72 } 73 74 err = c.Sandbox.CgroupsWriteControlFile(control.CgroupControlFile{ 75 Controller: f.Arg(1), 76 Path: f.Arg(2), 77 Name: f.Arg(3), 78 }, f.Arg(4)) 79 if err != nil { 80 fmt.Printf("ERROR: %s\n", err) 81 return subcommands.ExitFailure 82 } 83 return subcommands.ExitSuccess 84 }