Skip to content

Commit c7c0177

Browse files
committed
Hide LoadSnapshot from Machine
We should not expose LoadSnapshot, since it is allowed only before boot. Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent 5615e55 commit c7c0177

File tree

4 files changed

+52
-29
lines changed

4 files changed

+52
-29
lines changed

machine.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ type Config struct {
150150
// It is possible to use a valid IPv4 link-local address (169.254.0.0/16).
151151
// If not provided, the default address (169.254.169.254) will be used.
152152
MmdsAddress net.IP
153+
154+
Snapshot SnapshotConfig
155+
}
156+
157+
func (cfg *Config) hasSnapshot() bool {
158+
return cfg.Snapshot.MemFilePath != "" || cfg.Snapshot.SnapshotPath != ""
153159
}
154160

155161
// Validate will ensure that the required fields are set and that
@@ -722,6 +728,17 @@ func (m *Machine) captureFifoToFileWithChannel(ctx context.Context, logger *log.
722728
}
723729

724730
func (m *Machine) createMachine(ctx context.Context) error {
731+
ss := m.Cfg.Snapshot
732+
if ss.SnapshotPath != "" || ss.MemFilePath != "" {
733+
_, err := m.client.LoadSnapshot(ctx, &models.SnapshotLoadParams{
734+
SnapshotPath: String(ss.SnapshotPath),
735+
MemFilePath: String(ss.MemFilePath),
736+
EnableDiffSnapshots: ss.EnableDiffSnapshots,
737+
ResumeVM: ss.ResumeVM,
738+
})
739+
return err
740+
}
741+
725742
resp, err := m.client.PutMachineConfiguration(ctx, &m.Cfg.MachineCfg)
726743
if err != nil {
727744
m.logger.Errorf("PutMachineConfiguration returned %s", resp.Error())
@@ -738,6 +755,10 @@ func (m *Machine) createMachine(ctx context.Context) error {
738755
}
739756

740757
func (m *Machine) createBootSource(ctx context.Context, imagePath, initrdPath, kernelArgs string) error {
758+
if m.Cfg.hasSnapshot() {
759+
return nil
760+
}
761+
741762
bsrc := models.BootSource{
742763
KernelImagePath: &imagePath,
743764
InitrdPath: initrdPath,
@@ -845,6 +866,10 @@ func (m *Machine) addVsock(ctx context.Context, dev VsockDevice) error {
845866
}
846867

847868
func (m *Machine) startInstance(ctx context.Context) error {
869+
if m.Cfg.hasSnapshot() {
870+
return nil
871+
}
872+
848873
action := models.InstanceActionInfoActionTypeInstanceStart
849874
info := models.InstanceActionInfo{
850875
ActionType: &action,
@@ -1096,22 +1121,6 @@ func (m *Machine) CreateSnapshot(ctx context.Context, memFilePath, snapshotPath
10961121
return nil
10971122
}
10981123

1099-
// LoadSnapshot load a snapshot
1100-
func (m *Machine) LoadSnapshot(ctx context.Context, memFilePath, snapshotPath string, opts ...LoadSnapshotOpt) error {
1101-
snapshotParams := &models.SnapshotLoadParams{
1102-
MemFilePath: String(memFilePath),
1103-
SnapshotPath: String(snapshotPath),
1104-
}
1105-
1106-
if _, err := m.client.LoadSnapshot(ctx, snapshotParams, opts...); err != nil {
1107-
m.logger.Errorf("failed to load a snapshot for VM: %v", err)
1108-
return err
1109-
}
1110-
1111-
m.logger.Debug("snapshot loaded successfully")
1112-
return nil
1113-
}
1114-
11151124
// CreateBalloon creates a balloon device if one does not exist
11161125
func (m *Machine) CreateBalloon(ctx context.Context, amountMib int64, deflateOnOom bool, statsPollingIntervals int64, opts ...PutBalloonOpt) error {
11171126
balloon := models.Balloon{

machine_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,11 @@ func TestLoadSnapshot(t *testing.T) {
17801780

17811781
// Load a snapshot
17821782
{
1783-
cfg := createValidConfig(t, socketPath+".load")
1783+
cfg := Config{
1784+
DisableValidation: true,
1785+
SocketPath: socketPath + ".load",
1786+
Snapshot: SnapshotConfig{SnapshotPath: snapPath, MemFilePath: memPath},
1787+
}
17841788
m, err := NewMachine(ctx, cfg, func(m *Machine) {
17851789
// Rewriting m.cmd partially wouldn't work since Cmd has
17861790
// some unexported members
@@ -1789,7 +1793,7 @@ func TestLoadSnapshot(t *testing.T) {
17891793
}, WithLogger(logrus.NewEntry(machineLogger)))
17901794
require.NoError(t, err)
17911795

1792-
err = m.Start(ctx, WithSnapshot(ctx, memPath, snapPath))
1796+
err = m.Start(ctx)
17931797
require.NoError(t, err)
17941798

17951799
err = m.ResumeVM(ctx)

opts.go

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

1616
import (
17-
"context"
1817
"os/exec"
1918

2019
"github.com/sirupsen/logrus"
@@ -48,13 +47,3 @@ func WithProcessRunner(cmd *exec.Cmd) Opt {
4847
machine.cmd = cmd
4948
}
5049
}
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-
}

snapshot.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package firecracker
15+
16+
type SnapshotConfig struct {
17+
MemFilePath string
18+
SnapshotPath string
19+
EnableDiffSnapshots bool
20+
ResumeVM bool
21+
}

0 commit comments

Comments
 (0)