os

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SWAP_EX_ENOMEM  = 2  // swapoff failed due to OOM
	SWAP_EX_FAILURE = 4  // swapoff failed due to other reason
	SWAP_EX_USAGE   = 16 // usage/permissions/syntax error

	SWAPON_FLAG_DISCARD       = 0x10000 // enable discard for swap
	SWAPON_FLAG_DISCARD_ONCE  = 0x20000 // discard swap area at swapon-time
	SWAPON_FLAG_DISCARD_PAGES = 0x40000 // discard page-clusters after use

	ETC_LOCATION           = "/etc"
	FSTAB_LOCATION         = "/etc/fstab"
	PROC_SWAPS_LOCATION    = "/proc/swaps"
	DISK_BY_UUID_LOCATION  = "/dev/disk/by-uuid"
	DISK_BY_LABEL_LOCATION = "/dev/disk/by-label"

	SwapCommentPrefix = "#" // prefix used when commenting out swap lines in fstab

)

SwapOff exit code: https://github.com/util-linux/util-linux/blob/master/sys-utils/swapoff.c#L43-L49 SwapOn Flags: https://github.com/util-linux/util-linux/blob/57d59a5cd5ba6c0b32cae27f5ce48241274f6e6e/sys-utils/swapon.c#L50-L63

Variables

View Source
var (
	ErrNamespace = errorx.NewNamespace("os")

	SwapErrTrait    = errorx.RegisterTrait("swap_error")
	FileErrTrait    = errorx.RegisterTrait("file_error")
	SystemdErrTrait = errorx.RegisterTrait("systemd_error")

	ErrSwapOutOfMemory    = ErrNamespace.NewType("out_of_memory", SwapErrTrait)
	ErrInvalidSwapFile    = ErrNamespace.NewType("invalid_swap_file", SwapErrTrait)
	ErrSwapUnknownSyscall = ErrNamespace.NewType("unknown_syscall", SwapErrTrait)
	ErrNonSyscall         = ErrNamespace.NewType("non_syscall", SwapErrTrait)
	ErrSwapNotSuperUser   = ErrNamespace.NewType("not_super_user", SwapErrTrait)
	ErrFileInaccessible   = ErrNamespace.NewType("file_inaccessible", FileErrTrait)
	ErrSwapDeviceNotFound = ErrNamespace.NewType("device_not_found", SwapErrTrait)
	ErrFileRead           = ErrNamespace.NewType("file_read_error", FileErrTrait)
	ErrFileWrite          = ErrNamespace.NewType("file_write_error", FileErrTrait)
	ErrSystemdConnection  = ErrNamespace.NewType("systemd_connection_error", SystemdErrTrait)
	ErrSystemdOperation   = ErrNamespace.NewType("systemd_operation_error", SystemdErrTrait)

	PathProperty         = errorx.RegisterProperty("path")
	SysErrorCodeProperty = errorx.RegisterProperty("sys_error_code")
)

Functions

func DaemonReload

func DaemonReload(ctx context.Context) error

DaemonReload reloads the systemd manager configuration. It is equivalent to running "systemctl daemon-reload".

func DisableService

func DisableService(ctx context.Context, name string) error

DisableService disables the specified service. It is equivalent to running "systemctl disable <service>". The service name can be provided with or without the .service suffix.

func DisableSwap

func DisableSwap() error

DisableSwap disables swap completely on the system. It performs the following steps: 1. Masks systemd swap units to prevent auto-activation 2. Turns off all active swap (swapoff -a) 3. Comments out swap entries in /etc/fstab to prevent reactivation on boot 4. Reloads systemd daemon

This ensures swap stays disabled even on systemd-based systems that auto-detect swap partitions by their filesystem signature.

func EnableService

func EnableService(ctx context.Context, name string) error

EnableService enables the specified service. It is equivalent to running "systemctl enable <service>". The service name can be provided with or without the .service suffix.

func EnableSwap

func EnableSwap() error

EnableSwap re-enables swap on the system. It performs the following steps: 1. Uncomments swap entries in /etc/fstab 2. Unmasks systemd swap units to allow auto-activation 3. Reloads systemd daemon 4. Activates all swap entries from /etc/fstab (swapon -a)

func GoDump

func GoDump()

GoDump dumps output of goroutine dump.

In order to have this in the output, one need to send a signal (e.g. SIGQUIT) signal to the process using kill: `kill -SIGQUIT <pid>` and register a callback for that OS signal(i.e. SIGQUIT) with the SignalHandler.

Example: assuming `handler` is an instance of SignalHandler and we are adding SIGQUIT handler for a systemd daemon.

handler.Register(syscall.SIGQUIT, func(os.Signal) {
		nmtos.GoDump()
		logger.Debug("Received SIGQUIT. " +
			"Check Go routine dump in logs using 'sudo journalctl --unit <daemon-name> --follow'." +
			"Continuing daemon operation as before...")
	})

func IsServiceEnabled

func IsServiceEnabled(ctx context.Context, name string) (bool, error)

IsServiceEnabled checks if the specified service is enabled. The service name can be provided with or without the .service suffix.

func IsServiceRunning

func IsServiceRunning(ctx context.Context, name string) (bool, error)

IsServiceRunning checks if the specified service is running. The service name can be provided with or without the .service suffix.

func MaskUnit

func MaskUnit(ctx context.Context, name string) error

MaskUnit masks the specified unit, preventing systemd from activating it. It is equivalent to running "systemctl mask <unit>". The unit name can be provided with or without the appropriate suffix (.service, .swap, etc.).

func NewSignalHandler

func NewSignalHandler() (SignalHandler, ShutdownFunc)

NewSignalHandler returns an instance of SignalHandler. Caller is expected call the returned ShutdownFunc to stop listening for all the events.

func RestartService

func RestartService(ctx context.Context, name string) error

RestartService starts the specified service. This function waits until the service is fully started. It is equivalent to running "systemctl start <service>". The service name can be provided with or without the .service suffix.

func StopService

func StopService(ctx context.Context, name string) error

StopService stops the specified service. This function waits until the service is fully stopped. It is equivalent to running "systemctl stop <service>". The service name can be provided with or without the .service suffix.

func SwapOff

func SwapOff(path string) error

SwapOff performs swapoff for a single path On success, it returns SWAP_EX_OK and nil error. On error, it returns a non-zero status code and a wrapped errorx error with details.

func SwapOffAll

func SwapOffAll() error

SwapOffAll attempts to disable all swap devices/files on the system based on /proc/swaps and /etc/fstab.

func SwapOn

func SwapOn(path string, flags int) error

func SwapOnAll

func SwapOnAll() error

func UnmaskUnit

func UnmaskUnit(ctx context.Context, name string) error

UnmaskUnit unmasks the specified unit, allowing systemd to activate it again. It is equivalent to running "systemctl unmask <unit>". The unit name can be provided with or without the appropriate suffix (.service, .swap, etc.).

Types

type ShutdownFunc

type ShutdownFunc func()

ShutdownFunc defines a cleanup function to be called on exit of the process

type SignalCallback

type SignalCallback func(os.Signal)

SignalCallback defines the OS signal callback function

type SignalHandler

type SignalHandler interface {
	// Register registers a callback function for the given os.Signal.
	//
	// It only allows a single callback function to be registered for an OS signal.
	// It returns error if a callback function already exists.
	//
	// If the handler has been shutdown already, it will return error for any new registrations.
	Register(sig os.Signal, cb SignalCallback) error

	// Unregister unregisters callback function for the given os.Signal.
	Unregister(sig os.Signal)

	// IsActive returns true if is active and able to process signals.
	//
	// If it is not active, attempting to register a callback will return error.
	IsActive() bool

	// HasCallback returns true if a callback is already registered for the given OS signal
	HasCallback(sig os.Signal) bool
}

SignalHandler exposes signal handling functionalities It allows only one callback to be registered for an OS signal.

Jump to

Keyboard shortcuts

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