@@ -19,12 +19,10 @@ import (
19
19
"strings"
20
20
)
21
21
22
- // kernelArgs serializes+deserializes kernel boot parameters from/into a map.
23
- // Kernel docs: https://www.kernel.org/doc/Documentation/admin-guide/kernel-parameters.txt
24
- //
25
- // "key=value" will result in map["key"] = &"value"
26
- // "key=" will result in map["key"] = &""
27
- // "key" will result in map["key"] = nil
22
+ // kernelArg represents a key and optional value pair for passing an argument
23
+ // into the kernel. Additionally, it also saves the position of the argument
24
+ // in the whole command line input. This is important because the kernel stops reading
25
+ // everything after `--` and passes these keys into the init process
28
26
type kernelArg struct {
29
27
position uint
30
28
key string
@@ -38,10 +36,17 @@ func (karg kernelArg) String() string {
38
36
return karg .key
39
37
}
40
38
39
+ // kernelArgs serializes + deserializes kernel boot parameters from/into a map.
40
+ // Kernel docs: https://www.kernel.org/doc/Documentation/admin-guide/kernel-parameters.txt
41
+ //
42
+ // "key=value flag emptykey=" will be converted to
43
+ // map["key"] = { position: 0, key: "key", value: &"value" }
44
+ // map["flag"] = { position: 1, key: "flag", value: nil }
45
+ // map["emptykey"] = { position: 2, key: "emptykey", value: &"" }
41
46
type kernelArgs map [string ]kernelArg
42
47
43
- // serialize the sorted kernelArgs back to a string that can be provided
44
- // to the kernel
48
+ // Sorts the arguments by its position
49
+ // and serializes the map back into a single string
45
50
func (kargs kernelArgs ) String () string {
46
51
sortedArgs := make ([]kernelArg , 0 )
47
52
for _ , arg := range kargs {
@@ -58,6 +63,7 @@ func (kargs kernelArgs) String() string {
58
63
return strings .Join (args , " " )
59
64
}
60
65
66
+ // Add a new kernel argument to the kernelArgs, also the position is saved
61
67
func (kargs kernelArgs ) Add (key string , value * string ) {
62
68
kargs [key ] = kernelArg {
63
69
position : uint (len (kargs )),
@@ -66,7 +72,8 @@ func (kargs kernelArgs) Add(key string, value *string) {
66
72
}
67
73
}
68
74
69
- // deserialize the provided string to a kernelArgs map
75
+ // Parses an input string and deserializes it into a map
76
+ // saving its position in the command line
70
77
func parseKernelArgs (rawString string ) kernelArgs {
71
78
args := make (map [string ]kernelArg )
72
79
for index , kv := range strings .Fields (rawString ) {
0 commit comments