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 <[email protected]>
  • Loading branch information
afbjorklund committed Nov 24, 2023
1 parent ca07442 commit f356704
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
110 changes: 110 additions & 0 deletions cmd/limactl/guest_install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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 guestInstallAction(_ *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)
}

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

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}
scpCmd := exec.Command("scp", append(sshFlags, scpArgs...)...)
scpCmd.Stdout = os.Stdout
scpCmd.Stderr = os.Stderr
logrus.Debugf("executing %v", scpCmd.Args)
if err := scpCmd.Run(); err != nil {
return nil
}
logrus.Infof("Installing %s to %s", tmp, bin)
sshArgs := []string{hostname, "sudo", "install", "-m", "755", tmp, bin}
sshCmd := exec.Command("ssh", append(sshFlags, sshArgs...)...)
sshCmd.Stdout = os.Stdout
sshCmd.Stderr = os.Stderr
logrus.Debugf("executing %v", sshCmd.Args)
if err := sshCmd.Run(); err != nil {
return nil
}

// nerdctl-full.tgz
nerdctlFilename := cacheutil.NerdctlArchive(y)
if nerdctlFilename != "" {
nerdctlArchive, err := cacheutil.EnsureNerdctlArchiveCache(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}
scpCmd := exec.Command("scp", append(sshFlags, scpArgs...)...)
scpCmd.Stdout = os.Stdout
scpCmd.Stderr = os.Stderr
logrus.Debugf("executing %v", scpCmd.Args)
if err := scpCmd.Run(); err != nil {
return nil
}
logrus.Infof("Installing %s in %s", tmp, prefix)
sshArgs := []string{hostname, "sudo", "tar", "Cxzf", prefix, tmp}
sshCmd := exec.Command("ssh", append(sshFlags, sshArgs...)...)
sshCmd.Stdout = os.Stdout
sshCmd.Stderr = os.Stderr
logrus.Debugf("executing %v", sshCmd.Args)
if err := sshCmd.Run(); 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 @@ -107,6 +107,7 @@ func newApp() *cobra.Command {
newSudoersCommand(),
newPruneCommand(),
newHostagentCommand(),
newGuestInstallCommand(),
newInfoCommand(),
newShowSSHCommand(),
newDebugCommand(),
Expand Down
12 changes: 12 additions & 0 deletions pkg/cacheutil/cacheutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ import (
"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 f356704

Please sign in to comment.