Skip to content

Commit 0de145d

Browse files
Add racct limit option
1 parent 9140284 commit 0de145d

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

cmd/runj/create.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ the console's pseudoterminal`)
137137
if err := jail.CreateJail(cmd.Context(), confPath); err != nil {
138138
return err
139139
}
140+
err = jail.Limit(id, ociConfig)
141+
if err != nil {
142+
return err
143+
}
144+
defer func() {
145+
if err == nil {
146+
return
147+
}
148+
jail.Unlimit(id, ociConfig)
149+
}()
140150
err = jail.Mount(ociConfig)
141151
if err != nil {
142152
return err

cmd/runj/delete.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func deleteCommand() *cobra.Command {
5959
if ociConfig == nil {
6060
return errors.New("OCI config is required")
6161
}
62+
err = jail.Unlimit(id, ociConfig)
63+
if err != nil {
64+
return err
65+
}
6266
err = jail.Unmount(ociConfig)
6367
if err != nil {
6468
return err

jail/limit.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package jail
2+
3+
import (
4+
"bytes"
5+
"os/exec"
6+
7+
"go.sbk.wtf/runj/runtimespec"
8+
)
9+
10+
func Limit(id string, ociConfig *runtimespec.Spec) error {
11+
if ociConfig.FreeBSD == nil {
12+
return nil
13+
}
14+
for _, racctLimit := range ociConfig.FreeBSD.RacctLimits {
15+
rule := makeRCTLRule(id, &racctLimit)
16+
cmd := exec.Command("rctl", "-a", rule)
17+
err := cmd.Run()
18+
if (err != nil) {
19+
return err
20+
}
21+
}
22+
return nil
23+
}
24+
25+
func Unlimit(id string, ociConfig *runtimespec.Spec) error {
26+
if ociConfig.FreeBSD == nil {
27+
return nil
28+
}
29+
for _, racctLimit := range ociConfig.FreeBSD.RacctLimits {
30+
rule := makeRCTLRule(id, &racctLimit)
31+
cmd := exec.Command("rctl", "-r", rule)
32+
err := cmd.Run()
33+
if (err != nil) {
34+
return err
35+
}
36+
}
37+
return nil
38+
}
39+
40+
func makeRCTLRule(id string, racctLimit *runtimespec.RacctLimit) string {
41+
buf := bytes.Buffer{}
42+
buf.WriteString("jail:")
43+
buf.WriteString(id)
44+
buf.WriteString(":")
45+
buf.WriteString(racctLimit.Resource)
46+
buf.WriteString(":")
47+
buf.WriteString(racctLimit.Action)
48+
buf.WriteString("=")
49+
buf.WriteString(racctLimit.Amount)
50+
if racctLimit.Per != "" {
51+
buf.WriteString("/")
52+
buf.WriteString(racctLimit.Per)
53+
}
54+
return buf.String()
55+
}

runtimespec/config.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ type Spec struct {
5353
VM *VM `json:"vm,omitempty" platform:"vm"`
5454
*/
5555
// End of modification
56+
57+
// Modification by Cyril Zhang
58+
// FreeBSD is platform-specific configuration for FreeBSD based containers.
59+
FreeBSD *FreeBSD `json:"freebsd,omitempty" platform:"freebsd"`
60+
// End of modification
5661
}
5762

5863
// Modification by Samuel Karp
@@ -135,6 +140,26 @@ type Mount struct {
135140
Options []string `json:"options,omitempty"`
136141
}
137142

143+
// Modification by Cyril Zhang
144+
// FreeBSD contains platform-specific configuration for FreeBSD based containers.
145+
type FreeBSD struct {
146+
// RacctLimits specifies racct rules to apply to this jail.
147+
RacctLimits []RacctLimit `json:"racct,omitempty"`
148+
}
149+
150+
// RacctLimit is a racct rule to apply to a jail.
151+
type RacctLimit struct {
152+
// Resource is the resource to set a limit on.
153+
Resource string `json:"resource"`
154+
// Action is what will happen if a process exceeds the allowed amount.
155+
Action string `json:"action"`
156+
// Amount is the allowed amount of the resource.
157+
Amount string `json:"amount"`
158+
// Per defines the entity that the amount applies to.
159+
Per string `json:"per,omitempty"`
160+
}
161+
// End of modification
162+
138163
// Modification by Samuel Karp
139164
/*
140165
Omitted type definitions for:

0 commit comments

Comments
 (0)