Skip to content

Commit

Permalink
Add command to install the guest components
Browse files Browse the repository at this point in the history
This will install the lima-guestagent in the instance.

It will install nerdctl-full.tgz, if it has been enabled.

Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
  • Loading branch information
afbjorklund committed Mar 18, 2024
1 parent ac70e20 commit 9291305
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
106 changes: 106 additions & 0 deletions cmd/limactl/guest_install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package main

import (
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/lima-vm/lima/pkg/cacheutil"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/lima-vm/lima/pkg/usrlocalsharelima"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func newGuestInstallCommand() *cobra.Command {
guestInstallCommand := &cobra.Command{
Use: "guest-install INSTANCE",
Short: "Install guest components",
Args: WrapArgsError(cobra.MaximumNArgs(1)),
RunE: guestInstallAction,
ValidArgsFunction: cobra.NoFileCompletions,
Hidden: true,
}
return guestInstallCommand
}

func runCmd(name string, flags []string, args ...string) error {
cmd := exec.Command(name, append(flags, args...)...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
logrus.Debugf("executing %v", cmd.Args)
return cmd.Run()
}

func guestInstallAction(cmd *cobra.Command, args []string) error {
instName := DefaultInstanceName
if len(args) > 0 {
instName = args[0]
}

inst, err := store.Inspect(instName)
if err != nil {
return err
}
if inst.Status == store.StatusStopped {
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
}

sshExe := "ssh"
sshConfig := filepath.Join(inst.Dir, filenames.SSHConfig)
sshFlags := []string{"-F", sshConfig}

scpExe := "scp"
scpFlags := sshFlags

y, err := inst.LoadYAML()
if err != nil {
return err
}

hostname := fmt.Sprintf("lima-%s", inst.Name)
prefix := *y.GuestInstallPrefix

// lima-guestagent
guestAgentBinary, err := usrlocalsharelima.GuestAgentBinary(*y.OS, *y.Arch)
if err != nil {
return err
}
tmp := "/tmp/lima-guestagent"
bin := prefix + "/bin/lima-guestagent"
logrus.Infof("Copying %q to %s", guestAgentBinary, hostname)
scpArgs := []string{guestAgentBinary, hostname + ":" + tmp}
if err := runCmd(scpExe, scpFlags, scpArgs...); err != nil {
return nil
}
logrus.Infof("Installing %s to %s", tmp, bin)
sshArgs := []string{hostname, "sudo", "install", "-m", "755", tmp, bin}
if err := runCmd(sshExe, sshFlags, sshArgs...); err != nil {
return nil
}

// nerdctl-full.tgz
nerdctlFilename := cacheutil.NerdctlArchive(y)
if nerdctlFilename != "" {
nerdctlArchive, err := cacheutil.EnsureNerdctlArchiveCache(cmd.Context(), y, false)
if err != nil {
return err
}
tmp := "/tmp/nerdctl-full.tgz"
logrus.Infof("Copying %q to %s", nerdctlFilename, hostname)
scpArgs := []string{nerdctlArchive, hostname + ":" + tmp}
if err := runCmd(scpExe, scpFlags, scpArgs...); err != nil {
return nil
}
logrus.Infof("Installing %s in %s", tmp, prefix)
sshArgs := []string{hostname, "sudo", "tar", "Cxzf", prefix, tmp}
if err := runCmd(sshExe, sshFlags, sshArgs...); err != nil {
return nil
}
}

return nil
}
1 change: 1 addition & 0 deletions cmd/limactl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func newApp() *cobra.Command {
newSudoersCommand(),
newPruneCommand(),
newHostagentCommand(),
newGuestInstallCommand(),
newInfoCommand(),
newShowSSHCommand(),
newDebugCommand(),
Expand Down
13 changes: 13 additions & 0 deletions pkg/cacheutil/cacheutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@ package cacheutil
import (
"context"
"fmt"
"path"

"github.com/lima-vm/lima/pkg/downloader"
"github.com/lima-vm/lima/pkg/fileutils"
"github.com/lima-vm/lima/pkg/limayaml"
)

// NerdctlArchive returns the basename of the archive
func NerdctlArchive(y *limayaml.LimaYAML) string {
if *y.Containerd.System || *y.Containerd.User {
for _, f := range y.Containerd.Archives {
if f.Arch == *y.Arch {
return path.Base(f.Location)
}
}
}
return ""
}

// EnsureNerdctlArchiveCache prefetches the nerdctl-full-VERSION-GOOS-GOARCH.tar.gz archive
// into the cache before launching the hostagent process, so that we can show the progress in tty.
// https://github.com/lima-vm/lima/issues/326
Expand Down

0 comments on commit 9291305

Please sign in to comment.