@@ -9,19 +9,43 @@ import (
99 "strings"
1010 "time"
1111
12+ hostagentclient "github.com/lima-vm/lima/pkg/hostagent/api/client"
1213 hostagentevents "github.com/lima-vm/lima/pkg/hostagent/events"
14+ "github.com/lima-vm/lima/pkg/limayaml"
1315 "github.com/lima-vm/lima/pkg/osutil"
1416 "github.com/lima-vm/lima/pkg/store"
1517 "github.com/lima-vm/lima/pkg/store/filenames"
1618 "github.com/sirupsen/logrus"
1719)
1820
19- func StopGracefully (inst * store.Instance ) error {
20- // TODO: support store.StatusSuspended
21- if inst .Status != store .StatusRunning {
21+ func StopGracefully (inst * store.Instance , saveOnStop bool ) error {
22+ if inst .Status == store .StatusSaved {
23+ if saveOnStop {
24+ return fmt .Errorf ("instance %q is already saved" , inst .Name )
25+ }
26+ return fmt .Errorf ("instance %q is saved. To stop, run `limactl start %s && limactl stop %s`" , inst .Name , inst .Name , inst .Name )
27+ } else if inst .Status != store .StatusRunning {
2228 return fmt .Errorf ("expected status %q, got %q (maybe use `limactl stop -f`?)" , store .StatusRunning , inst .Status )
2329 }
2430
31+ if inst .VMType == limayaml .VZ {
32+ haSock := filepath .Join (inst .Dir , filenames .HostAgentSock )
33+ haClient , err := hostagentclient .NewHostAgentClient (haSock )
34+ if err != nil {
35+ logrus .WithError (err ).Error ("Failed to create a host agent client" )
36+ }
37+ ctx , cancel := context .WithTimeout (context .TODO (), 3 * time .Second )
38+ defer cancel ()
39+ disableSaveOnStopConfig := struct {
40+ SaveOnStop bool `json:"saveOnStop"`
41+ }{SaveOnStop : saveOnStop }
42+ _ , err = haClient .DriverConfig (ctx , disableSaveOnStopConfig )
43+ if err != nil {
44+ return fmt .Errorf ("failed to disable saveOnStop: %w" , err )
45+ }
46+ } else if saveOnStop {
47+ return fmt .Errorf ("save is not supported for %q" , inst .VMType )
48+ }
2549 begin := time .Now () // used for logrus propagation
2650 logrus .Infof ("Sending SIGINT to hostagent process %d" , inst .HostAgentPID )
2751 if err := osutil .SysKill (inst .HostAgentPID , osutil .SigInt ); err != nil {
0 commit comments