Skip to content

Conversation

norio-nomura
Copy link
Contributor

@norio-nomura norio-nomura commented Oct 5, 2025

This PR is formed with two parts:

  • autostart: Refactor to add autoStartManager to prepare for the following commit
  • autostart: Ensure the instance is started/stopped by launchd or systemctl

Refactor to add autoStartManager

  • Hide creating/deleting an autostart entry file into Register/Unregister as the implementation details.
  • Separate the launchd and systemd-specific code into packages.

Use launchctl or systemctl to start/stop the instance if it’s registered to autostart.

If the instance isn’t launched by launchd, it won’t be stopped on log out.

Affected sub commands:

  • limactl start
  • limactl stop
  • limactl restart
  • limactl edit
  • limactl shell

API changes

pkg/autostart

  • Introduced AutoStartedIdentifier():
    • If not empty, it indicates whether the instance was started by launchd or systemd.
  • Added RequestStart():
    • Delegates the operation to launchd or systemd.
  • Added RequestStop():
    • Delegates the operation to launchd or systemd.

pkg/autostart/launchd

  • Added AutoStartedServiceName():
    • Uses the XPC_SERVICE_NAME environment variable as the service name.
  • Added RequestStart():
    • Uses launchctl enable service-target to avoid failing bootstrap.
    • Uses launchctl bootstrap domain-target plist-path.
  • Added RequestStop():
    • Uses launchctl bootout service-target if the instance is launched by launchd.
  • Added --progress to the limactl option in io.lima-vm.autostart.INSTANCE.plist:
    • Required to support limactl start --progress.

pkg/autostart/systemd

  • Added AutoStartedServiceName():
    • Uses CurrentUnitName() by github.com/coreos/go-systemd/v22/util as the service identifier.
  • Added RequestStart():
    • Uses systemctl --user start unit-name.
  • Added RequestStop():
    • Uses systemctl --user stop unit-name if the instance is launched by systemd.
  • Added --progress to the limactl option in [email protected]:
    • Required to support limactl start --progress.

pkg/hostagent

  • Add AutoStartedIdentifier to Info.
    • If not empty, it indicates whether the instance was started by launchd or systemd.

pkg/instance

  • StartWithPaths():
    • Use autostart.IsRegistered() to check if the instance is registered to autostart.
    • If launchHostAgentForeground is true, ignore autostart registration.
    • If the instance is registered to autostart, use autostart.RequestStart() instead of launching HostAgent.
  • StopGracefully():
    • Use autostart.RequestStop().
  • Restart(), RestartForcibly():
    • Use autostart.IsRegistered() to skip networks.Reconcile() if the instance is registered to autostart.

@norio-nomura norio-nomura marked this pull request as draft October 5, 2025 08:55
@norio-nomura norio-nomura force-pushed the improve-autostart branch 7 times, most recently from 783c0f2 to 428fcb1 Compare October 5, 2025 12:13
@norio-nomura norio-nomura marked this pull request as ready for review October 5, 2025 12:42
@AkihiroSuda
Copy link
Member

How to test this PR?

@AkihiroSuda
Copy link
Member

Also please explain what issue is being solved by this PR

@norio-nomura
Copy link
Contributor Author

Also please explain what issue is being solved by this PR

opened #4141

@norio-nomura norio-nomura force-pushed the improve-autostart branch 2 times, most recently from ac9db28 to 31d7007 Compare October 6, 2025 07:05
@AkihiroSuda AkihiroSuda added this to the v2.0.0 milestone Oct 6, 2025
@norio-nomura norio-nomura force-pushed the improve-autostart branch 4 times, most recently from 89c500c to 7082f48 Compare October 8, 2025 15:42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be *_linux.go?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code that is platform-dependent and can only be built on Linux is already isolated in systemd_linux.go. systemd.go has code that can be built on any platform, only the operation is platform-dependent.

If we put the code itself included in systemd.go into systemd_linux.go, I think the number of dummy codes required for systemd_others.go will increase.


[Service]
ExecStart={{.Binary}} start %i --foreground
ExecStart={{.Binary}} start %i --foreground --progress
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be *_darwin.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code included in launchd.go is based on the darwin platform, but the build is not based on the darwin platform.

The runtime guard should be enough.

<string>start</string>
<string>{{ .Instance }}</string>
<string>--foreground</string>
<string>--progress</string>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

@norio-nomura norio-nomura Oct 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is no way to arbitrarily pass the --progress option from the limactl start process that the user starts to the limactl hostagent that is started via launchctl, It is to be handed over at the time of registration.

limactl hostagent collects progress logs regardless of whether there is a process to observe progress.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is --progress needed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To pass --progress to limactl hostagent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User -> limactl start --progress -> launchctl -> launchd -> limactl start --progress -> limactl hostagent --progress.
There is no api to passing --progress from user initiated limactl start to limactl hostagent via launchctl.

- Hide creating/deleting an autostart entry file into `Register`/`Unregister` as the implementation details.
- Separate the `launchd` and `systemd`-specific code into packages.

Signed-off-by: Norio Nomura <[email protected]>
…ctl`

If the instance isn’t launched by `launchd`, it won’t be stopped on log out.

This changes to use `launchctl` or `systemctl` to start/stop the instance if it’s registered to autostart.

## Affected sub commands:
- `limactl start`
- `limactl stop`
- `limactl restart`
- `limactl edit`
- `limactl shell`

## API changes

### `pkg/autostart`
- Introduced `AutoStartedIdentifier()`:
  - If not empty, it indicates whether the instance was started by `launchd` or `systemd`.
- Added `RequestStart()`:
  - Delegates the operation to `launchd` or `systemd`.
- Added `RequestStop()`:
  - Delegates the operation to `launchd` or `systemd`.

### `pkg/autostart/launchd`
- Added `AutoStartedServiceName()`:
  - Uses the XPC_SERVICE_NAME environment variable as the service name.
- Added `RequestStart()`:
  - Uses `launchctl enable service-target` to avoid failing `bootstrap`.
  - Uses `launchctl bootstrap domain-target plist-path`.
- Added `RequestStop()`:
  - Uses `launchctl bootout service-target` if the instance is launched by `launchd`.
- Added `--progress` to the `limactl` option in `io.lima-vm.autostart.INSTANCE.plist`:
  - Required to support `limactl start --progress`.

### `pkg/autostart/systemd`
- Added `AutoStartedServiceName()`:
  - Uses `CurrentUnitName()` by `github.com/coreos/go-systemd/v22/util` as the service identifier.
- Added `RequestStart()`:
  - Uses `systemctl --user start unit-name`.
- Added `RequestStop()`:
  - Uses `systemctl --user stop unit-name` if the instance is launched by `systemd`.
- Added `--progress` to the `limactl` option in `[email protected]`:
  - Required to support `limactl start --progress`.

### `pkg/hostagent`
- Add `AutoStartedIdentifier` to `Info`.
  - If not empty, it indicates whether the instance was started by `launchd` or `systemd`.

### `pkg/instance`
- `StartWithPaths()`:
  - Use `autostart.IsRegistered()` to check if the instance is registered to autostart.
  - If `launchHostAgentForeground` is true, ignore autostart registration.
  - If the instance is registered to autostart, use `autostart.RequestStart()` instead of launching HostAgent.
- `StopGracefully()`:
  - Use `autostart.RequestStop()`.
- `Restart()`, `RestartForcibly()`:
  - Use `autostart.IsRegistered()` to skip `networks.Reconcile()` if the instance is registered to autostart.

Signed-off-by: Norio Nomura <[email protected]>

bump `coreos/go-systemd` to v22.6.0

Signed-off-by: Norio Nomura <[email protected]>
…hctl 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]>
@norio-nomura
Copy link
Contributor Author

How to test this PR?

I was researching how to test and took a detour to fix test-port-forwarding.pl. I will consider it again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

limactl should use launchctl to start/stop instance if it's registered to start-at-login
2 participants