Skip to content

Commit 89c500c

Browse files
committed
pkg/autostart/launchd: call launchctl bootout before calling launchctl bootstrap
Because instances may be stopped without unloading the plist file. e.g. `limactl stop -f` or `limactl factory-reset`. If the plist file is not unloaded, `launchctl bootstrap` will fail. Signed-off-by: Norio Nomura <[email protected]>
1 parent 7817017 commit 89c500c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

pkg/autostart/launchd/launchd.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ func launchctl(ctx context.Context, args ...string) error {
4646
return cmd.Run()
4747
}
4848

49+
func launchctlWithoutOutput(ctx context.Context, args ...string) error {
50+
cmd := exec.CommandContext(ctx, "launchctl", args...)
51+
logrus.Debugf("running command without output: %v", cmd.Args)
52+
return cmd.Run()
53+
}
54+
4955
// AutoStartedServiceName returns the launchd service name if the instance is started by launchd.
5056
func AutoStartedServiceName() string {
5157
// Assume the instance is started by launchd if XPC_SERVICE_NAME is set and not "0".
@@ -67,7 +73,10 @@ func serviceTarget(instName string) string {
6773
}
6874

6975
func RequestStart(ctx context.Context, inst *limatype.Instance) error {
70-
// If disabled, bootstrap will fail.
76+
// Call `launchctl bootout` first, because instance may be stopped without unloading the plist file.
77+
// If the plist file is not unloaded, `launchctl bootstrap` will fail.
78+
_ = launchctlWithoutOutput(ctx, "bootout", serviceTarget(inst.Name))
79+
// If disabled, `launchctl bootstrap` will fail.
7180
_ = EnableDisableService(ctx, true, inst.Name)
7281
if err := launchctl(ctx, "bootstrap", domainTarget(), GetPlistPath(inst.Name)); err != nil {
7382
return fmt.Errorf("failed to start the instance %q via launchctl: %w", inst.Name, err)

0 commit comments

Comments
 (0)