@@ -151,6 +151,13 @@ type Config struct {
151151 // It is possible to use a valid IPv4 link-local address (169.254.0.0/16).
152152 // If not provided, the default address (169.254.169.254) will be used.
153153 MmdsAddress net.IP
154+
155+ // Configuration for snapshot loading
156+ Snapshot SnapshotConfig
157+ }
158+
159+ func (cfg * Config ) hasSnapshot () bool {
160+ return cfg .Snapshot .MemFilePath != "" || cfg .Snapshot .SnapshotPath != ""
154161}
155162
156163// Validate will ensure that the required fields are set and that
@@ -381,7 +388,7 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
381388// handlers succeed, then this will start the VMM instance.
382389// Start may only be called once per Machine. Subsequent calls will return
383390// ErrAlreadyStarted.
384- func (m * Machine ) Start (ctx context.Context ) error {
391+ func (m * Machine ) Start (ctx context.Context , opts ... StartOpt ) error {
385392 m .logger .Debug ("Called Machine.Start()" )
386393 alreadyStarted := true
387394 m .startOnce .Do (func () {
@@ -402,6 +409,10 @@ func (m *Machine) Start(ctx context.Context) error {
402409 }
403410 }()
404411
412+ for _ , opt := range opts {
413+ opt (m )
414+ }
415+
405416 err = m .Handlers .Run (ctx , m )
406417 if err != nil {
407418 return err
@@ -854,6 +865,10 @@ func (m *Machine) addVsock(ctx context.Context, dev VsockDevice) error {
854865}
855866
856867func (m * Machine ) startInstance (ctx context.Context ) error {
868+ if m .Cfg .hasSnapshot () {
869+ return nil
870+ }
871+
857872 action := models .InstanceActionInfoActionTypeInstanceStart
858873 info := models.InstanceActionInfo {
859874 ActionType : & action ,
@@ -1105,6 +1120,23 @@ func (m *Machine) CreateSnapshot(ctx context.Context, memFilePath, snapshotPath
11051120 return nil
11061121}
11071122
1123+ // loadSnapshot loads a snapshot of the VM
1124+ func (m * Machine ) loadSnapshot (ctx context.Context , snapshot * SnapshotConfig ) error {
1125+ snapshotParams := & models.SnapshotLoadParams {
1126+ MemFilePath : & snapshot .MemFilePath ,
1127+ SnapshotPath : & snapshot .SnapshotPath ,
1128+ EnableDiffSnapshots : snapshot .EnableDiffSnapshots ,
1129+ ResumeVM : snapshot .ResumeVM ,
1130+ }
1131+
1132+ if _ , err := m .client .LoadSnapshot (ctx , snapshotParams ); err != nil {
1133+ return fmt .Errorf ("failed to load a snapshot for VM: %v" , err )
1134+ }
1135+
1136+ m .logger .Debug ("snapshot loaded successfully" )
1137+ return nil
1138+ }
1139+
11081140// CreateBalloon creates a balloon device if one does not exist
11091141func (m * Machine ) CreateBalloon (ctx context.Context , amountMib int64 , deflateOnOom bool , statsPollingIntervals int64 , opts ... PutBalloonOpt ) error {
11101142 balloon := models.Balloon {
0 commit comments