sysctl

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sysctl is the sysctl behavior: kernel parameters osapi writes to /etc/sysctl.d and applies.

The files follow the layout every behavior has (see docs/docs/sidebar/ development/behaviors.md): behavior.go is the definition, types.go the input and output types, methods.go the methods, provider.go the interface each platform implements, and gen/ what osapi generates from them.

Index

Constants

View Source
const DefaultDir = "/etc/sysctl.d"

DefaultDir is where osapi writes its kernel parameter files unless behaviors.sysctl.dir says otherwise.

View Source
const ProcDir = "/proc/sys"

ProcDir is where the kernel shows its parameters, one file each, the dots of a parameter's name being the directories of its path.

Variables

View Source
var Behavior = behavior.Register(behavior.Define(
	"sysctl", "Kernel parameters, written to sysctl.d files and applied",
	behavior.Node,
	behavior.Config(defaults),
	behavior.Requires(managedfile.Behavior),
	behavior.Platform(behavior.PlatformDebian, newDebian),
	behavior.Sudo("sysctl"),

	behavior.Writes(DefaultDir+"/osapi-*.conf"),
	behavior.API,
	behavior.Area(area.Config),
	behavior.CLI,
	behavior.Validator("sysctl_key", keyRE.MatchString,
		func(t rule.Target) { t.Pattern(keyPattern) },
		"is not a valid sysctl key: it starts with a letter or a digit, "+
			"and has only letters, digits, dots, underscores and hyphens"),
	behavior.Read("list", List).
		Describe("List the kernel's parameters and which osapi manages").
		Explain("Lists every parameter the kernel shows under /proc/sys, with its current "+
			"value, as sysctl -a does, and marks the ones osapi manages. A prefix, such as "+
			"net.ipv4, limits the list to that part of the tree. The kernel can lack a "+
			"parameter that osapi manages, for example when its module is not loaded. "+
			"Then the list shows the value that osapi wrote.").
		Example("Every parameter, and which osapi manages", ListInput{}, []Entry{
			{Key: "kernel.panic", Value: "0"},
			{Key: "net.ipv4.ip_forward", Value: "1", Managed: true},
			{Key: "net.ipv4.ping_group_range", Value: "0 2147483647"},
		}, kernel()...).
		Example("One part of the tree", ListInput{Prefix: "net.ipv4"}, []Entry{
			{Key: "net.ipv4.ip_forward", Value: "1", Managed: true},
			{Key: "net.ipv4.ping_group_range", Value: "0 2147483647"},
		}, kernel()...).
		Example("A managed parameter the kernel does not have right now",
			ListInput{Prefix: "net.bridge"}, []Entry{
				{Key: "net.bridge.bridge-nf-call-iptables", Value: "1", Managed: true},
			},
			behavior.After(CreateInput{Key: "net.bridge.bridge-nf-call-iptables", Value: "1"}),
		).
		Fails("A prefix that is not a parameter name", ListInput{Prefix: "../etc"},
			"invalid sysctl key"),
	behavior.Read("get", Get).Key("key").
		Describe("Get a kernel parameter's current value, and whether osapi manages it").
		Example("A parameter osapi manages", GetInput{Key: "vm.swappiness"},
			Entry{Key: "vm.swappiness", Value: "10", Managed: true},
			behavior.After(CreateInput{Key: "vm.swappiness", Value: "10"}),
			behavior.Exists(proc("vm.swappiness"), "10\n"),
		).
		Example("A parameter osapi does not manage", GetInput{Key: "vm.swappiness"},
			Entry{Key: "vm.swappiness", Value: "60"},
			behavior.Exists(proc("vm.swappiness"), "60\n"),
		).
		Example("A value of several fields", GetInput{Key: "net.ipv4.ping_group_range"},
			Entry{Key: "net.ipv4.ping_group_range", Value: "0 2147483647"},
			behavior.Exists(proc("net.ipv4.ping_group_range"), "0\t2147483647\n"),
		).
		Example("A managed parameter the kernel does not have right now",
			GetInput{Key: "net.bridge.bridge-nf-call-iptables"},
			Entry{Key: "net.bridge.bridge-nf-call-iptables", Value: "1", Managed: true},
			behavior.After(CreateInput{Key: "net.bridge.bridge-nf-call-iptables", Value: "1"}),
			behavior.NotLive("a real host without the module refuses the create (osapi-io/osapi#1566)"),
		).
		Fails("A parameter the kernel does not have", GetInput{Key: "vm.nosuch"}, "not found").
		Fails("No key", GetInput{}, "key must not be empty").
		Fails("A key that is not a parameter name", GetInput{Key: "vm.swappiness;reboot"},
			"invalid sysctl key").
		Fails("A key holding a value", GetInput{Key: "vm.swappiness=1"}, "invalid sysctl key"),
	behavior.Write("create", Create).
		Describe("Manage a kernel parameter and apply it").
		Explain("Writes osapi-net.ipv4.ip_forward.conf for net.ipv4.ip_forward to behaviors.sysctl.dir, /etc/sysctl.d by default, "+
			"and applies it with sysctl -p. "+
			"It fails when sysctl -p fails, because the value is not set. "+
			"A second run with the same value changes nothing and reports changed: false. "+
			"If the kernel has a different value, for example after a failed apply, "+
			"it applies the value again.").
		Example("Turn on IP forwarding",
			CreateInput{Key: "net.ipv4.ip_forward", Value: "1"},
			MutationResult{Key: "net.ipv4.ip_forward", Changed: true},
			behavior.Wrote(conf("net.ipv4.ip_forward"), "net.ipv4.ip_forward = 1\n"),
			behavior.Ran("sysctl", "-p", conf("net.ipv4.ip_forward")),
			behavior.Reads(GetInput{Key: "net.ipv4.ip_forward"}, Entry{Key: "net.ipv4.ip_forward", Value: "1", Managed: true}),
		).
		Fails("No key", CreateInput{Value: "1"}, "key must not be empty").
		Fails("A key that is not a parameter name",
			CreateInput{Key: "../etc/passwd", Value: "1"}, "invalid sysctl key").
		Fails("A key that would read as a flag",
			CreateInput{Key: "-w", Value: "1"}, "invalid sysctl key").
		Fails("A key that would be a hidden file",
			CreateInput{Key: ".hidden", Value: "1"}, "invalid sysctl key").
		Fails("No value", CreateInput{Key: "vm.swappiness"}, "value must not be empty").
		Fails("A value on two lines",
			CreateInput{Key: "vm.swappiness", Value: "10\nkernel.panic = 0"}, "line breaks"),
	behavior.Write("update", Update).Key("key").
		Describe("Set a kernel parameter, manage it, and apply it").
		Explain("The same as create, addressed by the parameter: osapi manages the parameter "+
			"from then on whether it did before or not. Setting the value it already has "+
			"reports changed: false.").
		Example("Lower swappiness",
			UpdateInput{Key: "vm.swappiness", Value: "10"},
			MutationResult{Key: "vm.swappiness", Changed: true},
			behavior.After(CreateInput{Key: "vm.swappiness", Value: "60"}),
			behavior.Wrote(conf("vm.swappiness"), "vm.swappiness = 10\n"),
			behavior.Ran("sysctl", "-p", conf("vm.swappiness")),
			behavior.Reads(GetInput{Key: "vm.swappiness"}, Entry{Key: "vm.swappiness", Value: "10", Managed: true}),
		).
		Example("A parameter osapi did not manage",
			UpdateInput{Key: "vm.swappiness", Value: "10"},
			MutationResult{Key: "vm.swappiness", Changed: true},
			behavior.Wrote(conf("vm.swappiness"), "vm.swappiness = 10\n"),
			behavior.Ran("sysctl", "-p", conf("vm.swappiness")),
			behavior.Reads(GetInput{Key: "vm.swappiness"}, Entry{Key: "vm.swappiness", Value: "10", Managed: true}),
		).
		Fails("No key", UpdateInput{Value: "10"}, "key must not be empty").
		Fails("A key that is not a parameter name",
			UpdateInput{Key: "vm.swappiness;reboot", Value: "1"}, "invalid sysctl key").
		Fails("A key holding a value",
			UpdateInput{Key: "vm.swappiness=1", Value: "1"}, "invalid sysctl key").
		Fails("No value", UpdateInput{Key: "vm.swappiness"}, "value must not be empty").
		Fails("A value on two lines",
			UpdateInput{Key: "vm.swappiness", Value: "10\nkernel.panic = 0"}, "line breaks"),
	behavior.Write("delete", Delete).Key("key").
		Describe("Stop the management of a kernel parameter").
		Explain("Removes the file that osapi wrote for it and reloads with sysctl --system. "+
			"Then a setting for the key in a different file takes effect. If there is no "+
			"such setting, the parameter keeps its current value until the next reboot. "+
			"A delete of a parameter that osapi does not manage reports changed: false.").
		Example("Remove a parameter", DeleteInput{Key: "vm.swappiness"},
			MutationResult{Key: "vm.swappiness", Changed: true},
			behavior.After(CreateInput{Key: "vm.swappiness", Value: "10"}),
			behavior.Removed(conf("vm.swappiness")),
			behavior.Ran("sysctl", "--system"),
			behavior.Reads(GetInput{Key: "vm.swappiness"}, Entry{Key: "vm.swappiness"}, "key", "managed"),
		).
		Example("A parameter osapi does not manage", DeleteInput{Key: "vm.swappiness"},
			MutationResult{Key: "vm.swappiness"},
			behavior.Reads(GetInput{Key: "vm.swappiness"}, Entry{Key: "vm.swappiness"}, "key", "managed"),
		).
		Example("A parameter osapi already stopped managing", DeleteInput{Key: "vm.swappiness"},
			MutationResult{Key: "vm.swappiness"},
			behavior.After(CreateInput{Key: "vm.swappiness", Value: "10"}),
			behavior.After(DeleteInput{Key: "vm.swappiness"}),
			behavior.Reads(GetInput{Key: "vm.swappiness"}, Entry{Key: "vm.swappiness"}, "key", "managed"),
		).
		Fails("No key", DeleteInput{}, "key must not be empty").
		Fails("A key that is not a parameter name", DeleteInput{Key: "vm.swappiness;reboot"},
			"invalid sysctl key").
		Fails("A key holding a value", DeleteInput{Key: "vm.swappiness=1"}, "invalid sysctl key"),
))

Behavior is the sysctl definition. Everything else, the routes, the agent's dispatch, the CLI commands, the spec and the docs, is built from it.

Functions

This section is empty.

Types

type CreateInput

type CreateInput struct {
	Key   string `json:"key"   validate:"required,min=1,sysctl_key" doc:"Parameter name, such as net.ipv4.ip_forward" telemetry:"attr"`
	Value string `json:"value" validate:"required,no_linebreak"     doc:"Value to set"`
}

CreateInput is a parameter and the value to set.

type CreateResult

type CreateResult struct {
	Key     string `json:"key"`
	Changed bool   `json:"changed"`
	Error   string `json:"error,omitempty"`
}

CreateResult represents the outcome of a sysctl create operation.

type Debian

type Debian struct {
	// contains filtered or unexported fields
}

Debian implements the Provider interface for Debian-family systems. It writes sysctl conf files directly to its directory, /etc/sysctl.d/ by default, and tracks state in the file-state KV for idempotency. Unlike the cron meta-provider, sysctl generates trivial content inline rather than fetching from the NATS Object Store.

func NewDebianProvider

func NewDebianProvider(
	logger *slog.Logger,
	fs avfs.VFS,
	stateKV provider.FileState,
	stateBucket string,
	execManager host.Exec,
	hostname string,
) *Debian

NewDebianProvider factory to create a new Debian instance.

func (*Debian) Create

func (d *Debian) Create(
	ctx context.Context,
	entry Entry,
) (*CreateResult, error)

Create deploys a new sysctl conf file and applies it. Idempotent on content rather than on existence: a drop-in already holding what was asked for reports Changed: false, and one that has been edited, deleted, or holds a different value is rewritten, because the operation's job is to make the setting true.

func (*Debian) Delete

func (d *Debian) Delete(
	ctx context.Context,
	key string,
) (*DeleteResult, error)

Delete removes a managed sysctl conf file and reloads defaults.

func (*Debian) Get

func (d *Debian) Get(
	ctx context.Context,
	key string,
) (*Entry, error)

Get returns one kernel parameter's current value, and whether osapi manages it. It is not found only when the kernel has no such parameter and osapi does not manage it.

func (*Debian) List

func (d *Debian) List(
	ctx context.Context,
	prefix string,
) ([]Entry, error)

List returns every kernel parameter the host has, with its current value from ProcDir, marking the ones osapi manages. A prefix, such as net.ipv4, narrows it to that parameter and the ones under it. A managed parameter the kernel does not have right now, such as one whose module is not loaded, is listed with the value osapi wrote. A parameter whose file cannot be read, such as one only root may read, is left out, as sysctl -a leaves it out.

func (*Debian) Update

func (d *Debian) Update(
	ctx context.Context,
	entry Entry,
) (*UpdateResult, error)

Update sets a parameter and manages it, whether or not osapi managed it before, as Create does. Idempotent: if the content hasn't changed, Changed is false.

func (*Debian) WithDir

func (d *Debian) WithDir(
	dir string,
) *Debian

WithDir sets the directory the conf files are written to, from behaviors.sysctl.dir. An empty dir keeps DefaultDir.

func (*Debian) WithFiles

func (d *Debian) WithFiles(
	files managedfile.Provider,
) *Debian

WithFiles sets the managed file provider the conf files are written through. The sysctl behavior passes the one the agent built for the managedfile behavior it requires.

type DeleteInput

type DeleteInput struct {
	Key string `json:"key" validate:"required,min=1,sysctl_key" doc:"Parameter name, such as net.ipv4.ip_forward"`
}

DeleteInput names the parameter to stop managing.

type DeleteResult

type DeleteResult struct {
	Key     string `json:"key"`
	Changed bool   `json:"changed"`
	Error   string `json:"error,omitempty"`
}

DeleteResult represents the outcome of a sysctl delete operation.

type Entry

type Entry struct {
	Key     string `json:"key"     doc:"Parameter name"`
	Value   string `json:"value"   doc:"Current runtime value"`
	Managed bool   `json:"managed" doc:"Whether osapi manages it: a file of its own sets it at boot"`
}

Entry is a parameter, its current runtime value, and whether osapi manages it.

func Get

func Get(
	ctx context.Context,
	p Provider,
	in GetInput,
) (Entry, error)

Get returns one parameter.

func List

func List(
	ctx context.Context,
	p Provider,
	in ListInput,
) ([]Entry, error)

List returns every parameter, or those under in.Prefix.

type GetInput

type GetInput struct {
	Key string `json:"key" validate:"required,min=1,sysctl_key" doc:"Parameter name, such as net.ipv4.ip_forward"`
}

GetInput names one parameter.

type ListInput

type ListInput struct {
	Prefix string `json:"prefix" validate:"omitempty,sysctl_key" doc:"Only this parameter and the ones under it, such as net.ipv4"`
}

ListInput optionally narrows the list to one part of the tree.

type MutationResult

type MutationResult struct {
	Key     string `json:"key"     doc:"Parameter name"`
	Changed bool   `json:"changed" doc:"Whether the host changed"`
}

MutationResult says whether a write changed the host.

func Create

func Create(
	ctx context.Context,
	p Provider,
	in CreateInput,
) (MutationResult, error)

Create manages a parameter and applies it.

func Delete

func Delete(
	ctx context.Context,
	p Provider,
	in DeleteInput,
) (MutationResult, error)

Delete stops managing a parameter.

func Update

func Update(
	ctx context.Context,
	p Provider,
	in UpdateInput,
) (MutationResult, error)

Update sets a parameter, managing it if osapi did not, and applies it.

type Provider

type Provider interface {
	// List returns every parameter with its current value and whether osapi
	// manages it, narrowed to prefix and the parameters under it if set.
	List(ctx context.Context, prefix string) ([]Entry, error)
	// Get returns one parameter's current value and whether osapi manages it.
	Get(ctx context.Context, key string) (*Entry, error)
	// Create deploys a new sysctl conf file and applies it. Idempotent: returns Changed: false if already managed.
	Create(ctx context.Context, entry Entry) (*CreateResult, error)
	// Update sets a parameter and manages it, as Create does.
	Update(ctx context.Context, entry Entry) (*UpdateResult, error)
	// Delete removes a managed sysctl conf file and reloads defaults.
	Delete(ctx context.Context, key string) (*DeleteResult, error)
}

Provider is what each platform implements: debian.go on Debian and its family, darwin.go and linux.go elsewhere. The hand-written sysctl domain uses the same implementations until its cutover.

type Settings

type Settings struct {
	// Dir is where osapi writes its kernel parameter files, one per key,
	// named osapi-<key>.conf.
	Dir string `` /* 149-byte string literal not displayed */
}

Settings is sysctl's own configuration, under behaviors.sysctl. The agent reads it when it builds the provider.

type UpdateInput

type UpdateInput struct {
	Key   string `json:"key"   validate:"required,min=1,sysctl_key" doc:"Parameter name, such as net.ipv4.ip_forward"`
	Value string `json:"value" validate:"required,no_linebreak"     doc:"Value to set"`
}

UpdateInput is a parameter and its new value.

type UpdateResult

type UpdateResult struct {
	Key     string `json:"key"`
	Changed bool   `json:"changed"`
	Error   string `json:"error,omitempty"`
}

UpdateResult represents the outcome of a sysctl update operation.

Directories

Path Synopsis
Package mocks provides mock implementations for testing.
Package mocks provides mock implementations for testing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL