...
1[](https://godoc.org/github.com/pmorjan/kmod)
2
3# kmod
4
5A Go implementation of functions to load and unload Linux kernel modules.
6
7Module dependencies are loaded / unloaded automatically as defined in
8`<mod_dir>/modules.dep`.
9Kmod uses the syscall finit_module(2) to load a kernel file into the kernel
10and if that fails init_module(2). Compressed files are not supported
11directly but users can provide a custom function to uncompress and load a module
12file into the kernel. (**SetInitFunc**). This is to keep the number of external
13dependencies low and also allows maximum flexibility.
14
15See the simple examples below and [modprobe.go](cmd/modprobe/modprobe.go)
16for an complete example.
17
18```go
19// Load uncompressed kernel module
20package main
21
22import (
23 "log"
24
25 "github.com/pmorjan/kmod"
26)
27
28func main() {
29 k, err := kmod.New()
30 if err != nil {
31 log.Fatal(err)
32 }
33 if err := k.Load("brd", "rd_size=32768 rd_nr=16", 0); err != nil {
34 log.Fatal(err)
35 }
36}
37```
38
39```go
40// Load XZ compressed module
41package main
42
43import (
44 "io/ioutil"
45 "log"
46 "os"
47
48 "github.com/pmorjan/kmod"
49 "github.com/ulikunitz/xz"
50 "golang.org/x/sys/unix"
51)
52
53func main() {
54 k, err := kmod.New(kmod.SetInitFunc(modInit))
55 if err != nil {
56 log.Fatal(err)
57 }
58 if err := k.Load("brd", "rd_size=32768 rd_nr=16", 0); err != nil {
59 log.Fatal(err)
60 }
61}
62
63func modInit(path, params string, flags int) error {
64 f, err := os.Open(path)
65 if err != nil {
66 return err
67 }
68 rd, err := xz.NewReader(f)
69 if err != nil {
70 return err
71 }
72 buf, err := ioutil.ReadAll(rd)
73 if err != nil {
74 return err
75 }
76 return unix.InitModule(buf, params)
77}
78```
View as plain text