@@ -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
@@ -375,7 +382,7 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
375382// handlers succeed, then this will start the VMM instance.
376383// Start may only be called once per Machine. Subsequent calls will return
377384// ErrAlreadyStarted.
378- func (m * Machine ) Start (ctx context.Context ) error {
385+ func (m * Machine ) Start (ctx context.Context , opts ... StartOpt ) error {
379386 m .logger .Debug ("Called Machine.Start()" )
380387 alreadyStarted := true
381388 m .startOnce .Do (func () {
@@ -396,6 +403,10 @@ func (m *Machine) Start(ctx context.Context) error {
396403 }
397404 }()
398405
406+ for _ , opt := range opts {
407+ opt (m )
408+ }
409+
399410 err = m .Handlers .Run (ctx , m )
400411 if err != nil {
401412 return err
@@ -840,6 +851,10 @@ func (m *Machine) addVsock(ctx context.Context, dev VsockDevice) error {
840851}
841852
842853func (m * Machine ) startInstance (ctx context.Context ) error {
854+ if m .Cfg .hasSnapshot () {
855+ return nil
856+ }
857+
843858 action := models .InstanceActionInfoActionTypeInstanceStart
844859 info := models.InstanceActionInfo {
845860 ActionType : & action ,
@@ -1091,6 +1106,23 @@ func (m *Machine) CreateSnapshot(ctx context.Context, memFilePath, snapshotPath
10911106 return nil
10921107}
10931108
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+
10941126// CreateBalloon creates a balloon device if one does not exist
10951127func (m * Machine ) CreateBalloon (ctx context.Context , amountMib int64 , deflateOnOom bool , statsPollingIntervals int64 , opts ... PutBalloonOpt ) error {
10961128 balloon := models.Balloon {
0 commit comments