@@ -151,6 +151,13 @@ type Config struct {
151
151
// It is possible to use a valid IPv4 link-local address (169.254.0.0/16).
152
152
// If not provided, the default address (169.254.169.254) will be used.
153
153
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 != ""
154
161
}
155
162
156
163
// Validate will ensure that the required fields are set and that
@@ -375,7 +382,7 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
375
382
// handlers succeed, then this will start the VMM instance.
376
383
// Start may only be called once per Machine. Subsequent calls will return
377
384
// ErrAlreadyStarted.
378
- func (m * Machine ) Start (ctx context.Context ) error {
385
+ func (m * Machine ) Start (ctx context.Context , opts ... StartOpt ) error {
379
386
m .logger .Debug ("Called Machine.Start()" )
380
387
alreadyStarted := true
381
388
m .startOnce .Do (func () {
@@ -396,6 +403,10 @@ func (m *Machine) Start(ctx context.Context) error {
396
403
}
397
404
}()
398
405
406
+ for _ , opt := range opts {
407
+ opt (m )
408
+ }
409
+
399
410
err = m .Handlers .Run (ctx , m )
400
411
if err != nil {
401
412
return err
@@ -840,6 +851,10 @@ func (m *Machine) addVsock(ctx context.Context, dev VsockDevice) error {
840
851
}
841
852
842
853
func (m * Machine ) startInstance (ctx context.Context ) error {
854
+ if m .Cfg .hasSnapshot () {
855
+ return nil
856
+ }
857
+
843
858
action := models .InstanceActionInfoActionTypeInstanceStart
844
859
info := models.InstanceActionInfo {
845
860
ActionType : & action ,
@@ -1091,6 +1106,23 @@ func (m *Machine) CreateSnapshot(ctx context.Context, memFilePath, snapshotPath
1091
1106
return nil
1092
1107
}
1093
1108
1109
+ // loadSnapshot loads a snapshot of the VM
1110
+ func (m * Machine ) loadSnapshot (ctx context.Context , snapshot * SnapshotConfig ) error {
1111
+ snapshotParams := & models.SnapshotLoadParams {
1112
+ MemFilePath : & snapshot .MemFilePath ,
1113
+ SnapshotPath : & snapshot .SnapshotPath ,
1114
+ EnableDiffSnapshots : snapshot .EnableDiffSnapshots ,
1115
+ ResumeVM : snapshot .ResumeVM ,
1116
+ }
1117
+
1118
+ if _ , err := m .client .LoadSnapshot (ctx , snapshotParams ); err != nil {
1119
+ return fmt .Errorf ("failed to load a snapshot for VM: %v" , err )
1120
+ }
1121
+
1122
+ m .logger .Debug ("snapshot loaded successfully" )
1123
+ return nil
1124
+ }
1125
+
1094
1126
// CreateBalloon creates a balloon device if one does not exist
1095
1127
func (m * Machine ) CreateBalloon (ctx context.Context , amountMib int64 , deflateOnOom bool , statsPollingIntervals int64 , opts ... PutBalloonOpt ) error {
1096
1128
balloon := models.Balloon {
0 commit comments