| 1 | |
| 2 | |
| 3 | |
| 4 | package instance |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/sirupsen/logrus" |
| 16 | |
| 17 | "github.com/lima-vm/lima/v2/pkg/autostart" |
| 18 | "github.com/lima-vm/lima/v2/pkg/driver/external/server" |
| 19 | hostagentevents "github.com/lima-vm/lima/v2/pkg/hostagent/events" |
| 20 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 21 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 22 | "github.com/lima-vm/lima/v2/pkg/osutil" |
| 23 | "github.com/lima-vm/lima/v2/pkg/store" |
| 24 | ) |
| 25 | |
| 26 | func StopGracefully(ctx context.Context, inst *limatype.Instance, isRestart bool) error { |
| 27 | if inst.Status != limatype.StatusRunning { |
| 28 | if isRestart { |
| 29 | logrus.Warn("The instance is not running, continuing with the restart") |
| 30 | return nil |
| 31 | } |
| 32 | return fmt.Errorf("expected status %#q, got %#q (maybe use `limactl stop -f`?)", limatype.StatusRunning, inst.Status) |
| 33 | } |
| 34 | |
| 35 | begin := time.Now() |
| 36 | if requested, err := autostart.RequestStop(ctx, inst); err != nil && !errors.Is(err, autostart.ErrNotSupported) { |
| 37 | return fmt.Errorf("failed to request stop via autostart manager: %w", err) |
| 38 | } else if !requested { |
| 39 | logrus.Infof("Sending SIGINT to hostagent process %d", inst.HostAgentPID) |
| 40 | if err := osutil.SysKill(inst.HostAgentPID, osutil.SigInt); err != nil { |
| 41 | logrus.Error(err) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | logrus.Info("Waiting for the host agent and the driver processes to shut down") |
| 46 | err := waitForHostAgentTermination(ctx, inst, begin) |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | |
| 51 | logrus.Info("Waiting for the instance to shut down") |
| 52 | return waitForInstanceShutdown(ctx, inst) |
| 53 | } |
| 54 | |
| 55 | func waitForHostAgentTermination(ctx context.Context, inst *limatype.Instance, begin time.Time) error { |
| 56 | ctx, cancel := context.WithTimeout(ctx, 3*time.Minute+10*time.Second) |
| 57 | defer cancel() |
| 58 | |
| 59 | var receivedExitingEvent bool |
| 60 | onEvent := func(ev hostagentevents.Event) bool { |
| 61 | if len(ev.Status.Errors) > 0 { |
| 62 | logrus.Errorf("%+v", ev.Status.Errors) |
| 63 | } |
| 64 | if ev.Status.Exiting { |
| 65 | receivedExitingEvent = true |
| 66 | return true |
| 67 | } |
| 68 | return false |
| 69 | } |
| 70 | |
| 71 | haStdoutPath := filepath.Join(inst.Dir, filenames.HostAgentStdoutLog) |
| 72 | haStderrPath := filepath.Join(inst.Dir, filenames.HostAgentStderrLog) |
| 73 | |
| 74 | if err := hostagentevents.Watch(ctx, haStdoutPath, haStderrPath, begin, true, onEvent); err != nil { |
| 75 | return err |
| 76 | } |
| 77 | |
| 78 | if !receivedExitingEvent { |
| 79 | return errors.New("did not receive an event with the `exiting` status") |
| 80 | } |
| 81 | |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | func waitForInstanceShutdown(ctx context.Context, inst *limatype.Instance) error { |
| 86 | ctx, cancel := context.WithTimeout(ctx, 3*time.Minute) |
| 87 | defer cancel() |
| 88 | |
| 89 | ticker := time.NewTicker(500 * time.Millisecond) |
| 90 | defer ticker.Stop() |
| 91 | |
| 92 | for { |
| 93 | select { |
| 94 | case <-ticker.C: |
| 95 | updatedInst, err := store.Inspect(ctx, inst.Name) |
| 96 | if err != nil { |
| 97 | return fmt.Errorf("failed to inspect instance status: %w", err) |
| 98 | } |
| 99 | |
| 100 | if updatedInst.Status == limatype.StatusStopped { |
| 101 | logrus.Infof("The instance %s has shut down", updatedInst.Name) |
| 102 | return nil |
| 103 | } |
| 104 | case <-ctx.Done(): |
| 105 | return errors.New("timed out waiting for instance to shut down after 3 minutes") |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func StopForcibly(inst *limatype.Instance) { |
| 111 | if inst.DriverPID > 0 { |
| 112 | logrus.Infof("Sending SIGKILL to the %s driver process %d", inst.VMType, inst.DriverPID) |
| 113 | if err := osutil.SysKill(inst.DriverPID, osutil.SigKill); err != nil { |
| 114 | logrus.Error(err) |
| 115 | } |
| 116 | } else { |
| 117 | logrus.Infof("The %s driver process seems already stopped", inst.VMType) |
| 118 | } |
| 119 | |
| 120 | |
| 121 | server.Stop(inst.Dir, true) |
| 122 | |
| 123 | for _, d := range inst.AdditionalDisks { |
| 124 | diskName := d.Name |
| 125 | disk, err := store.InspectDisk(diskName, d.FSType) |
| 126 | if err != nil { |
| 127 | logrus.Warnf("Disk %#q does not exist", diskName) |
| 128 | continue |
| 129 | } |
| 130 | if err := disk.Unlock(); err != nil { |
| 131 | logrus.Warnf("Failed to unlock disk %#q. To use, run `limactl disk unlock %v`", diskName, diskName) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if inst.HostAgentPID > 0 { |
| 136 | logrus.Infof("Sending SIGKILL to the host agent process %d", inst.HostAgentPID) |
| 137 | if err := osutil.SysKill(inst.HostAgentPID, osutil.SigKill); err != nil { |
| 138 | logrus.Error(err) |
| 139 | } |
| 140 | } else { |
| 141 | logrus.Info("The host agent process seems already stopped") |
| 142 | } |
| 143 | |
| 144 | globPatterns := strings.ReplaceAll(strings.Join(filenames.TmpFileSuffixes, " "), ".", "*.") |
| 145 | logrus.Infof("Removing %s under %#q", globPatterns, inst.Dir) |
| 146 | |
| 147 | fi, err := os.ReadDir(inst.Dir) |
| 148 | if err != nil { |
| 149 | logrus.Error(err) |
| 150 | return |
| 151 | } |
| 152 | for _, f := range fi { |
| 153 | path := filepath.Join(inst.Dir, f.Name()) |
| 154 | for _, suffix := range filenames.TmpFileSuffixes { |
| 155 | if strings.HasSuffix(path, suffix) { |
| 156 | logrus.Infof("Removing %#q", path) |
| 157 | if err := os.Remove(path); err != nil { |
| 158 | if errors.Is(err, os.ErrNotExist) { |
| 159 | logrus.Debug(err.Error()) |
| 160 | } else { |
| 161 | logrus.Error(err) |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |