Skip to content

Commit

Permalink
Merge pull request #433 from sondavidb/snapshot-validation
Browse files Browse the repository at this point in the history
Removed unnecessary requirements for loading snapshots
  • Loading branch information
sondavidb authored Aug 12, 2022
2 parents 388c2df + 5294633 commit 490e336
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 10 deletions.
20 changes: 19 additions & 1 deletion examples/cmd/snapshotting/example_demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,26 @@ func loadSnapshotSSH(ctx context.Context, socketPath, memPath, snapPath, ipToRes
},
}

driveID := "root"
isRootDevice := true
isReadOnly := false
rootfsPath := "root-drive-with-ssh.img"

socketFile := fmt.Sprintf("%s.load", socketPath)
cfg := createNewConfig(socketFile, withNetworkInterface(networkInterface))
cfg := sdk.Config{
SocketPath: socketPath + ".load",
Drives: []models.Drive{
{
DriveID: &driveID,
IsRootDevice: &isRootDevice,
IsReadOnly: &isReadOnly,
PathOnHost: &rootfsPath,
},
},
NetworkInterfaces: []sdk.NetworkInterface{
networkInterface,
},
}

// Use the firecracker binary
cmd := sdk.VMCommandBuilder{}.WithSocketPath(socketFile).WithBin(filepath.Join(dir, "firecracker")).Build(ctx)
Expand Down
2 changes: 1 addition & 1 deletion examples/cmd/snapshotting/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
)

replace github.com/firecracker-microvm/firecracker-go-sdk => ../../..
replace github.com/firecracker-microvm/firecracker-go-sdk => ../../..
22 changes: 19 additions & 3 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ const (
CreateBalloonHandlerName = "fcinit.CreateBalloon"
LoadSnapshotHandlerName = "fcinit.LoadSnapshot"

ValidateCfgHandlerName = "validate.Cfg"
ValidateJailerCfgHandlerName = "validate.JailerCfg"
ValidateNetworkCfgHandlerName = "validate.NetworkCfg"
ValidateCfgHandlerName = "validate.Cfg"
ValidateJailerCfgHandlerName = "validate.JailerCfg"
ValidateNetworkCfgHandlerName = "validate.NetworkCfg"
ValidateLoadSnapshotCfgHandlerName = "validate.LoadSnapshotCfg"
)

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

// LoadSnapshotConfigValidationHandler is used to validate that required
// fields are present.
var LoadSnapshotConfigValidationHandler = Handler{
Name: ValidateLoadSnapshotCfgHandlerName,
Fn: func(ctx context.Context, m *Machine) error {
// ensure that the configuration is valid for the FcInit handlers.
return m.Cfg.ValidateLoadSnapshot()
},
}

// JailerConfigValidationHandler is used to validate that required fields are
// present.
var JailerConfigValidationHandler = Handler{
Expand Down Expand Up @@ -317,6 +328,11 @@ var defaultValidationHandlerList = HandlerList{}.Append(
NetworkConfigValidationHandler,
)

var loadSnapshotValidationHandlerList = HandlerList{}.Append(
NetworkConfigValidationHandler,
LoadSnapshotConfigValidationHandler,
)

var defaultHandlers = Handlers{
Validation: defaultValidationHandlerList,
FcInit: defaultFcInitHandlerList,
Expand Down
27 changes: 27 additions & 0 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,33 @@ func (cfg *Config) Validate() error {
return nil
}

func (cfg *Config) ValidateLoadSnapshot() error {
if cfg.DisableValidation {
return nil
}

for _, drive := range cfg.Drives {
rootPath := StringValue(drive.PathOnHost)
if _, err := os.Stat(rootPath); err != nil {
return fmt.Errorf("failed to stat drive path, %q: %v", rootPath, err)
}
}

if _, err := os.Stat(cfg.SocketPath); err == nil {
return fmt.Errorf("socket %s already exists", cfg.SocketPath)
}

if _, err := os.Stat(cfg.Snapshot.MemFilePath); err != nil {
return err
}

if _, err := os.Stat(cfg.Snapshot.SnapshotPath); err != nil {
return err
}

return nil
}

func (cfg *Config) ValidateNetwork() error {
if cfg.DisableValidation {
return nil
Expand Down
33 changes: 28 additions & 5 deletions machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1939,7 +1939,19 @@ func TestLoadSnapshot(t *testing.T) {
},

loadSnapshot: func(ctx context.Context, machineLogger *logrus.Logger, socketPath, memPath, snapPath string) {
cfg := createValidConfig(t, socketPath+".load")
// Note that many fields are not necessary when loading a snapshot
cfg := Config{
SocketPath: socketPath + ".load",
Drives: []models.Drive{
{
DriveID: String("root"),
IsRootDevice: Bool(true),
IsReadOnly: Bool(true),
PathOnHost: String(testRootfs),
},
},
}

m, err := NewMachine(ctx, cfg, func(m *Machine) {
// Rewriting m.cmd partially wouldn't work since Cmd has
// some unexported members
Expand Down Expand Up @@ -2078,10 +2090,21 @@ func TestLoadSnapshot(t *testing.T) {
VMIfName: "eth0",
},
}
cfg := createValidConfig(t, fmt.Sprintf("%s.load", socketPath),
withRootDrive(rootfsPath),
withNetworkInterface(networkInterface),
)

cfg := Config{
SocketPath: socketPath + ".load",
Drives: []models.Drive{
{
DriveID: String("root"),
IsRootDevice: Bool(true),
IsReadOnly: Bool(true),
PathOnHost: String(rootfsPath),
},
},
NetworkInterfaces: []NetworkInterface{
networkInterface,
},
}

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

Expand Down
1 change: 1 addition & 0 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func WithSnapshot(memFilePath, snapshotPath string, opts ...WithSnapshotOpt) Opt
opt(&m.Cfg.Snapshot)
}

m.Handlers.Validation = loadSnapshotValidationHandlerList
m.Handlers.FcInit = loadSnapshotHandlerList
}
}

0 comments on commit 490e336

Please sign in to comment.