Skip to content

Commit 4dccc1c

Browse files
authored
Merge pull request #493 from vteratipally/kernel_cmdline_parameters
add code to retrieve kernel command line parameters
2 parents 8f2a94f + 4085da8 commit 4dccc1c

File tree

5 files changed

+192
-1
lines changed

5 files changed

+192
-1
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors All rights reserved.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package system
15+
16+
import (
17+
"encoding/json"
18+
"fmt"
19+
"strings"
20+
)
21+
22+
var cmdlineFilePath = "/proc/cmdline"
23+
24+
type CmdlineArg struct {
25+
Key string `json:"key"`
26+
Value string `json:"value"`
27+
}
28+
29+
func (d CmdlineArg) String() string {
30+
s, _ := json.Marshal(d)
31+
return string(s)
32+
}
33+
34+
// As per the documentation kernel command line parameters can also be specified
35+
// within double quotes and are separated with spaces.
36+
// https://www.kernel.org/doc/html/v4.14/admin-guide/kernel-parameters.html#the-kernel-s-command-line-parameters
37+
var withinQuotes = false
38+
39+
func splitAfterSpace(inputChar rune) bool {
40+
// words with space within double quotes cannot be split.
41+
// so we track the start quote and when we find the
42+
// end quote, then we reiterate.
43+
if inputChar == '"' {
44+
withinQuotes = !withinQuotes
45+
return false
46+
}
47+
//ignore spaces when it is within quotes.
48+
if withinQuotes {
49+
return false
50+
}
51+
return inputChar == ' '
52+
}
53+
54+
// CmdlineArgs returns all the kernel cmdline. It is read from cat /proc/cmdline.
55+
func CmdlineArgs() ([]CmdlineArg, error) {
56+
lines, err := ReadFileIntoLines(cmdlineFilePath)
57+
if err != nil {
58+
return nil, fmt.Errorf("error reading the file %s, %v", cmdlineFilePath, err)
59+
}
60+
if len(lines) < 1 {
61+
return nil, fmt.Errorf("no lines are retured")
62+
}
63+
cmdlineArgs := strings.FieldsFunc(lines[0], splitAfterSpace)
64+
var result = make([]CmdlineArg, 0, len(cmdlineArgs))
65+
// for commandline only one line is returned.
66+
for _, words := range cmdlineArgs {
67+
// Ignore the keys that start with double quotes
68+
if strings.Index(words, "\"") == 0 {
69+
continue
70+
}
71+
tokens := strings.Split(words, "=")
72+
if len(tokens) < 2 {
73+
var stats = CmdlineArg{
74+
Key: tokens[0],
75+
}
76+
result = append(result, stats)
77+
} else {
78+
//remove quotes in the values
79+
trimmedValue := strings.Trim(tokens[1], "\"'")
80+
var stats = CmdlineArg{
81+
Key: tokens[0],
82+
Value: trimmedValue,
83+
}
84+
result = append(result, stats)
85+
}
86+
}
87+
return result, nil
88+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package system
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestCmdlineStats(t *testing.T) {
11+
testcases := []struct {
12+
name string
13+
fakeCmdlineFilePath string
14+
expectedCmdlineArgs []CmdlineArg
15+
unExpectedCmdlineArgs []CmdlineArg
16+
}{
17+
{
18+
name: "default_cos",
19+
fakeCmdlineFilePath: "testdata/cmdline_args_key_cos.txt",
20+
expectedCmdlineArgs: []CmdlineArg{
21+
{
22+
Key: "console",
23+
Value: "ttyS0",
24+
},
25+
{
26+
Key: "boot",
27+
Value: "local",
28+
},
29+
{
30+
Key: "cros_efi",
31+
},
32+
},
33+
unExpectedCmdlineArgs: []CmdlineArg{
34+
{
35+
Key: "hashstart",
36+
Value: "4077568",
37+
},
38+
{
39+
Key: "vroot",
40+
},
41+
},
42+
},
43+
{
44+
name: "sample",
45+
fakeCmdlineFilePath: "testdata/cmdline_args_sample.txt",
46+
expectedCmdlineArgs: []CmdlineArg{
47+
{
48+
Key: "key1",
49+
Value: "value1",
50+
},
51+
{
52+
Key: "key3",
53+
Value: "value2 value3",
54+
},
55+
{
56+
Key: "key2",
57+
},
58+
},
59+
unExpectedCmdlineArgs: []CmdlineArg{
60+
{
61+
Key: "value2",
62+
Value: "value3",
63+
},
64+
{
65+
Key: "value3",
66+
},
67+
},
68+
},
69+
}
70+
for _, test := range testcases {
71+
t.Run(test.name, func(t *testing.T) {
72+
originalCmdlineFilePath := cmdlineFilePath
73+
defer func() {
74+
cmdlineFilePath = originalCmdlineFilePath
75+
}()
76+
77+
cmdlineFilePath = test.fakeCmdlineFilePath
78+
cmdlineArgs, err := CmdlineArgs()
79+
if err != nil {
80+
t.Errorf("Unexpected error retrieving cmdlineArgs: %v\nCmdlineArgsFilePath: %s\n", err, cmdlineFilePath)
81+
}
82+
for _, expectedCmdlineArg := range test.expectedCmdlineArgs {
83+
assert.Contains(t, cmdlineArgs, expectedCmdlineArg, "Failed to find cmdlineArgs: %v\n", expectedCmdlineArg)
84+
}
85+
for _, unExpectedCmdlineArg := range test.unExpectedCmdlineArgs {
86+
assert.NotContains(t, cmdlineArgs, unExpectedCmdlineArg, "Unpected expected cmdlinearg found: %v\n", unExpectedCmdlineArg)
87+
}
88+
})
89+
}
90+
}
91+
92+
func TestCmdlineStats_String(t *testing.T) {
93+
v := CmdlineArg{
94+
Key: "test",
95+
Value: "test",
96+
}
97+
e := `{"key":"test","value":"test"}`
98+
assert.Equal(t,
99+
e, fmt.Sprintf("%v", v), "CmdlineArg string is invalid: %v", v)
100+
101+
}

pkg/util/metrics/system/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"os"
1919
)
2020

21-
// ReadFile reads contents from a file and returns lines.
21+
// ReadFileIntoLines reads contents from a file and returns lines.
2222
func ReadFileIntoLines(filename string) ([]string, error) {
2323
file, err := os.Open(filename)
2424
if err != nil {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BOOT_IMAGE=/syslinux/vmlinuz.A init=/usr/lib/systemd/systemd boot=local rootwait ro noresume noswap loglevel=7 noinitrd console=ttyS0 virtio_net.napi_tx=1 systemd.unified_cgroup_hierarchy=false systemd.legacy_systemd_cgroup_controller=false csm.disabled=1 loadpin.exclude=kernel-module modules-load=loadpin_trigger module.sig_enforce=1 dm_verity.error_behavior=3 dm_verity.max_bios=-1 dm_verity.dev_wait=1 i915.modeset=1 cros_efi "dm=1 vroot none ro 1,0 verity hashstart=4077568"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
key1=value1 key2 key3="value2 value3"

0 commit comments

Comments
 (0)