@@ -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 
@@ -826,6 +837,10 @@ func (m *Machine) UpdateGuestNetworkInterfaceRateLimit(ctx context.Context, ifac
826837
827838// attachDrive attaches a secondary block device 
828839func  (m  * Machine ) attachDrive (ctx  context.Context , dev  models.Drive ) error  {
840+ 	if  m .Cfg .hasSnapshot () {
841+ 		return  nil 
842+ 	}
843+ 
829844	hostPath  :=  StringValue (dev .PathOnHost )
830845	m .logger .Infof ("Attaching drive %s, slot %s, root %t." , hostPath , StringValue (dev .DriveID ), BoolValue (dev .IsRootDevice ))
831846	respNoContent , err  :=  m .client .PutGuestDriveByID (ctx , StringValue (dev .DriveID ), & dev )
@@ -854,6 +869,10 @@ func (m *Machine) addVsock(ctx context.Context, dev VsockDevice) error {
854869}
855870
856871func  (m  * Machine ) startInstance (ctx  context.Context ) error  {
872+ 	if  m .Cfg .hasSnapshot () {
873+ 		return  nil 
874+ 	}
875+ 
857876	action  :=  models .InstanceActionInfoActionTypeInstanceStart 
858877	info  :=  models.InstanceActionInfo {
859878		ActionType : & action ,
@@ -1105,6 +1124,21 @@ func (m *Machine) CreateSnapshot(ctx context.Context, memFilePath, snapshotPath
11051124	return  nil 
11061125}
11071126
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+ 
11081142// CreateBalloon creates a balloon device if one does not exist 
11091143func  (m  * Machine ) CreateBalloon (ctx  context.Context , amountMib  int64 , deflateOnOom  bool , statsPollingIntervals  int64 , opts  ... PutBalloonOpt ) error  {
11101144	balloon  :=  models.Balloon {
0 commit comments