Skip to content

Commit 2b31035

Browse files
Gabriel Ionescugbionescu
authored andcommitted
extend machine start to support snapshots
By adding functional options to Start(), we can call Start() with the WithSnapshot parameter to load a snapshot before starting. Signed-off-by: Gabriel Ionescu <gbi@amazon.com> Signed-off-by: Gabriel Ionescu <plp.github@gmail.com>
1 parent 4af1b25 commit 2b31035

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

machine.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
385385
// handlers succeed, then this will start the VMM instance.
386386
// Start may only be called once per Machine. Subsequent calls will return
387387
// ErrAlreadyStarted.
388-
func (m *Machine) Start(ctx context.Context) error {
388+
func (m *Machine) Start(ctx context.Context, opts ...Opt) error {
389389
m.logger.Debug("Called Machine.Start()")
390390
alreadyStarted := true
391391
m.startOnce.Do(func() {
@@ -406,6 +406,10 @@ func (m *Machine) Start(ctx context.Context) error {
406406
}
407407
}()
408408

409+
for _, opt := range opts {
410+
opt(m)
411+
}
412+
409413
err = m.Handlers.Run(ctx, m)
410414
if err != nil {
411415
return err

machineiface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var _ MachineIface = (*Machine)(nil)
2323
// MachineIface can be used for mocking and testing of the Machine. The Machine
2424
// is subject to change, meaning this interface would change.
2525
type MachineIface interface {
26-
Start(context.Context) error
26+
Start(context.Context, ...Opt) error
2727
StopVMM() error
2828
Shutdown(context.Context) error
2929
Wait(context.Context) error

opts.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package firecracker
1515

1616
import (
17+
"context"
1718
"os/exec"
1819

1920
"github.com/sirupsen/logrus"
@@ -47,3 +48,13 @@ func WithProcessRunner(cmd *exec.Cmd) Opt {
4748
machine.cmd = cmd
4849
}
4950
}
51+
52+
// WithSnapshot will allow for the machine to start using a given snapshot.
53+
func WithSnapshot(ctx context.Context, memFilePath, snapshotPath string) Opt {
54+
return func(machine *Machine) {
55+
err := machine.LoadSnapshot(ctx, memFilePath, snapshotPath)
56+
if err != nil {
57+
machine.logger.Errorf("LoadSnapshot failed with %s", err)
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)