MCPcopy
hub / github.com/lima-vm/lima / StopForcibly

Function StopForcibly

pkg/instance/stop.go:111–179  ·  view source on GitHub ↗
(inst *limatype.Instance)

Source from the content-addressed store, hash-verified

109}
110
111func StopForcibly(inst *limatype.Instance) {
112 if inst.DriverPID > 0 {
113 logrus.Infof("Sending SIGKILL to the %s driver process %d", inst.VMType, inst.DriverPID)
114 if err := osutil.SysKill(inst.DriverPID, osutil.SigKill); err != nil {
115 logrus.Error(err)
116 }
117 } else {
118 logrus.Infof("The %s driver process seems already stopped", inst.VMType)
119 }
120
121 driverPIDPattern := filepath.Join(inst.Dir, "lima-driver-*.pid")
122 files, _ := filepath.Glob(driverPIDPattern)
123 for _, pidFile := range files {
124 if b, readErr := os.ReadFile(pidFile); readErr == nil {
125 if pid, convErr := strconv.Atoi(strings.TrimSpace(string(b))); convErr == nil && pid > 0 {
126 logrus.Infof("Sending SIGKILL to external driver process %d (from %s)", pid, filepath.Base(pidFile))
127 if err := osutil.SysKill(pid, osutil.SigKill); err != nil && !errors.Is(err, os.ErrProcessDone) && !errors.Is(err, syscall.ESRCH) {
128 logrus.Warnf("Failed to kill external driver process %d: %v", pid, err)
129 }
130 }
131 }
132 _ = os.Remove(pidFile) // Clean up the orphaned pid file
133 }
134
135 for _, d := range inst.AdditionalDisks {
136 diskName := d.Name
137 disk, err := store.InspectDisk(diskName, d.FSType)
138 if err != nil {
139 logrus.Warnf("Disk %q does not exist", diskName)
140 continue
141 }
142 if err := disk.Unlock(); err != nil {
143 logrus.Warnf("Failed to unlock disk %q. To use, run `limactl disk unlock %v`", diskName, diskName)
144 }
145 }
146
147 if inst.HostAgentPID > 0 {
148 logrus.Infof("Sending SIGKILL to the host agent process %d", inst.HostAgentPID)
149 if err := osutil.SysKill(inst.HostAgentPID, osutil.SigKill); err != nil {
150 logrus.Error(err)
151 }
152 } else {
153 logrus.Info("The host agent process seems already stopped")
154 }
155
156 globPatterns := strings.ReplaceAll(strings.Join(filenames.TmpFileSuffixes, " "), ".", "*.")
157 logrus.Infof("Removing %s under %q", globPatterns, inst.Dir)
158
159 fi, err := os.ReadDir(inst.Dir)
160 if err != nil {
161 logrus.Error(err)
162 return
163 }
164 for _, f := range fi {
165 path := filepath.Join(inst.Dir, f.Name())
166 for _, suffix := range filenames.TmpFileSuffixes {
167 if strings.HasSuffix(path, suffix) {
168 logrus.Infof("Removing %q", path)

Callers 4

stopActionFunction · 0.92
factoryResetActionFunction · 0.92
RestartForciblyFunction · 0.85
DeleteFunction · 0.85

Calls 9

SysKillFunction · 0.92
InspectDiskFunction · 0.92
GlobMethod · 0.80
ReadFileMethod · 0.80
RemoveMethod · 0.80
UnlockMethod · 0.80
InfoMethod · 0.65
NameMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected