forked from TrenchBoot/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_linux.go
75 lines (66 loc) · 2.13 KB
/
init_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
// Copyright 2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"log"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"github.com/u-root/u-root/pkg/cmdline"
"github.com/u-root/u-root/pkg/kmodule"
)
// installModules installs kernel modules (.ko files) from /lib/modules.
// Useful for modules that need to be loaded for boot (ie a network
// driver needed for netboot)
func installModules() {
modulePattern := "/lib/modules/*.ko"
files, err := filepath.Glob(modulePattern)
if err != nil {
log.Printf("installModules: %v", err)
return
}
if len(files) == 0 {
// Since it is common for users to not have modules, no need to error or
// print if there are none to install
return
}
for _, filename := range files {
f, err := os.Open(filename)
if err != nil {
log.Printf("installModules: can't open %q: %v", filename, err)
continue
}
// Module flags are passed to the command line in the form modulename.flag=val
// And must be passed to FileInit as flag=val to be installed properly
moduleName := strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename))
flags := cmdline.FlagsForModule(moduleName)
err = kmodule.FileInit(f, flags, 0)
f.Close()
if err != nil {
log.Printf("installModules: can't install %q: %v", filename, err)
}
}
}
func init() {
osInitGo = runOSInitGo
}
func runOSInitGo() {
// Install modules before exec-ing into user mode below
installModules()
// systemd is "special". If we are supposed to run systemd, we're
// going to exec, and if we're going to exec, we're done here.
// systemd uber alles.
initFlags := cmdline.GetInitFlagMap()
// systemd gets upset when it discovers it isn't really process 1, so
// we can't start it in its own namespace. I just love systemd.
systemd, present := initFlags["systemd"]
systemdEnabled, boolErr := strconv.ParseBool(systemd)
if present && boolErr == nil && systemdEnabled {
if err := syscall.Exec("/inito", []string{"/inito"}, os.Environ()); err != nil {
log.Printf("Lucky you, systemd failed: %v", err)
}
}
}