Skip to content

Commit 7b904b1

Browse files
committed
Adjust MemoryBackend API and add test
Signed-off-by: Brandon Duffany <[email protected]>
1 parent 62f8e17 commit 7b904b1

File tree

4 files changed

+78
-14
lines changed

4 files changed

+78
-14
lines changed

machine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ type Config struct {
172172
}
173173

174174
func (cfg *Config) hasSnapshot() bool {
175-
return cfg.Snapshot.GetMemFilePath() != "" || cfg.Snapshot.SnapshotPath != ""
175+
return cfg.Snapshot.GetMemBackendPath() != "" || cfg.Snapshot.SnapshotPath != ""
176176
}
177177

178178
// Validate will ensure that the required fields are set and that
@@ -235,7 +235,7 @@ func (cfg *Config) ValidateLoadSnapshot() error {
235235
return fmt.Errorf("socket %s already exists", cfg.SocketPath)
236236
}
237237

238-
if _, err := os.Stat(cfg.Snapshot.GetMemFilePath()); err != nil {
238+
if _, err := os.Stat(cfg.Snapshot.GetMemBackendPath()); err != nil {
239239
return err
240240
}
241241

machine_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,6 +2125,62 @@ func TestLoadSnapshot(t *testing.T) {
21252125
require.NoError(t, err)
21262126
},
21272127
},
2128+
{
2129+
name: "TestLoadSnapshotWithMemoryBackend",
2130+
createSnapshot: func(ctx context.Context, machineLogger *logrus.Logger, socketPath, memPath, snapPath string) {
2131+
// Create a snapshot
2132+
cfg := createValidConfig(t, socketPath+".create")
2133+
m, err := NewMachine(ctx, cfg, func(m *Machine) {
2134+
// Rewriting m.cmd partially wouldn't work since Cmd has
2135+
// some unexported members
2136+
args := m.cmd.Args[1:]
2137+
m.cmd = exec.Command(getFirecrackerBinaryPath(), args...)
2138+
}, WithLogger(logrus.NewEntry(machineLogger)))
2139+
require.NoError(t, err)
2140+
2141+
err = m.Start(ctx)
2142+
require.NoError(t, err)
2143+
2144+
err = m.PauseVM(ctx)
2145+
require.NoError(t, err)
2146+
2147+
err = m.CreateSnapshot(ctx, memPath, snapPath)
2148+
require.NoError(t, err)
2149+
2150+
err = m.StopVMM()
2151+
require.NoError(t, err)
2152+
},
2153+
2154+
loadSnapshot: func(ctx context.Context, machineLogger *logrus.Logger, socketPath, memPath, snapPath string) {
2155+
// Note that many fields are not necessary when loading a snapshot
2156+
cfg := Config{
2157+
SocketPath: socketPath + ".load",
2158+
Drives: []models.Drive{
2159+
{
2160+
DriveID: String("root"),
2161+
IsRootDevice: Bool(true),
2162+
IsReadOnly: Bool(true),
2163+
PathOnHost: String(testRootfs),
2164+
},
2165+
},
2166+
}
2167+
2168+
m, err := NewMachine(ctx, cfg, func(m *Machine) {
2169+
// Rewriting m.cmd partially wouldn't work since Cmd has
2170+
// some unexported members
2171+
args := m.cmd.Args[1:]
2172+
m.cmd = exec.Command(getFirecrackerBinaryPath(), args...)
2173+
}, WithLogger(logrus.NewEntry(machineLogger)), WithSnapshot("", snapPath, WithMemoryBackend("File", memPath)))
2174+
require.NoError(t, err)
2175+
require.Equal(t, m.Cfg.Snapshot.ResumeVM, true)
2176+
2177+
err = m.Start(ctx)
2178+
require.NoError(t, err)
2179+
2180+
err = m.StopVMM()
2181+
require.NoError(t, err)
2182+
},
2183+
},
21282184
{
21292185
name: "TestLoadSnapshot without create",
21302186
createSnapshot: func(ctx context.Context, machineLogger *logrus.Logger, socketPath, memPath, snapPath string) {

opts.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ func WithProcessRunner(cmd *exec.Cmd) Opt {
5353
// to be passed to LoadSnapshot
5454
type WithSnapshotOpt func(*SnapshotConfig)
5555

56-
// WithSnapshot will allow for the machine to start using a given snapshot. A
57-
// UFFD socket file path may be used as memFilePath if a MemoryBackendType opt
58-
// is passed setting the backend type to models.MemoryBackendBackendTypeUffd.
56+
// WithSnapshot will allow for the machine to start using a given snapshot.
57+
//
58+
// If using the UFFD memory backend, the memFilePath may be empty (it is
59+
// ignored), and instead the UFFD socket should be specified using
60+
// MemoryBackendType, as in the following example:
61+
//
62+
// WithSnapshot(
63+
// "", snapshotPath,
64+
// WithMemoryBackend(models.MemoryBackendBackendTypeUffd, "uffd.sock"))
5965
func WithSnapshot(memFilePath, snapshotPath string, opts ...WithSnapshotOpt) Opt {
6066
return func(m *Machine) {
6167
m.Cfg.Snapshot.MemFilePath = memFilePath
@@ -70,16 +76,18 @@ func WithSnapshot(memFilePath, snapshotPath string, opts ...WithSnapshotOpt) Opt
7076
}
7177
}
7278

73-
// MemoryBackendType sets the memory backend type to the given value.
74-
func MemoryBackendType(typ string) WithSnapshotOpt {
79+
// WithMemoryBackend sets the memory backend to the given type, using the given
80+
// backing file path (a regular file for "File" type, or a UFFD socket path for
81+
// "Uffd" type).
82+
//
83+
// Note that if MemFilePath is already configured for the snapshot config, it
84+
// will be ignored, and the backendPath specified here will be used instead.
85+
func WithMemoryBackend(backendType, backendPath string) WithSnapshotOpt {
7586
return func(cfg *SnapshotConfig) {
76-
if cfg.MemBackend == nil {
77-
cfg.MemBackend = &models.MemoryBackend{
78-
BackendPath: String(cfg.MemFilePath),
79-
}
87+
cfg.MemBackend = &models.MemoryBackend{
88+
BackendType: String(backendType),
89+
BackendPath: String(backendPath),
8090
}
81-
cfg.MemBackend.BackendType = String(typ)
82-
// Clear MemFilePath since only one of MemFilePath or MemBackend is allowed.
8391
cfg.MemFilePath = ""
8492
}
8593
}

snapshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type SnapshotConfig struct {
2323
ResumeVM bool
2424
}
2525

26-
func (cfg *SnapshotConfig) GetMemFilePath() string {
26+
func (cfg *SnapshotConfig) GetMemBackendPath() string {
2727
if cfg.MemBackend != nil && cfg.MemBackend.BackendPath != nil {
2828
return *cfg.MemBackend.BackendPath
2929
}

0 commit comments

Comments
 (0)