Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
agent: parse kernel command line as soon as possible
Browse files Browse the repository at this point in the history
The kernel command line should be parsed as soon as possible to allow the agent
setup the system according to the configuration, for example what `initMounts`
it has to mount or not.

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Mar 4, 2020
1 parent 56190a9 commit ddd9188
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 98 deletions.
15 changes: 9 additions & 6 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ var debugConsoleVSockPort = uint32(0)
// Timeout waiting for a device to be hotplugged
var hotplugTimeout = 3 * time.Second

// Specify the log level
var logLevel = defaultLogLevel

// commType is used to denote the communication channel type used.
type commType int

Expand Down Expand Up @@ -984,12 +987,7 @@ func logsToVPort() {
func (s *sandbox) initLogger(ctx context.Context) error {
agentLog.Logger.Formatter = &logrus.TextFormatter{DisableColors: true, TimestampFormat: time.RFC3339Nano}

config := newConfig(defaultLogLevel)
if err := config.getConfig(kernelCmdlineFile); err != nil {
agentLog.WithError(err).Warn("Failed to get config from kernel cmdline")
}

agentLog.Logger.SetLevel(config.logLevel)
agentLog.Logger.SetLevel(logLevel)

agentLog = agentLog.WithField("debug_console", debugConsole)

Expand Down Expand Up @@ -1426,6 +1424,9 @@ func initAgentAsInit() error {
if err := generalMount(); err != nil {
return err
}
if err := parseKernelCmdline(); err != nil {
return err
}
if err := cgroupsMount(); err != nil {
return err
}
Expand Down Expand Up @@ -1477,6 +1478,8 @@ func realMain() error {
if err = initAgentAsInit(); err != nil {
panic(fmt.Sprintf("failed to setup agent as init: %v", err))
}
} else if err := parseKernelCmdline(); err != nil {
return err
}

r := &agentReaper{}
Expand Down
24 changes: 7 additions & 17 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,28 @@ const (
debugConsoleFlag = optionPrefix + "debug_console"
debugConsoleVPortFlag = optionPrefix + "debug_console_vport"
hotplugTimeoutFlag = optionPrefix + "hotplug_timeout"
kernelCmdlineFile = "/proc/cmdline"
traceModeStatic = "static"
traceModeDynamic = "dynamic"
traceTypeIsolated = "isolated"
traceTypeCollated = "collated"
defaultTraceType = traceTypeIsolated
)

type agentConfig struct {
logLevel logrus.Level
}

func newConfig(level logrus.Level) agentConfig {
return agentConfig{
logLevel: level,
}
}
var kernelCmdlineFile = "/proc/cmdline"

//Get the agent configuration from kernel cmdline
func (c *agentConfig) getConfig(cmdLineFile string) error {
if cmdLineFile == "" {
func parseKernelCmdline() error {
if kernelCmdlineFile == "" {
return grpcStatus.Error(codes.FailedPrecondition, "Kernel cmdline file cannot be empty")
}

kernelCmdline, err := ioutil.ReadFile(cmdLineFile)
kernelCmdline, err := ioutil.ReadFile(kernelCmdlineFile)
if err != nil {
return err
}

words := strings.Fields(string(kernelCmdline))
for _, word := range words {
if err := c.parseCmdlineOption(word); err != nil {
if err := parseCmdlineOption(word); err != nil {
agentLog.WithFields(logrus.Fields{
"error": err,
"option": word,
Expand All @@ -70,7 +60,7 @@ func (c *agentConfig) getConfig(cmdLineFile string) error {
}

//Parse a string that represents a kernel cmdline option
func (c *agentConfig) parseCmdlineOption(option string) error {
func parseCmdlineOption(option string) error {
const (
optionPosition = iota
valuePosition
Expand Down Expand Up @@ -106,7 +96,7 @@ func (c *agentConfig) parseCmdlineOption(option string) error {
if err != nil {
return err
}
c.logLevel = level
logLevel = level
if level == logrus.DebugLevel {
debug = true
}
Expand Down
109 changes: 34 additions & 75 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,47 @@ package main
import (
"io/ioutil"
"os"
"reflect"
"testing"
"time"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func TestNewConfig(t *testing.T) {
assert := assert.New(t)

testLogLevel := logrus.DebugLevel

expectedConfig := agentConfig{
logLevel: testLogLevel,
}

config := newConfig(testLogLevel)

assert.True(reflect.DeepEqual(config, expectedConfig),
"Config structures should be identical: got %+v, expecting %+v",
config, expectedConfig)
}

func TestParseCmdlineOptionEmptyOption(t *testing.T) {
assert := assert.New(t)

a := &agentConfig{}

err := a.parseCmdlineOption("")
err := parseCmdlineOption("")
assert.NoError(err, "%v", err)
}

func TestParseCmdlineOptionWrongOptionValue(t *testing.T) {
assert := assert.New(t)

a := &agentConfig{}

wrongOption := logLevelFlag + "=debgu"

err := a.parseCmdlineOption(wrongOption)
err := parseCmdlineOption(wrongOption)
assert.Errorf(err, "Parsing should fail because wrong option %q", wrongOption)
}

func TestParseCmdlineOptionWrongOptionParam(t *testing.T) {
assert := assert.New(t)

a := &agentConfig{}

wrongOption := "agent.lgo=debug"

err := a.parseCmdlineOption(wrongOption)
err := parseCmdlineOption(wrongOption)
assert.Errorf(err, "Parsing should fail because wrong option %q", wrongOption)
}

func TestParseCmdlineOptionCorrectOptions(t *testing.T) {
assert := assert.New(t)

a := &agentConfig{}

logFlagList := []string{"debug", "info", "warn", "error", "fatal", "panic"}

for _, logFlag := range logFlagList {
debug = false
option := logLevelFlag + "=" + logFlag

err := a.parseCmdlineOption(option)
err := parseCmdlineOption(option)
assert.NoError(err, "%v", err)

if logFlag == "debug" {
Expand All @@ -87,23 +61,19 @@ func TestParseCmdlineOptionCorrectOptions(t *testing.T) {
func TestParseCmdlineOptionIncorrectOptions(t *testing.T) {
assert := assert.New(t)

a := &agentConfig{}

logFlagList := []string{"debg", "ifo", "wan", "eror", "ftal", "pnic"}

for _, logFlag := range logFlagList {
option := logLevelFlag + "=" + logFlag

err := a.parseCmdlineOption(option)
err := parseCmdlineOption(option)
assert.Errorf(err, "Should fail because of incorrect option %q", logFlag)
}
}

func TestParseCmdlineOptionDevMode(t *testing.T) {
assert := assert.New(t)

a := &agentConfig{}

type testData struct {
option string
expectDevModeEnabled bool
Expand All @@ -126,7 +96,7 @@ func TestParseCmdlineOptionDevMode(t *testing.T) {
debug = false
crashOnError = false

err := a.parseCmdlineOption(d.option)
err := parseCmdlineOption(d.option)
assert.NoError(err)

if !d.expectDevModeEnabled {
Expand All @@ -138,20 +108,9 @@ func TestParseCmdlineOptionDevMode(t *testing.T) {
}
}

func TestGetConfigEmptyFileName(t *testing.T) {
assert := assert.New(t)

a := &agentConfig{}

err := a.getConfig("")
assert.Error(err, "Should fail because command line path is empty")
}

func TestGetConfigFilePathNotExist(t *testing.T) {
assert := assert.New(t)

a := &agentConfig{}

tmpFile, err := ioutil.TempFile("", "test")
assert.NoError(err, "%v", err)

Expand All @@ -160,15 +119,17 @@ func TestGetConfigFilePathNotExist(t *testing.T) {
err = os.Remove(fileName)
assert.NoError(err, "%v", err)

err = a.getConfig(fileName)
assert.Error(err, "Should fail because command line path does not exist")
kernelCmdlineFileOld := kernelCmdlineFile
defer func() {
kernelCmdlineFile = kernelCmdlineFileOld
}()
kernelCmdlineFile = fileName
assert.Error(parseKernelCmdline())
}

func TestGetConfig(t *testing.T) {
func TestParseKernelCmdline(t *testing.T) {
assert := assert.New(t)

a := &agentConfig{}

tmpFile, err := ioutil.TempFile("", "test")
assert.NoError(err, "%v", err)
fileName := tmpFile.Name()
Expand All @@ -178,12 +139,17 @@ func TestGetConfig(t *testing.T) {

defer os.Remove(fileName)

err = a.getConfig(fileName)
assert.NoError(err, "%v", err)
kernelCmdlineFileOld := kernelCmdlineFile
defer func() {
kernelCmdlineFile = kernelCmdlineFileOld
}()
kernelCmdlineFile = fileName

assert.NoError(parseKernelCmdline())

assert.True(a.logLevel == logrus.InfoLevel,
assert.True(logLevel == logrus.InfoLevel,
"Log levels should be identical: got %+v, expecting %+v",
a.logLevel, logrus.InfoLevel)
logLevel, logrus.InfoLevel)
}

func TestParseCmdlineOptionTracing(t *testing.T) {
Expand Down Expand Up @@ -214,14 +180,17 @@ func TestParseCmdlineOptionTracing(t *testing.T) {
{traceModeFlag + "=" + traceTypeCollated + "x", false, false},
}

kernelCmdlineFileOld := kernelCmdlineFile
defer func() {
kernelCmdlineFile = kernelCmdlineFileOld
}()

for i, d := range data {
// force reset
tracing = false
collatedTrace = false
debug = false

a := &agentConfig{}

tmpFile, err := ioutil.TempFile("", "")
assert.NoError(err)

Expand All @@ -235,8 +204,8 @@ func TestParseCmdlineOptionTracing(t *testing.T) {
assert.False(collatedTrace)
assert.False(debug)

err = a.getConfig(fileName)
assert.NoError(err)
kernelCmdlineFile = fileName
assert.NoError(parseKernelCmdline())

if d.expectTraceEnabled {
assert.Truef(tracing, "test %d (%+v)", i, d)
Expand Down Expand Up @@ -296,19 +265,15 @@ func TestParseCmdlineOptionWrongOptionVsock(t *testing.T) {
t.Skip()
assert := assert.New(t)

a := &agentConfig{}

wrongOption := "use_vsockkk=true"

err := a.parseCmdlineOption(wrongOption)
err := parseCmdlineOption(wrongOption)
assert.Errorf(err, "Parsing should fail because wrong option %q", wrongOption)
}

func TestParseCmdlineOptionsVsock(t *testing.T) {
assert := assert.New(t)

a := &agentConfig{}

type testData struct {
val string
shouldErr bool
Expand All @@ -325,7 +290,7 @@ func TestParseCmdlineOptionsVsock(t *testing.T) {
commCh = unknownCh
option := useVsockFlag + "=" + d.val

err := a.parseCmdlineOption(option)
err := parseCmdlineOption(option)
if d.shouldErr {
assert.Error(err)
} else {
Expand All @@ -338,8 +303,6 @@ func TestParseCmdlineOptionsVsock(t *testing.T) {
func TestParseCmdlineOptionDebugConsole(t *testing.T) {
assert := assert.New(t)

a := &agentConfig{}

type testData struct {
option string
expectDebugConsoleEnabled bool
Expand All @@ -357,7 +320,7 @@ func TestParseCmdlineOptionDebugConsole(t *testing.T) {
for i, d := range data {
debugConsole = false

err := a.parseCmdlineOption(d.option)
err := parseCmdlineOption(d.option)
assert.NoError(err)

if !d.expectDebugConsoleEnabled {
Expand All @@ -371,8 +334,6 @@ func TestParseCmdlineOptionDebugConsole(t *testing.T) {
func TestParseCmdlineOptionDebugConsoleVPort(t *testing.T) {
assert := assert.New(t)

a := &agentConfig{}

type testData struct {
option string
expectDebugConsoleEnabled bool
Expand All @@ -395,7 +356,7 @@ func TestParseCmdlineOptionDebugConsoleVPort(t *testing.T) {
debugConsole = false
debugConsoleVSockPort = 0

err := a.parseCmdlineOption(d.option)
err := parseCmdlineOption(d.option)
if d.expectedError {
assert.Error(err)
} else {
Expand All @@ -413,8 +374,6 @@ func TestParseCmdlineOptionDebugConsoleVPort(t *testing.T) {
func TestParseCmdlineOptionHotplugTimeout(t *testing.T) {
assert := assert.New(t)

a := &agentConfig{}

type testData struct {
option string
shouldErr bool
Expand All @@ -441,7 +400,7 @@ func TestParseCmdlineOptionHotplugTimeout(t *testing.T) {
// reset the hotplug timeout
hotplugTimeout = 3 * time.Second

err := a.parseCmdlineOption(d.option)
err := parseCmdlineOption(d.option)
if d.shouldErr {
assert.Error(err)
} else {
Expand Down

0 comments on commit ddd9188

Please sign in to comment.