package systemd
import (
"context"
_ "embed"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/sirupsen/logrus"
"github.com/lima-vm/lima/v2/pkg/limatype"
)
var Template string
func GetUnitPath(instName string) string {
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
if xdgConfigHome == "" {
xdgConfigHome = filepath.Join(os.Getenv("HOME"), ".config")
}
return fmt.Sprintf("%s/systemd/user/%s", xdgConfigHome, UnitNameFrom(instName))
}
func UnitNameFrom(instName string) string {
return fmt.Sprintf("lima-vm@%s.service", instName)
}
func EnableDisableUnit(ctx context.Context, enable bool, instName string) error {
action := "enable"
if !enable {
action = "disable"
}
return systemctl(ctx, "--user", action, UnitNameFrom(instName))
}
func systemctl(ctx context.Context, args ...string) error {
cmd := exec.CommandContext(ctx, "systemctl", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
logrus.Debugf("running command: %v", cmd.Args)
return cmd.Run()
}
func AutoStartedUnitName() string {
return CurrentUnitName()
}
func RequestStart(ctx context.Context, inst *limatype.Instance) error {
if err := systemctl(ctx, "--user", "start", UnitNameFrom(inst.Name)); err != nil {
return fmt.Errorf("failed to start the instance %q via systemctl: %w", inst.Name, err)
}
return nil
}
func RequestStop(ctx context.Context, inst *limatype.Instance) (bool, error) {
if inst.AutoStartedIdentifier == UnitNameFrom(inst.Name) {
logrus.Infof("Stopping the instance %q started by systemd", inst.Name)
if err := systemctl(ctx, "--user", "stop", inst.AutoStartedIdentifier); err != nil {
return false, fmt.Errorf("failed to stop the instance %q via systemctl: %w", inst.Name, err)
}
return true, nil
}
return false, nil
}