-
Notifications
You must be signed in to change notification settings - Fork 5
/
kmod_linux.go
507 lines (441 loc) · 11 KB
/
kmod_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//go:generate stringer -type status
// 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.
//
package kmod
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"golang.org/x/sys/unix"
)
// ErrModuleNotFound is the error resulting if a module can't be found.
var ErrModuleNotFound = errors.New("module not found")
// ErrModuleInUse is the error resulting if a module can't be unloaded because
// it is in use.
var ErrModuleInUse = errors.New("module is in use")
// InitFunc provides a hook to load a kernel module into the kernel.
type InitFunc func(filename string, params string, flags int) error
// Kmod represents internal configuration
type Kmod struct {
dryrun bool
ignoreAlias bool
ignoreBuiltin bool
ignoreStatus bool
modConfig string
modDir string
modInitFunc InitFunc
modRootdir string
verbose bool
}
// Option configures Kmod.
type Option func(*Kmod)
// SetDryrun returns an Option that specifies to do everything but actually load or unload.
func SetDryrun() Option { return func(k *Kmod) { k.dryrun = true } }
// SetIgnoreAlias returns an Option that specifies not to consult modules.alias
// to resolve aliases.
func SetIgnoreAlias() Option { return func(k *Kmod) { k.ignoreAlias = true } }
// SetIgnoreBuiltin returns an Option that specifies not to consult modules.builtin
// to find built-in modules.
func SetIgnoreBuiltin() Option { return func(k *Kmod) { k.ignoreBuiltin = true } }
// SetIgnoreStatus returns an Option that specifies not to consult /proc/modules
// to get the current status of a module.
func SetIgnoreStatus() Option { return func(k *Kmod) { k.ignoreStatus = true } }
// 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 SetInitFunc(fn InitFunc) Option {
return func(k *Kmod) { k.modInitFunc = fn }
}
// 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 SetConfigFile(path string) Option {
return func(k *Kmod) { k.modConfig = path }
}
// SetRootDir returns an Option that sets dir as root directory for modules,
// default: /lib/modules
func SetRootDir(dir string) Option { return func(k *Kmod) { k.modRootdir = dir } }
// SetVerbose returns an Option that specifies to log info messages about what's going on.
func SetVerbose() Option { return func(k *Kmod) { k.verbose = true } }
// New returns a new Kmod.
func New(opts ...Option) (*Kmod, error) {
k := &Kmod{
modConfig: "/etc/modprobe.conf",
modRootdir: "/lib/modules",
}
for _, opt := range opts {
opt(k)
}
var u unix.Utsname
if err := unix.Uname(&u); err != nil {
return nil, err
}
rel := string(u.Release[:bytes.IndexByte(u.Release[:], 0)])
k.modDir = filepath.Join(k.modRootdir, rel)
if _, err := os.Stat(filepath.Join(k.modDir, "modules.dep")); err != nil {
return nil, err
}
return k, nil
}
// Load loads a kernel module. If the module depends on other modules
// Load will try to load all dependencies first.
func (k *Kmod) Load(name, params string, flags int) error {
name = cleanName(name)
realname, err := k.checkAlias(name)
if err != nil {
return fmt.Errorf("get alias %s failed: %w", name, err)
}
if realname != "" {
k.infof("%s is alias for %s", name, realname)
name = realname
}
builtin, err := k.isBuiltin(name)
if err != nil {
return fmt.Errorf("check builtin %s failed: %w", name, err)
}
if builtin {
k.infof("%s is builtin", name)
return nil
}
modules, err := k.modDeps(name)
if err != nil {
return err
}
if err := k.applyConfig(modules); err != nil {
return err
}
modules[0].params += " " + params
modules[0].flags = flags
// load dependencies first, ignore errors
for i := len(modules) - 1; i > 0; i-- {
_ = k.load(modules[i])
}
// load target module, check error
err = k.load(modules[0])
if errors.Is(err, unix.EEXIST) {
return nil
}
if err != nil {
return fmt.Errorf("load %s failed: %w", modules[0].name, err)
}
return nil
}
// Unload unloads a module from the kernel. Unload also tries to unload all
// module dependencies that are no longer in use.
func (k *Kmod) Unload(name string) error {
name = cleanName(name)
alias, err := k.checkAlias(name)
if err != nil {
return fmt.Errorf("check alias %s failed: %w", name, err)
}
if alias != "" {
k.infof("%s is alias for %s", name, alias)
name = alias
}
builtin, err := k.isBuiltin(name)
if err != nil {
return fmt.Errorf("check builtin %s failed: %w", name, err)
}
if builtin {
k.infof("%s is builtin", name)
return nil
}
modules, err := k.modDeps(name)
if err != nil {
return err
}
// unload target module, check error
if err := k.unload(modules[0]); err != nil {
if err == unix.EBUSY || err == unix.EAGAIN {
return ErrModuleInUse
}
if err != unix.ENOENT {
return err
}
}
// unload dependencies, ignore errors
for i := 1; i < len(modules); i++ {
_ = k.unload(modules[i])
}
return nil
}
// Dependencies returns a list of module dependencies.
func (k *Kmod) Dependencies(name string) ([]string, error) {
name = cleanName(name)
realname, err := k.checkAlias(name)
if err != nil {
return nil, fmt.Errorf("get alias %s failed: %w", name, err)
}
if realname != "" {
k.infof("%s is alias for %s", name, realname)
name = realname
}
builtin, err := k.isBuiltin(name)
if err != nil {
return nil, fmt.Errorf("check builtin %s failed: %w", name, err)
}
if builtin {
return nil, fmt.Errorf("%s is builtin", name)
}
modules, err := k.modDeps(name)
if err != nil {
return nil, err
}
var list []string
for i := len(modules) - 1; i >= 0; i-- {
list = append(list, modules[i].path)
}
return list, nil
}
func (k *Kmod) isBuiltin(name string) (bool, error) {
if k.ignoreBuiltin {
return false, nil
}
f, err := os.Open(filepath.Join(k.modDir, "modules.builtin"))
if err != nil {
return false, err
}
defer f.Close()
var found bool
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if pathToName(line) == name {
found = true
break
}
}
if err := scanner.Err(); err != nil {
return false, err
}
return found, nil
}
type module struct {
name string
path string
params string
flags int
}
type status int
const (
unknown status = iota
unloaded
unloading
loading
live
inuse
)
// /proc/modules
// name | memory size | reference count | references | state: <Live|Loading|Unloading>
// macvlan 28672 1 macvtap, Live 0x0000000000000000
func (k *Kmod) modStatus(name string) (status, error) {
var state status = unknown
if k.ignoreStatus {
return state, nil
}
f, err := os.Open("/proc/modules")
if err != nil {
return state, err
}
defer f.Close()
state = unloaded
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if fields[0] == name {
if fields[2] != "0" {
state = inuse
break
}
switch fields[4] {
case "Live":
state = live
case "Loading":
state = loading
case "Unloading":
state = unloading
}
break
}
}
if err := scanner.Err(); err != nil {
return state, err
}
return state, nil
}
// modules.alias
// alias fs-xfs xfs
func (k *Kmod) checkAlias(name string) (string, error) {
if k.ignoreAlias {
return "", nil
}
f, err := os.Open(filepath.Join(k.modDir, "modules.alias"))
if err != nil {
return "", err
}
defer f.Close()
var realname string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
fields := strings.Fields(line)
if len(fields) >= 3 {
if cleanName(fields[1]) == name {
realname = fields[2]
break
}
}
}
if err := scanner.Err(); err != nil {
return "", err
}
return realname, nil
}
// modDeps returns a module and all its depenencies
func (k *Kmod) modDeps(name string) ([]module, error) {
f, err := os.Open(filepath.Join(k.modDir, "modules.dep"))
if err != nil {
return nil, err
}
defer f.Close()
var deps []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if pathToName(strings.TrimSuffix(fields[0], ":")) == name {
deps = fields
break
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
if len(deps) == 0 {
return nil, ErrModuleNotFound
}
deps[0] = strings.TrimSuffix(deps[0], ":")
var modules []module
for _, v := range deps {
modules = append(modules, module{
name: pathToName(v),
path: filepath.Join(k.modDir, v),
})
}
return modules, nil
}
// modprobe.conf
// options <modname> option [option]...
func (k *Kmod) applyConfig(modules []module) error {
if k.modConfig == "" {
return nil
}
f, err := os.Open(k.modConfig)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "#") {
continue
}
fields := strings.Fields(line)
if len(fields) < 3 || fields[0] != "options" {
continue
}
name := cleanName(fields[1])
for i, m := range modules {
if m.name == name {
m.params = strings.Join(fields[2:], " ")
modules[i] = m
break
}
}
}
return scanner.Err()
}
func (k *Kmod) load(m module) error {
state, err := k.modStatus(m.name)
if err != nil {
return err
}
if state >= loading {
return nil
}
if k.dryrun {
return nil
}
k.infof("loading %s %s %s", m.name, m.path, m.params)
if k.modInitFunc != nil {
return k.modInitFunc(m.path, m.params, m.flags)
}
f, err := os.Open(m.path)
if err != nil {
return err
}
defer f.Close()
// first try finit_module(2), then init_module(2)
err = unix.FinitModule(int(f.Fd()), m.params, m.flags)
if errors.Is(err, unix.ENOSYS) {
if m.flags != 0 {
return err
}
buf, err := io.ReadAll(f)
if err != nil {
return err
}
return unix.InitModule(buf, m.params)
}
return err
}
func (k *Kmod) unload(m module) error {
state, err := k.modStatus(m.name)
if err != nil {
return err
}
if state == unloading || state == unloaded {
return nil
}
if state == inuse {
return ErrModuleInUse
}
if k.dryrun {
return nil
}
k.infof("unloading %s", m.name)
return unix.DeleteModule(m.name, 0)
}
func (k *Kmod) infof(format string, a ...interface{}) {
if k.verbose {
log.Printf(format, a...)
}
}
func cleanName(s string) string {
return strings.ReplaceAll(strings.TrimSpace(s), "-", "_")
}
func pathToName(s string) string {
s = filepath.Base(s)
for ext := filepath.Ext(s); ext != ""; ext = filepath.Ext(s) {
s = strings.TrimSuffix(s, ext)
}
return cleanName(s)
}