Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

osutil: add helper for getting the kernel command line #9687

Merged
merged 3 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions osutil/kcmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ func KernelCommandLineSplit(s string) (out []string, err error) {
// missing from the kernel command line or it has no value (eg. quiet), the key
// is omitted from the returned map.
func KernelCommandLineKeyValues(keys ...string) (map[string]string, error) {
buf, err := ioutil.ReadFile(procCmdline)
cmdline, err := KernelCommandLine()
if err != nil {
return nil, err
}
params, err := KernelCommandLineSplit(strings.TrimSpace(string(buf)))
params, err := KernelCommandLineSplit(cmdline)
if err != nil {
return nil, err
}
Expand All @@ -203,3 +203,12 @@ func KernelCommandLineKeyValues(keys ...string) (map[string]string, error) {
}
return m, nil
}

// KernelCommandLine returns the command line reported by the running kernel.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed for this PR, but we could simplify KernelCommandLineKeyValues to use this helper now instead of manually doing exactly the same thing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

func KernelCommandLine() (string, error) {
buf, err := ioutil.ReadFile(procCmdline)
if err != nil {
return "", err
}
return strings.TrimSpace(string(buf)), nil
}
17 changes: 17 additions & 0 deletions osutil/kcmdline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,20 @@ func (s *kcmdlineTestSuite) TestGetKernelCommandLineKeyValue(c *C) {
}
}
}

func (s *kcmdlineTestSuite) TestKernelCommandLine(c *C) {
d := c.MkDir()
newProcCmdline := filepath.Join(d, "cmdline")
restore := osutil.MockProcCmdline(newProcCmdline)
defer restore()

cmd, err := osutil.KernelCommandLine()
c.Assert(err, ErrorMatches, `.*/cmdline: no such file or directory`)
c.Check(cmd, Equals, "")

err = ioutil.WriteFile(newProcCmdline, []byte("foo bar baz panic=-1\n"), 0644)
c.Assert(err, IsNil)
cmd, err = osutil.KernelCommandLine()
c.Assert(err, IsNil)
c.Check(cmd, Equals, "foo bar baz panic=-1")
}