Skip to content

Commit cd08a85

Browse files
Add racct limit option
1 parent 9140284 commit cd08a85

File tree

4 files changed

+96
-0
lines changed

4 files changed

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

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)