kmod

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2024 License: MIT Imports: 11 Imported by: 8

README

GoDoc

kmod

A Go implementation of functions to load and unload Linux kernel modules.

Module dependencies are loaded / unloaded automatically as defined in <mod_dir>/modules.dep. Kmod uses the syscall finit_module(2) to load a kernel file into the kernel and if that fails init_module(2). Compressed files are not supported directly but users can provide a custom function to uncompress and load a module file into the kernel. (SetInitFunc). This is to keep the number of external dependencies low and also allows maximum flexibility.

See the simple examples below and modprobe.go for an complete example.

// Load uncompressed kernel module
package main

import (
    "log"

    "github.com/pmorjan/kmod"
)

func main() {
    k, err := kmod.New()
    if err != nil {
        log.Fatal(err)
    }
    if err := k.Load("brd", "rd_size=32768 rd_nr=16", 0); err != nil {
        log.Fatal(err)
    }
}
// Load XZ compressed module
package main

import (
    "io/ioutil"
    "log"
    "os"

    "github.com/pmorjan/kmod"
    "github.com/ulikunitz/xz"
    "golang.org/x/sys/unix"
)

func main() {
    k, err := kmod.New(kmod.SetInitFunc(modInit))
    if err != nil {
        log.Fatal(err)
    }
    if err := k.Load("brd", "rd_size=32768 rd_nr=16", 0); err != nil {
        log.Fatal(err)
    }
}

func modInit(path, params string, flags int) error {
    f, err := os.Open(path)
    if err != nil {
        return err
    }
    rd, err := xz.NewReader(f)
    if err != nil {
        return err
    }
    buf, err := ioutil.ReadAll(rd)
    if err != nil {
        return err
    }
    return unix.InitModule(buf, params)
}

Documentation

Overview

Package kmod provides functions to load and unload Linux kernel modules.

Module dependencies are loaded / unloaded automatically according to <mod_dir>/modules.dep. Compressed module files can be loaded via a custom InitFunc provided by the caller. See SetInitFunc and cmd/modprobe for details.

Index

Constants

This section is empty.

Variables

View Source
var ErrModuleInUse = errors.New("module is in use")

ErrModuleInUse is the error resulting if a module can't be unloaded because it is in use.

View Source
var ErrModuleNotFound = errors.New("module not found")

ErrModuleNotFound is the error resulting if a module can't be found.

Functions

This section is empty.

Types

type InitFunc

type InitFunc func(filename string, params string, flags int) error

InitFunc provides a hook to load a kernel module into the kernel.

type Kmod

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

Kmod represents internal configuration

func New

func New(opts ...Option) (*Kmod, error)

New returns a new Kmod.

func (*Kmod) Dependencies

func (k *Kmod) Dependencies(name string) ([]string, error)

Dependencies returns a list of module dependencies.

func (*Kmod) Load

func (k *Kmod) Load(name, params string, flags int) error

Load loads a kernel module. If the module depends on other modules Load will try to load all dependencies first.

func (*Kmod) Unload

func (k *Kmod) Unload(name string) error

Unload unloads a module from the kernel. Unload also tries to unload all module dependencies that are no longer in use.

type Option

type Option func(*Kmod)

Option configures Kmod.

func SetConfigFile

func SetConfigFile(path string) Option

SetConfigFile returns an Option that specifies the (optional) configuration file for modules, default: /etc/modprobe.conf. The config file is used only for module parameters. options <name> parameter [parameter]...

func SetDryrun

func SetDryrun() Option

SetDryrun returns an Option that specifies to do everything but actually load or unload.

func SetIgnoreAlias

func SetIgnoreAlias() Option

SetIgnoreAlias returns an Option that specifies not to consult modules.alias to resolve aliases.

func SetIgnoreBuiltin

func SetIgnoreBuiltin() Option

SetIgnoreBuiltin returns an Option that specifies not to consult modules.builtin to find built-in modules.

func SetIgnoreStatus

func SetIgnoreStatus() Option

SetIgnoreStatus returns an Option that specifies not to consult /proc/modules to get the current status of a module.

func SetInitFunc

func SetInitFunc(fn InitFunc) Option

SetInitFunc returns an Option that sets fn to be used for loading module files into the kernel. The default function tries to use finit_module(2) first and if that failes init_module(2). To support compressed module files see the example cmd/modprobe.

func SetRootDir

func SetRootDir(dir string) Option

SetRootDir returns an Option that sets dir as root directory for modules, default: /lib/modules

func SetVerbose

func SetVerbose() Option

SetVerbose returns an Option that specifies to log info messages about what's going on.

Directories

Path Synopsis
cmd
modprobe Module

Jump to

Keyboard shortcuts

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