@@ -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
@@ -381,7 +388,7 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
381
388
// handlers succeed, then this will start the VMM instance.
382
389
// Start may only be called once per Machine. Subsequent calls will return
383
390
// ErrAlreadyStarted.
384
- func (m * Machine ) Start (ctx context.Context ) error {
391
+ func (m * Machine ) Start (ctx context.Context , opts ... StartOpt ) error {
385
392
m .logger .Debug ("Called Machine.Start()" )
386
393
alreadyStarted := true
387
394
m .startOnce .Do (func () {
@@ -402,6 +409,10 @@ func (m *Machine) Start(ctx context.Context) error {
402
409
}
403
410
}()
404
411
412
+ for _ , opt := range opts {
413
+ opt (m )
414
+ }
415
+
405
416
err = m .Handlers .Run (ctx , m )
406
417
if err != nil {
407
418
return err
@@ -854,6 +865,10 @@ func (m *Machine) addVsock(ctx context.Context, dev VsockDevice) error {
854
865
}
855
866
856
867
func (m * Machine ) startInstance (ctx context.Context ) error {
868
+ if m .Cfg .hasSnapshot () {
869
+ return nil
870
+ }
871
+
857
872
action := models .InstanceActionInfoActionTypeInstanceStart
858
873
info := models.InstanceActionInfo {
859
874
ActionType : & action ,
@@ -1105,6 +1120,23 @@ func (m *Machine) CreateSnapshot(ctx context.Context, memFilePath, snapshotPath
1105
1120
return nil
1106
1121
}
1107
1122
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
+
1108
1140
// CreateBalloon creates a balloon device if one does not exist
1109
1141
func (m * Machine ) CreateBalloon (ctx context.Context , amountMib int64 , deflateOnOom bool , statsPollingIntervals int64 , opts ... PutBalloonOpt ) error {
1110
1142
balloon := models.Balloon {
0 commit comments