Skip to content

Commit

Permalink
Add racct limit option
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilzhangfreebsd committed Jun 2, 2021
1 parent 9140284 commit 0de145d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cmd/runj/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ the console's pseudoterminal`)
if err := jail.CreateJail(cmd.Context(), confPath); err != nil {
return err
}
err = jail.Limit(id, ociConfig)
if err != nil {
return err
}
defer func() {
if err == nil {
return
}
jail.Unlimit(id, ociConfig)
}()
err = jail.Mount(ociConfig)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions cmd/runj/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func deleteCommand() *cobra.Command {
if ociConfig == nil {
return errors.New("OCI config is required")
}
err = jail.Unlimit(id, ociConfig)
if err != nil {
return err
}
err = jail.Unmount(ociConfig)
if err != nil {
return err
Expand Down
55 changes: 55 additions & 0 deletions jail/limit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package jail

import (
"bytes"
"os/exec"

"go.sbk.wtf/runj/runtimespec"
)

func Limit(id string, ociConfig *runtimespec.Spec) error {
if ociConfig.FreeBSD == nil {
return nil
}
for _, racctLimit := range ociConfig.FreeBSD.RacctLimits {
rule := makeRCTLRule(id, &racctLimit)
cmd := exec.Command("rctl", "-a", rule)
err := cmd.Run()
if (err != nil) {
return err
}
}
return nil
}

func Unlimit(id string, ociConfig *runtimespec.Spec) error {
if ociConfig.FreeBSD == nil {
return nil
}
for _, racctLimit := range ociConfig.FreeBSD.RacctLimits {
rule := makeRCTLRule(id, &racctLimit)
cmd := exec.Command("rctl", "-r", rule)
err := cmd.Run()
if (err != nil) {
return err
}
}
return nil
}

func makeRCTLRule(id string, racctLimit *runtimespec.RacctLimit) string {
buf := bytes.Buffer{}
buf.WriteString("jail:")
buf.WriteString(id)
buf.WriteString(":")
buf.WriteString(racctLimit.Resource)
buf.WriteString(":")
buf.WriteString(racctLimit.Action)
buf.WriteString("=")
buf.WriteString(racctLimit.Amount)
if racctLimit.Per != "" {
buf.WriteString("/")
buf.WriteString(racctLimit.Per)
}
return buf.String()
}
25 changes: 25 additions & 0 deletions runtimespec/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ type Spec struct {
VM *VM `json:"vm,omitempty" platform:"vm"`
*/
// End of modification

// Modification by Cyril Zhang
// FreeBSD is platform-specific configuration for FreeBSD based containers.
FreeBSD *FreeBSD `json:"freebsd,omitempty" platform:"freebsd"`
// End of modification
}

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

// Modification by Cyril Zhang
// FreeBSD contains platform-specific configuration for FreeBSD based containers.
type FreeBSD struct {
// RacctLimits specifies racct rules to apply to this jail.
RacctLimits []RacctLimit `json:"racct,omitempty"`
}

// RacctLimit is a racct rule to apply to a jail.
type RacctLimit struct {
// Resource is the resource to set a limit on.
Resource string `json:"resource"`
// Action is what will happen if a process exceeds the allowed amount.
Action string `json:"action"`
// Amount is the allowed amount of the resource.
Amount string `json:"amount"`
// Per defines the entity that the amount applies to.
Per string `json:"per,omitempty"`
}
// End of modification

// Modification by Samuel Karp
/*
Omitted type definitions for:
Expand Down

0 comments on commit 0de145d

Please sign in to comment.