Skip to content

Commit

Permalink
Merge pull request #9687 from bboozzoo/bboozzoo/uc20-get-kernel-cmdline
Browse files Browse the repository at this point in the history
osutil: add helper for getting the kernel command line
  • Loading branch information
bboozzoo committed Nov 27, 2020
2 parents ca021d1 + 5ddac77 commit 5c11fd7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
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.
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")
}

0 comments on commit 5c11fd7

Please sign in to comment.