kmod

package
v0.0.0-...-808c2b3 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2017 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Package kmod performs bindings over libkmod to manipulate kernel modules from Golang seemlessly.

libkmod is a well-known library to handle kernel modules and which is used in the kmod set of tools (modprobe, modinfo, depmod etc ...). This Golang wrapper exposes common operations: list installed modules, retrieve information on a module, insert or remove a module from the tree.

The following file shows those abilities in practice are available

package main

import (
	"fmt"
	"github.com/ElyKar/golang-kmod/kmod"
)

func main() {
	km, err := kmod.NewKmod()

	if err != nil {
		panic(err)
	}

	// List all loaded modules
	list, err := km.List()
	if err != nil {
		panic(err)
	}

	for _, module := range list {
		fmt.Printf("%s, %d\n", module.Name(), module.Size())
	}

	// Finds a specific module and display some informations about it
	if pcspkr, err := km.ModuleFromName("pcspkr"); err == nil {
		infoMod, err := pcspkr.Info()
		if err != nil {
			panic(err)
		}

		fmt.Printf("Author: %s\n", infoMod["author"])
		fmt.Printf("Description: %s\n", infoMod["description"])
		fmt.Printf("License: %s\n", infoMod["license"])
	}

	// Insert a module and its dependencies
	_ = km.Insert("rtl2832")

	// Remove a module
	_ = km.Remove("rtl2832")
}

Copyright 2017 Tristan Claverie. All rights reserved. Use of this source code is governed by an Apache license that can be found in the LICENSE file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Kmod

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

Kmod wraps a kmod_context inside it. When garbage collected, all module handles will be freed by libkmod.

func NewKmod

func NewKmod() (*Kmod, error)

NewKmod creates a new context from default directories and configuration files. It will search for modules in /lib/modules/`uname -r` and configuration files in /run/modprobe.d, /etc/modprobe.d and /lib/modprobe.d.

This function returns an error in case the library encounters a problem for creating and populating the context.

The returned *Kmod must not be discarded, as releasing it will free the underlying C structure and all the modules in the context.

func (*Kmod) Insert

func (kmod *Kmod) Insert(name string) error

Insert a module in the tree with its name.

It returns an error if the module could not be found or if it could not be inserted.

To insert a wanted module:

kmod := NewKmod()
kmod.Insert("pcspkr")

If this module depends on others that are not yet loaded, depencies will be loaded.

func (*Kmod) List

func (kmod *Kmod) List() ([]*Module, error)

List returns a slice containing all loaded modules.

The method returns an error in case the list can't be retrieved.

func (*Kmod) Lookup

func (kmod *Kmod) Lookup(aliasName string) ([]*Module, error)

Lookup returns a slice of all modules matching 'alias_name'.

The method returns an error in case the lookup fails

func (*Kmod) ModuleFromName

func (kmod *Kmod) ModuleFromName(name string) (*Module, error)

ModuleFromName returns a module handle from its name.

The method returns an error if the module could not be found.

func (*Kmod) Remove

func (kmod *Kmod) Remove(name string) error

Remove a module from the current tree using its name.

It returns an error if the module could not be found or could not be removed.

Provided the module pcspkr is loaded and not used:

kmod := NewKmod()
kmod.Remove("pcspkr")

type Module

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

Module wraps a module from kmod.

func (*Module) Info

func (mod *Module) Info() (map[string]string, error)

Info returns the informations about a module (author, description etc ...).

This method returns an error if informations about the module couldn't be read

func (*Module) InstallCommands

func (mod *Module) InstallCommands() string

InstallCommands returns the install commands of the module.

func (*Module) Name

func (mod *Module) Name() string

Name returns the name of the module.

func (*Module) Options

func (mod *Module) Options() string

Options returns the options given to the module.

func (*Module) Path

func (mod *Module) Path() string

Path returns the path at which the module is stored.

func (*Module) RefCnt

func (mod *Module) RefCnt() int32

RefCnt returns the reference count for this module.

func (*Module) RemoveCommands

func (mod *Module) RemoveCommands() string

RemoveCommands returns the remove commands of the module.

func (*Module) Size

func (mod *Module) Size() uint64

Size returns the size of the module in bytes.

func (*Module) Versions

func (mod *Module) Versions() ([]*Version, error)

Versions returns the list of exported symbols for a module.

This method returns an error if the versions could not be retrieved

type ModuleList

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

ModuleList wraps a kmod_list structure.

type Version

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

Version structure for an individual entry in __versions.

func (*Version) Crc

func (ver *Version) Crc() uint64

Crc returns the crc for this instance of a Version.

func (*Version) Symbol

func (ver *Version) Symbol() string

Symbol returns the symbol for this instance of a Version.

Jump to

Keyboard shortcuts

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