/
kernel.go
56 lines (45 loc) · 1.22 KB
/
kernel.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
package kernel
import (
"io/ioutil"
"strings"
"github.com/rs/zerolog/log"
"github.com/google/shlex"
)
// Params represent the parameters passed to the kernel at boot
type Params map[string][]string
// Exists checks if a key is present in the kernel parameters
func (k Params) Exists(key string) bool {
_, ok := k[key]
return ok
}
// Get returns the values link to a key and a boolean
// boolean if true when the key exists in the params or false otherwise
// a nil list, and a true will be returned if the `key` is set in kernel params, but with
// no associated value
func (k Params) Get(key string) ([]string, bool) {
v, ok := k[key]
return v, ok
}
func parseParams(content string) Params {
options := Params{}
cmdline, _ := shlex.Split(strings.TrimSpace(content))
for _, option := range cmdline {
kv := strings.SplitN(option, "=", 2)
key := kv[0]
if len(kv) == 2 {
options[key] = append(options[key], kv[1])
} else {
options[key] = nil
}
}
return options
}
//GetParams Get kernel cmdline arguments
func GetParams() Params {
content, err := ioutil.ReadFile("/proc/cmdline")
if err != nil {
log.Error().Err(err).Msg("Failed to read /proc/cmdline")
return Params{}
}
return parseParams(string(content))
}