kernel

package
v0.0.0-...-47294c1 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2024 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Overview

Package kernel is utilities for the Linux kernel

Index

Constants

This section is empty.

Variables

View Source
var Family = funcs.Memoize(func() (string, error) {
	pi, err := platformInformation()
	return pi.family, err
})

Family is the string describing the Linux distribution family (rhel, debian, etc.)

View Source
var HeaderProvider *headerProvider

HeaderProvider is a global object which is responsible for managing the fetching of kernel headers

View Source
var Machine = funcs.Memoize(func() (string, error) {
	u, err := uname()
	if err != nil {
		return "", err
	}
	return unix.ByteSliceToString(u.Machine[:]), nil
})

Machine is the equivalent of uname -m

View Source
var OnlineCPUs = funcs.Memoize(func() ([]uint, error) {
	return parseCPUMultipleRangeFromFile("/sys/devices/system/cpu/online")
})

OnlineCPUs returns the individual CPU numbers that are online for the system

View Source
var Platform = funcs.Memoize(func() (string, error) {
	pi, err := platformInformation()
	return pi.platform, err
})

Platform is the string describing the Linux distribution (ubuntu, debian, fedora, etc.)

View Source
var PlatformVersion = funcs.Memoize(func() (string, error) {
	pi, err := platformInformation()
	return pi.version, err
})

PlatformVersion is the string describing the platform version (`22.04` for Ubuntu jammy, etc.)

View Source
var PossibleCPUs = funcs.Memoize(func() (int, error) {
	return parseCPUSingleRangeFromFile("/sys/devices/system/cpu/possible")
})

PossibleCPUs returns the max number of CPUs a system may possibly have Logical CPU numbers must be of the form 0-n

View Source
var ProcFSRoot = funcs.MemoizeNoError(func() string {
	if v := os.Getenv("HOST_PROC"); v != "" {
		return v
	}
	if os.Getenv("DOCKER_DD_AGENT") != "" {
		if _, err := os.Stat("/host"); err == nil {
			return "/host/proc"
		}
	}
	return "/proc"
})

ProcFSRoot retrieves the current procfs dir we should use

View Source
var Release = funcs.Memoize(func() (string, error) {
	u, err := uname()
	if err != nil {
		return "", err
	}
	return unix.ByteSliceToString(u.Release[:]), nil
})

Release is the equivalent of uname -r

View Source
var RootNSPID = funcs.Memoize(func() (int, error) {
	pidPath := filepath.Join(ProcFSRoot(), "self")
	pidStr, err := os.Readlink(pidPath)
	if err != nil {
		return 0, err
	}

	return strconv.Atoi(pidStr)
})

RootNSPID returns the current PID from the root namespace

View Source
var SysFSRoot = funcs.MemoizeNoError(func() string {
	if v := os.Getenv("HOST_SYS"); v != "" {
		return v
	}
	if os.Getenv("DOCKER_DD_AGENT") != "" {
		if _, err := os.Stat("/host"); err == nil {
			return "/host/sys"
		}
	}
	return "/sys"
})

SysFSRoot retrieves the current sysfs dir we should use

View Source
var UnameVersion = funcs.Memoize(func() (string, error) {
	u, err := uname()
	if err != nil {
		return "", err
	}
	return unix.ByteSliceToString(u.Version[:]), nil
})

UnameVersion is the equivalent of uname -v

Functions

func AllPidsProcs

func AllPidsProcs(procRoot string) ([]int, error)

AllPidsProcs will return all pids under procRoot

func Arch

func Arch() string

Arch returns the kernel architecture value, often used within the kernel `include/arch` directory.

func GetCurrentIno

func GetCurrentIno() (uint32, error)

GetCurrentIno returns the ino number for the current network namespace

func GetInoForNs

func GetInoForNs(ns netns.NsHandle) (uint32, error)

GetInoForNs gets the inode number for the given network namespace

func GetKernelHeaders

func GetKernelHeaders(opts HeaderOptions, client statsd.ClientInterface) []string

GetKernelHeaders fetches and returns kernel headers for the currently running kernel.

The first time GetKernelHeaders is called, it will search the host for kernel headers, and if they cannot be found it will attempt to download headers to the configured header download directory.

Any subsequent calls to GetKernelHeaders will return the result of the first call. This is because kernel header downloading can be a resource intensive process, so we don't want to retry it an unlimited number of times.

func GetNetNamespaceFromPid

func GetNetNamespaceFromPid(procRoot string, pid int) (netns.NsHandle, error)

GetNetNamespaceFromPid gets the network namespace for a given `pid`

func GetNetNamespaces

func GetNetNamespaces(procRoot string) ([]netns.NsHandle, error)

GetNetNamespaces returns a list of network namespaces on the machine. The caller is responsible for calling Close() on each of the returned NsHandle's.

func GetNetNsInoFromPid

func GetNetNsInoFromPid(procRoot string, pid int) (uint32, error)

GetNetNsInoFromPid gets the network namespace inode number for the given `pid`

func GetProcessEnvVariable

func GetProcessEnvVariable(pid int, procRoot string, envVar string) (string, error)

GetProcessEnvVariable retrieves the given environment variable for the specified process ID, without loading the entire environment into memory. Will return an empty string if the variable is not found.

func GetRootNetNamespace

func GetRootNetNamespace(procRoot string) (netns.NsHandle, error)

GetRootNetNamespace gets the root network namespace

func HostProc

func HostProc(combineWith ...string) string

HostProc returns the location of a host's procfs. This can and will be overridden when running inside a container.

func IsIPv6Enabled

func IsIPv6Enabled() bool

IsIPv6Enabled returns whether IPv6 has been enabled on the host

func MountInfoPidPath

func MountInfoPidPath(pid int32) string

MountInfoPidPath returns the path to the mountinfo file of a pid in /proc

func ParseMountInfoFile

func ParseMountInfoFile(pid int32) ([]*mountinfo.Info, error)

ParseMountInfoFile collects the mounts for a specific process ID.

func WithAllProcs

func WithAllProcs(procRoot string, fn func(int) error) error

WithAllProcs will execute `fn` for every pid under procRoot. `fn` is passed the `pid`. If `fn` returns an error the iteration aborts, returning the last error returned from `fn`.

func WithNS

func WithNS(ns netns.NsHandle, fn func() error) error

WithNS executes the given function in the given network namespace, and then switches back to the previous namespace.

func WithRootNS

func WithRootNS(procRoot string, fn func() error) error

WithRootNS executes a function within root network namespace and then switch back to the previous namespace. If the thread is already in the root network namespace, the function is executed without calling SYS_SETNS.

Types

type HeaderOptions

type HeaderOptions struct {
	DownloadEnabled bool
	Dirs            []string
	DownloadDir     string

	AptConfigDir   string
	YumReposDir    string
	ZypperReposDir string
}

HeaderOptions are options for the kernel header download process

type LockdownMode

type LockdownMode string

LockdownMode defines a lockdown type

const (
	// None mode
	None LockdownMode = "none"
	// Integrity mode
	Integrity LockdownMode = "integrity"
	// Confidentiality mode
	Confidentiality LockdownMode = "confidentiality"
	// Unknown mode
	Unknown LockdownMode = "unknown"
)

func GetLockdownMode

func GetLockdownMode() LockdownMode

GetLockdownMode returns the lockdown

type UbuntuKernelVersion

type UbuntuKernelVersion struct {
	Major  int
	Minor  int
	Patch  int // always 0
	Abi    int
	Flavor string
}

UbuntuKernelVersion represents a version from an ubuntu kernel Please see: https://ubuntu.com/kernel for the documentation of this scheme

func NewUbuntuKernelVersion

func NewUbuntuKernelVersion(unameRelease string) (*UbuntuKernelVersion, error)

NewUbuntuKernelVersion parses the ubuntu release string and returns a structure with each extracted fields

type Version

type Version uint32

Version is a numerical representation of a kernel version

func HostVersion

func HostVersion() (Version, error)

HostVersion returns the running kernel version of the host

func MustHostVersion

func MustHostVersion() Version

MustHostVersion returns the running kernel version of the host

func ParseReleaseString

func ParseReleaseString(releaseString string) (Version, error)

ParseReleaseString converts a release string with format 4.4.2[-1] to a kernel version number in LINUX_VERSION_CODE format. That is, for kernel "a.b.c", the version number will be (a<<16 + b<<8 + c)

func ParseVersion

func ParseVersion(s string) Version

ParseVersion parses a string in the format of x.x.x to a Version

func VersionCode

func VersionCode(major, minor, patch byte) Version

VersionCode returns a Version computed from the individual parts of a x.x.x version

func (Version) Major

func (v Version) Major() uint8

Major returns the major number of the version code

func (Version) Minor

func (v Version) Minor() uint8

Minor returns the minor number of the version code

func (Version) Patch

func (v Version) Patch() uint8

Patch returns the patch number of the version code

func (Version) String

func (v Version) String() string

String returns a string representing the version in x.x.x format

Jump to

Keyboard shortcuts

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