@@ -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
@@ -826,6 +837,10 @@ func (m *Machine) UpdateGuestNetworkInterfaceRateLimit(ctx context.Context, ifac
826
837
827
838
// attachDrive attaches a secondary block device
828
839
func (m * Machine ) attachDrive (ctx context.Context , dev models.Drive ) error {
840
+ if m .Cfg .hasSnapshot () {
841
+ return nil
842
+ }
843
+
829
844
hostPath := StringValue (dev .PathOnHost )
830
845
m .logger .Infof ("Attaching drive %s, slot %s, root %t." , hostPath , StringValue (dev .DriveID ), BoolValue (dev .IsRootDevice ))
831
846
respNoContent , err := m .client .PutGuestDriveByID (ctx , StringValue (dev .DriveID ), & dev )
@@ -854,6 +869,10 @@ func (m *Machine) addVsock(ctx context.Context, dev VsockDevice) error {
854
869
}
855
870
856
871
func (m * Machine ) startInstance (ctx context.Context ) error {
872
+ if m .Cfg .hasSnapshot () {
873
+ return nil
874
+ }
875
+
857
876
action := models .InstanceActionInfoActionTypeInstanceStart
858
877
info := models.InstanceActionInfo {
859
878
ActionType : & action ,
@@ -1105,6 +1124,21 @@ func (m *Machine) CreateSnapshot(ctx context.Context, memFilePath, snapshotPath
1105
1124
return nil
1106
1125
}
1107
1126
1127
+ // loadSnapshot loads a snapshot of the VM
1128
+ func (m * Machine ) loadSnapshot (ctx context.Context , memFilePath , snapshotPath string , opts ... LoadSnapshotOpt ) error {
1129
+ snapshotParams := & models.SnapshotLoadParams {
1130
+ MemFilePath : String (memFilePath ),
1131
+ SnapshotPath : String (snapshotPath ),
1132
+ }
1133
+
1134
+ if _ , err := m .client .LoadSnapshot (ctx , snapshotParams , opts ... ); err != nil {
1135
+ return fmt .Errorf ("failed to load a snapshot for VM: %v" , err )
1136
+ }
1137
+
1138
+ m .logger .Debug ("snapshot loaded successfully" )
1139
+ return nil
1140
+ }
1141
+
1108
1142
// CreateBalloon creates a balloon device if one does not exist
1109
1143
func (m * Machine ) CreateBalloon (ctx context.Context , amountMib int64 , deflateOnOom bool , statsPollingIntervals int64 , opts ... PutBalloonOpt ) error {
1110
1144
balloon := models.Balloon {
0 commit comments