diff --git a/jailer_test.go b/jailer_test.go index fdfc6e53..086edee6 100644 --- a/jailer_test.go +++ b/jailer_test.go @@ -320,6 +320,7 @@ func TestJail(t *testing.T) { }, } cfg := &Config{ + VMID: "vmid", JailerCfg: &c.jailerCfg, NetNS: c.netns, SocketPath: c.socketPath, diff --git a/machine.go b/machine.go index c09e370a..f4c55439 100644 --- a/machine.go +++ b/machine.go @@ -137,9 +137,9 @@ type Config struct { JailerCfg *JailerConfig // (Optional) VMID is a unique identifier for this VM. It's set to a - // random uuid if not provided by the user. It's currently used to - // set the CNI ContainerID and create a network namespace path if - // CNI configuration is provided as part of NetworkInterfaces + // random uuid if not provided by the user. It's used to set Firecracker's instance ID. + // If CNI configuration is provided as part of NetworkInterfaces, + // the VMID is used to set CNI ContainerID and create a network namespace path. VMID string // NetNS represents the path to a network namespace handle. If present, the @@ -303,6 +303,12 @@ func (m *Machine) LogLevel() string { return m.Cfg.LogLevel } +func configureBuilder(builder VMCommandBuilder, cfg Config) VMCommandBuilder { + return builder. + WithSocketPath(cfg.SocketPath). + AddArgs("--seccomp-level", cfg.SeccompLevel.String(), "--id", cfg.VMID) +} + // NewMachine initializes a new Machine instance and performs validation of the // provided Config. func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error) { @@ -310,6 +316,14 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error) exitCh: make(chan struct{}), } + if cfg.VMID == "" { + randomID, err := uuid.NewV4() + if err != nil { + return nil, errors.Wrap(err, "failed to create random ID for VMID") + } + cfg.VMID = randomID.String() + } + m.Handlers = defaultHandlers if cfg.JailerCfg != nil { @@ -319,10 +333,7 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error) } } else { m.Handlers.Validation = m.Handlers.Validation.Append(ConfigValidationHandler) - m.cmd = defaultFirecrackerVMMCommandBuilder. - WithSocketPath(cfg.SocketPath). - AddArgs("--seccomp-level", cfg.SeccompLevel.String()). - Build(ctx) + m.cmd = configureBuilder(defaultFirecrackerVMMCommandBuilder, cfg).Build(ctx) } for _, opt := range opts { @@ -339,14 +350,6 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error) m.client = NewClient(cfg.SocketPath, m.logger, false) } - if cfg.VMID == "" { - randomID, err := uuid.NewV4() - if err != nil { - return nil, errors.Wrap(err, "failed to create random ID for VMID") - } - cfg.VMID = randomID.String() - } - if cfg.ForwardSignals == nil { cfg.ForwardSignals = []os.Signal{ os.Interrupt, diff --git a/machine_test.go b/machine_test.go index 783245b1..bfa31101 100644 --- a/machine_test.go +++ b/machine_test.go @@ -420,6 +420,9 @@ func TestStartVMM(t *testing.T) { } func TestLogAndMetrics(t *testing.T) { + const logLevel = "DEBUG" + const vmID = "UserSuppliedVMID" + dir, err := ioutil.TempDir("", t.Name()) require.NoError(t, err) defer os.RemoveAll(dir) @@ -427,6 +430,7 @@ func TestLogAndMetrics(t *testing.T) { socketPath := filepath.Join(dir, "fc.sock") cfg := Config{ + VMID: vmID, SocketPath: socketPath, DisableValidation: true, KernelImagePath: getVmlinuxPath(t), @@ -438,13 +442,10 @@ func TestLogAndMetrics(t *testing.T) { }, MetricsPath: filepath.Join(dir, "fc-metrics.out"), LogPath: filepath.Join(dir, "fc.log"), - LogLevel: "Debug", + LogLevel: logLevel, } ctx := context.Background() - cmd := VMCommandBuilder{}. - WithSocketPath(cfg.SocketPath). - WithBin(getFirecrackerBinaryPath()). - Build(ctx) + cmd := configureBuilder(VMCommandBuilder{}.WithBin(getFirecrackerBinaryPath()), cfg).Build(ctx) m, err := NewMachine(ctx, cfg, WithProcessRunner(cmd), WithLogger(fctesting.NewLogEntry(t))) require.NoError(t, err) @@ -472,6 +473,10 @@ func TestLogAndMetrics(t *testing.T) { log, err := os.Stat(cfg.LogPath) require.NoError(t, err) assert.NotEqual(t, 0, log.Size()) + + content, err := ioutil.ReadFile(cfg.LogPath) + require.NoError(t, err) + assert.Contains(t, string(content), "["+vmID+":"+logLevel+"]") } func TestStartVMMOnce(t *testing.T) {