Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

limactl start: add --foreground flag #2146

Merged
merged 1 commit into from
Jan 18, 2024

Conversation

norio-nomura
Copy link
Contributor

This change adds the --foreground flag to limactl start.

This flag:

  • Launches limactl hostagent with syscall.Exec, which allows limactl start --foreground to be used in macOS's launchd.plist, making it possible to hide limactl hostagent.
  • When launched from the terminal, it uses the terminal for standard output and standard error instead of the default ha.std{out,err}.log files. In this case, a message indicating that the default log files are not used is written to the log files.
  • When launched from something other than the terminal, such as launchd, it uses the ha.std{out,err}.log files as before.
  • Cannot be used on Windows.

The following are the updated steps to set up the Docker instance of Lima to start at login using launchd on macOS:

# Create the docker instance
limactl create template://docker --vm-type vz --rosetta --network vzNAT --tty=false

# Create the launchd plist
echo '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>io.lima-vm.docker</string>
	<key>ProgramArguments</key>
	<array>
		<string>'"$(which limactl)"'</string>
		<string>start</string>
		<string>--foreground</string>
		<string>docker</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>StandardErrorPath</key>
	<string>launchd.stderr.log</string>
	<key>StandardOutPath</key>
	<string>launchd.stdout.log</string>
	<key>WorkingDirectory</key>
	<string>'"$(limactl list docker --format '{{.Dir}}')"'</string>
</dict>
</plist>' > ~/Library/LaunchAgents/io.lima-vm.docker.plist

# Load the launchd plist and launchd will start the docker instance
launchctl load ~/Library/LaunchAgents/io.lima-vm.docker.plist

that changed from #2140:

  • Changed to limactl create because it's no longer necessary to run limactl start before loading into launchd.
  • Changed from limactl hostagent to limactl start --foreground.
  • Changed Standard{Error,Out}Path to launchd.std{err,out}.log, so that only the output of limactl start is written.

return err
}
if foreground {
ctx = start.WithLaunchHostAgentForeground(ctx, foreground)
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason to use a context for this?
Wondering if we can just add an argument to start.Start()

Copy link
Contributor Author

@norio-nomura norio-nomura Jan 17, 2024

Choose a reason for hiding this comment

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

There's no particular reason. I just mimicked the way the timeout flag is written.

Copy link
Member

Choose a reason for hiding this comment

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

The timeout is contextualized because it has to be passed to many functions.
This is different from the foreground flag that is only used once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understood, I'll try changing it.

cmd/limactl/start.go Outdated Show resolved Hide resolved
@AkihiroSuda AkihiroSuda added this to the v0.20.0 milestone Jan 17, 2024
@AkihiroSuda
Copy link
Member

Please squash the commits

@norio-nomura norio-nomura force-pushed the limactl-start-foreground branch 2 times, most recently from 324d034 to 3a74a52 Compare January 17, 2024 12:27
@@ -122,7 +122,7 @@ func editAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
return start.Start(ctx, inst)
return start.Start(ctx, inst, start.DefaultLaunchHostAgentForeground)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it be better if the --foreground flag was also available for limactl edit?

@@ -33,6 +35,10 @@ import (
// to be running before timing out.
const DefaultWatchHostAgentEventsTimeout = 10 * time.Minute

// DefaultLaunchHostAgentForeground is the default value of the boolean to use for
// Start()
const DefaultLaunchHostAgentForeground = false
Copy link
Member

Choose a reason for hiding this comment

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

No need to define this const

AkihiroSuda
AkihiroSuda previously approved these changes Jan 17, 2024
Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

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

Thanks

This change adds the `--foreground` flag to `limactl start`.

This flag:

- Launches `limactl hostagent` with `syscall.Exec`, which allows `limactl start --foreground` to be used in macOS's `launchd.plist`, making it possible to hide `limactl hostagent`.
- When launched from the terminal, it uses the terminal for standard output and standard error instead of the default `ha.std{out,err}.log` files. In this case, a message indicating that the default log files are not used is written to the log files.
- When launched from something other than the terminal, such as `launchd`, it uses the `ha.std{out,err}.log` files as before.
- Cannot be used on Windows.

The following are the updated steps to set up the Docker instance of Lima to start at login using launchd on macOS:
```bash
# Create the docker instance
limactl create template://docker --vm-type vz --rosetta --network vzNAT --tty=false

# Create the launchd plist
echo '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>io.lima-vm.docker</string>
	<key>ProgramArguments</key>
	<array>
		<string>'"$(which limactl)"'</string>
		<string>start</string>
		<string>--foreground</string>
		<string>docker</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>StandardErrorPath</key>
	<string>launchd.stderr.log</string>
	<key>StandardOutPath</key>
	<string>launchd.stdout.log</string>
	<key>WorkingDirectory</key>
	<string>'"$(limactl list docker --format '{{.Dir}}')"'</string>
</dict>
</plist>' > ~/Library/LaunchAgents/io.lima-vm.docker.plist

# Load the launchd plist and launchd will start the docker instance
launchctl load ~/Library/LaunchAgents/io.lima-vm.docker.plist
```
that changed from lima-vm#2140:

- Changed to `limactl create` because it's no longer necessary to run `limactl start` before loading into `launchd`.
- Changed from `limactl hostagent` to `limactl start --foreground`.
- Changed `Standard{Error,Out}Path` to `launchd.std{err,out}.log`, so that only the output of `limactl start` is written.

Signed-off-by: Norio Nomura <norio.nomura@gmail.com>

Update cmd/limactl/start.go

Change `instance` to `hostagent`

Co-authored-by: Akihiro Suda <suda.kyoto@gmail.com>

Add `launchHostAgentForeground` to `start.Start()`

Avoid using `Context` to passing `launchHostAgentForeground`

Remove `start.DefaultLaunchHostAgentForeground`
Copy link
Member

@jandubois jandubois left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM

if err := haCmd.Start(); err != nil {
if launchHostAgentForeground {
logrus.Info("Running the host agent in the foreground")
if isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd()) {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it is time to create a wrapper for this condition:

$ ag IsCygwin
cmd/limactl/main.go
82:		if runtime.GOOS == "windows" && isatty.IsCygwinTerminal(os.Stdout.Fd()) {

cmd/limactl/list.go
171:		if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {

cmd/limactl/shell.go
176:	if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {

pkg/progressbar/progressbar.go
15:	if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {

pkg/qemu/entitlementutil/entitlementutil.go
88:		if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {

Not sure what the right package is, maybe uiutil.isatty()?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but it can be another PR

@AkihiroSuda AkihiroSuda merged commit 3057806 into lima-vm:master Jan 18, 2024
25 checks passed
@norio-nomura norio-nomura deleted the limactl-start-foreground branch January 18, 2024 13:49
@norio-nomura
Copy link
Contributor Author

Thanks! 🙏🏻

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.

3 participants