hostswap

package
v1.5.17 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 Imports: 10 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 hostswap reports a host's RAM and swap and plans an opt-in swapfile. Nothing here changes the host unless Apply is called with an explicit request.

Index

Constants

View Source
const (
	// DefaultPath is where the helper creates its swapfile.
	DefaultPath = "/swapfile-levelrail"
	// FstabPath is the file the persistent entry is appended to.
	FstabPath = "/etc/fstab"

	// MaxSizeMB bounds a swapfile so a typo cannot fill the disk.
	MaxSizeMB = 262144
)

Variables

This section is empty.

Functions

func Apply

func Apply(ctx context.Context, p Plan, fstab string) error

Apply runs the plan. It needs root on Linux and removes the file again if a later step fails, so a failed run leaves the host as it was.

func SizeMB

func SizeMB(setting int, ramBytes int64) int

SizeMB resolves the swapfile size: a positive setting wins, otherwise the size matches RAM (the usual guidance for small hosts).

func ValidatePath

func ValidatePath(path string) error

ValidatePath accepts only a clean absolute path under an allowed prefix with no character that could break an fstab line or an argv value.

Types

type Plan

type Plan struct {
	Path     string   `json:"path"`
	SizeMB   int      `json:"size_mb"`
	Commands []string `json:"commands"`
	Notes    []string `json:"notes,omitempty"`
}

Plan is the exact set of steps to add a swapfile.

func BuildPlan

func BuildPlan(st State, path string, sizeMB int, freeBytes int64, fstab string) (Plan, error)

BuildPlan refuses when any swap setup exists, the path is taken or free disk cannot hold the file twice over (the file plus room to keep running).

type State

type State struct {
	Supported  bool     `json:"supported"`
	RAMBytes   int64    `json:"ram_bytes"`
	SwapBytes  int64    `json:"swap_bytes"`
	Swapfiles  []string `json:"swapfiles,omitempty"`
	Swappiness int      `json:"swappiness"` // -1 when unknown
}

State is what the host reports about memory and swap.

func Read

func Read(root string) State

Read inspects root+"/proc"; pass "" for the live host.

func (State) Summary

func (s State) Summary() string

Summary is one plain sentence for doctor rows.

Jump to

Keyboard shortcuts

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