Skip to content

Commit 5294633

Browse files
committed
Removed unnecessary requirements for loading snapshots
Signed-off-by: David Son <[email protected]>
1 parent 388c2df commit 5294633

File tree

6 files changed

+95
-10
lines changed

6 files changed

+95
-10
lines changed

examples/cmd/snapshotting/example_demo.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,26 @@ func loadSnapshotSSH(ctx context.Context, socketPath, memPath, snapPath, ipToRes
299299
},
300300
}
301301

302+
driveID := "root"
303+
isRootDevice := true
304+
isReadOnly := false
305+
rootfsPath := "root-drive-with-ssh.img"
306+
302307
socketFile := fmt.Sprintf("%s.load", socketPath)
303-
cfg := createNewConfig(socketFile, withNetworkInterface(networkInterface))
308+
cfg := sdk.Config{
309+
SocketPath: socketPath + ".load",
310+
Drives: []models.Drive{
311+
{
312+
DriveID: &driveID,
313+
IsRootDevice: &isRootDevice,
314+
IsReadOnly: &isReadOnly,
315+
PathOnHost: &rootfsPath,
316+
},
317+
},
318+
NetworkInterfaces: []sdk.NetworkInterface{
319+
networkInterface,
320+
},
321+
}
304322

305323
// Use the firecracker binary
306324
cmd := sdk.VMCommandBuilder{}.WithSocketPath(socketFile).WithBin(filepath.Join(dir, "firecracker")).Build(ctx)

examples/cmd/snapshotting/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ require (
4040
gopkg.in/yaml.v2 v2.4.0 // indirect
4141
)
4242

43-
replace github.com/firecracker-microvm/firecracker-go-sdk => ../../..
43+
replace github.com/firecracker-microvm/firecracker-go-sdk => ../../..

handlers.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ const (
3737
CreateBalloonHandlerName = "fcinit.CreateBalloon"
3838
LoadSnapshotHandlerName = "fcinit.LoadSnapshot"
3939

40-
ValidateCfgHandlerName = "validate.Cfg"
41-
ValidateJailerCfgHandlerName = "validate.JailerCfg"
42-
ValidateNetworkCfgHandlerName = "validate.NetworkCfg"
40+
ValidateCfgHandlerName = "validate.Cfg"
41+
ValidateJailerCfgHandlerName = "validate.JailerCfg"
42+
ValidateNetworkCfgHandlerName = "validate.NetworkCfg"
43+
ValidateLoadSnapshotCfgHandlerName = "validate.LoadSnapshotCfg"
4344
)
4445

4546
// HandlersAdapter is an interface used to modify a given set of handlers.
@@ -57,6 +58,16 @@ var ConfigValidationHandler = Handler{
5758
},
5859
}
5960

61+
// LoadSnapshotConfigValidationHandler is used to validate that required
62+
// fields are present.
63+
var LoadSnapshotConfigValidationHandler = Handler{
64+
Name: ValidateLoadSnapshotCfgHandlerName,
65+
Fn: func(ctx context.Context, m *Machine) error {
66+
// ensure that the configuration is valid for the FcInit handlers.
67+
return m.Cfg.ValidateLoadSnapshot()
68+
},
69+
}
70+
6071
// JailerConfigValidationHandler is used to validate that required fields are
6172
// present.
6273
var JailerConfigValidationHandler = Handler{
@@ -317,6 +328,11 @@ var defaultValidationHandlerList = HandlerList{}.Append(
317328
NetworkConfigValidationHandler,
318329
)
319330

331+
var loadSnapshotValidationHandlerList = HandlerList{}.Append(
332+
NetworkConfigValidationHandler,
333+
LoadSnapshotConfigValidationHandler,
334+
)
335+
320336
var defaultHandlers = Handlers{
321337
Validation: defaultValidationHandlerList,
322338
FcInit: defaultFcInitHandlerList,

machine.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,33 @@ func (cfg *Config) Validate() error {
204204
return nil
205205
}
206206

207+
func (cfg *Config) ValidateLoadSnapshot() error {
208+
if cfg.DisableValidation {
209+
return nil
210+
}
211+
212+
for _, drive := range cfg.Drives {
213+
rootPath := StringValue(drive.PathOnHost)
214+
if _, err := os.Stat(rootPath); err != nil {
215+
return fmt.Errorf("failed to stat drive path, %q: %v", rootPath, err)
216+
}
217+
}
218+
219+
if _, err := os.Stat(cfg.SocketPath); err == nil {
220+
return fmt.Errorf("socket %s already exists", cfg.SocketPath)
221+
}
222+
223+
if _, err := os.Stat(cfg.Snapshot.MemFilePath); err != nil {
224+
return err
225+
}
226+
227+
if _, err := os.Stat(cfg.Snapshot.SnapshotPath); err != nil {
228+
return err
229+
}
230+
231+
return nil
232+
}
233+
207234
func (cfg *Config) ValidateNetwork() error {
208235
if cfg.DisableValidation {
209236
return nil

machine_test.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,19 @@ func TestLoadSnapshot(t *testing.T) {
19391939
},
19401940

19411941
loadSnapshot: func(ctx context.Context, machineLogger *logrus.Logger, socketPath, memPath, snapPath string) {
1942-
cfg := createValidConfig(t, socketPath+".load")
1942+
// Note that many fields are not necessary when loading a snapshot
1943+
cfg := Config{
1944+
SocketPath: socketPath + ".load",
1945+
Drives: []models.Drive{
1946+
{
1947+
DriveID: String("root"),
1948+
IsRootDevice: Bool(true),
1949+
IsReadOnly: Bool(true),
1950+
PathOnHost: String(testRootfs),
1951+
},
1952+
},
1953+
}
1954+
19431955
m, err := NewMachine(ctx, cfg, func(m *Machine) {
19441956
// Rewriting m.cmd partially wouldn't work since Cmd has
19451957
// some unexported members
@@ -2078,10 +2090,21 @@ func TestLoadSnapshot(t *testing.T) {
20782090
VMIfName: "eth0",
20792091
},
20802092
}
2081-
cfg := createValidConfig(t, fmt.Sprintf("%s.load", socketPath),
2082-
withRootDrive(rootfsPath),
2083-
withNetworkInterface(networkInterface),
2084-
)
2093+
2094+
cfg := Config{
2095+
SocketPath: socketPath + ".load",
2096+
Drives: []models.Drive{
2097+
{
2098+
DriveID: String("root"),
2099+
IsRootDevice: Bool(true),
2100+
IsReadOnly: Bool(true),
2101+
PathOnHost: String(rootfsPath),
2102+
},
2103+
},
2104+
NetworkInterfaces: []NetworkInterface{
2105+
networkInterface,
2106+
},
2107+
}
20852108

20862109
cmd := VMCommandBuilder{}.WithSocketPath(fmt.Sprintf("%s.load", socketPath)).WithBin(getFirecrackerBinaryPath()).Build(ctx)
20872110

opts.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func WithSnapshot(memFilePath, snapshotPath string, opts ...WithSnapshotOpt) Opt
6262
opt(&m.Cfg.Snapshot)
6363
}
6464

65+
m.Handlers.Validation = loadSnapshotValidationHandlerList
6566
m.Handlers.FcInit = loadSnapshotHandlerList
6667
}
6768
}

0 commit comments

Comments
 (0)