Skip to content

Commit e5e3dea

Browse files
authored
Merge pull request #600 from bduffany/cgroup-values
Support jailer cgroup args
2 parents f74f43b + 82d24f5 commit e5e3dea

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

jailer.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ type JailerConfig struct {
8585
// CgroupVersion is the version of the cgroup filesystem to use.
8686
CgroupVersion string
8787

88+
// CgroupArgs are cgroup settings applied by the jailer. Each arg must be
89+
// formatted like <cgroup_file>=<value>, like "cpu.shares=10"
90+
CgroupArgs []string
91+
8892
// Stdout specifies the IO writer for STDOUT to use when spawning the jailer.
8993
Stdout io.Writer
9094
// Stderr specifies the IO writer for STDERR to use when spawning the jailer.
@@ -109,6 +113,7 @@ type JailerCommandBuilder struct {
109113
daemonize bool
110114
firecrackerArgs []string
111115
cgroupVersion string
116+
cgroupArgs []string
112117

113118
stdin io.Reader
114119
stdout io.Writer
@@ -143,6 +148,10 @@ func (b JailerCommandBuilder) Args() []string {
143148
args = append(args, "--cgroup", fmt.Sprintf("cpuset.cpus=%s", cpulist))
144149
}
145150

151+
for _, cgroupArg := range b.cgroupArgs {
152+
args = append(args, "--cgroup", cgroupArg)
153+
}
154+
146155
if len(b.cgroupVersion) > 0 {
147156
args = append(args, "--cgroup-version", b.cgroupVersion)
148157
}
@@ -204,13 +213,30 @@ func (b JailerCommandBuilder) WithExecFile(path string) JailerCommandBuilder {
204213
return b
205214
}
206215

207-
// WithNumaNode uses the specfied node for the jailer. This represents the numa
216+
// WithNumaNode uses the specified node for the jailer. This represents the numa
208217
// node that the process will get assigned to.
218+
// Note: this is a convenience function that just sets the values of the cgroup
219+
// files "cpuset.mems" and "cpuset.cpus".
220+
// If those files are also configured using WithCgroupArgs, the values passed to
221+
// WithCgroupArgs will take precedence.
209222
func (b JailerCommandBuilder) WithNumaNode(node int) JailerCommandBuilder {
210223
b.node = node
211224
return b
212225
}
213226

227+
// WithCgroupArgs sets cgroup file values to be set by the jailer.
228+
// Each arg must be of the form <cgroup_file>=<value>.
229+
// Each call to this function resets the cgroup arguments, rather than
230+
// appending.
231+
//
232+
// Example:
233+
//
234+
// b = b.WithCgroupArgs("cpu.shares=10")
235+
func (b JailerCommandBuilder) WithCgroupArgs(cgroupArgs ...string) JailerCommandBuilder {
236+
b.cgroupArgs = cgroupArgs
237+
return b
238+
}
239+
214240
// WithChrootBaseDir will set the given path as the chroot base directory. This
215241
// specifies where chroot jails are built and defaults to /srv/jailer.
216242
func (b JailerCommandBuilder) WithChrootBaseDir(path string) JailerCommandBuilder {
@@ -348,6 +374,7 @@ func jail(ctx context.Context, m *Machine, cfg *Config) error {
348374
WithChrootBaseDir(cfg.JailerCfg.ChrootBaseDir).
349375
WithDaemonize(cfg.JailerCfg.Daemonize).
350376
WithCgroupVersion(cfg.JailerCfg.CgroupVersion).
377+
WithCgroupArgs(cfg.JailerCfg.CgroupArgs...).
351378
WithFirecrackerArgs(fcArgs...).
352379
WithStdout(stdout).
353380
WithStderr(stderr)

jailer_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func TestJailerBuilder(t *testing.T) {
103103
UID: Int(123),
104104
GID: Int(100),
105105
NumaNode: Int(0),
106+
CgroupArgs: []string{"cpu.shares=10"},
106107
ChrootStrategy: NewNaiveChrootStrategy("kernel-image-path"),
107108
ExecFile: "/path/to/firecracker",
108109
ChrootBaseDir: "/tmp",
@@ -123,6 +124,8 @@ func TestJailerBuilder(t *testing.T) {
123124
"cpuset.mems=0",
124125
"--cgroup",
125126
fmt.Sprintf("cpuset.cpus=%s", getNumaCpuset(0)),
127+
"--cgroup",
128+
"cpu.shares=10",
126129
"--cgroup-version",
127130
"2",
128131
"--chroot-base-dir",
@@ -146,6 +149,7 @@ func TestJailerBuilder(t *testing.T) {
146149
WithUID(IntValue(c.jailerCfg.UID)).
147150
WithGID(IntValue(c.jailerCfg.GID)).
148151
WithNumaNode(IntValue(c.jailerCfg.NumaNode)).
152+
WithCgroupArgs(c.jailerCfg.CgroupArgs...).
149153
WithCgroupVersion(c.jailerCfg.CgroupVersion).
150154
WithExecFile(c.jailerCfg.ExecFile)
151155

0 commit comments

Comments
 (0)