Skip to content

Commit 0682b37

Browse files
committed
Add command to install the guest components
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]>
1 parent a6ca564 commit 0682b37

File tree

3 files changed

+118
-2
lines changed

3 files changed

+118
-2
lines changed

Diff for: cmd/limactl/guest-install.go

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"os/exec"
10+
"path/filepath"
11+
12+
"github.com/lima-vm/lima/pkg/cacheutil"
13+
"github.com/lima-vm/lima/pkg/store"
14+
"github.com/lima-vm/lima/pkg/store/filenames"
15+
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
16+
17+
"github.com/sirupsen/logrus"
18+
"github.com/spf13/cobra"
19+
)
20+
21+
func newGuestInstallCommand() *cobra.Command {
22+
guestInstallCommand := &cobra.Command{
23+
Use: "guest-install INSTANCE",
24+
Short: "Install guest components",
25+
Args: WrapArgsError(cobra.MaximumNArgs(1)),
26+
RunE: guestInstallAction,
27+
ValidArgsFunction: cobra.NoFileCompletions,
28+
Hidden: true,
29+
}
30+
return guestInstallCommand
31+
}
32+
33+
func runCmd(name string, flags []string, args ...string) error {
34+
cmd := exec.Command(name, append(flags, args...)...)
35+
cmd.Stdout = os.Stdout
36+
cmd.Stderr = os.Stderr
37+
logrus.Debugf("executing %v", cmd.Args)
38+
return cmd.Run()
39+
}
40+
41+
func guestInstallAction(cmd *cobra.Command, args []string) error {
42+
instName := DefaultInstanceName
43+
if len(args) > 0 {
44+
instName = args[0]
45+
}
46+
47+
inst, err := store.Inspect(instName)
48+
if err != nil {
49+
return err
50+
}
51+
if inst.Status == store.StatusStopped {
52+
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
53+
}
54+
55+
sshExe := "ssh"
56+
sshConfig := filepath.Join(inst.Dir, filenames.SSHConfig)
57+
sshFlags := []string{"-F", sshConfig}
58+
59+
scpExe := "scp"
60+
scpFlags := sshFlags
61+
62+
hostname := fmt.Sprintf("lima-%s", inst.Name)
63+
prefix := *inst.Config.GuestInstallPrefix
64+
65+
// lima-guestagent
66+
guestAgentBinary, err := usrlocalsharelima.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
67+
if err != nil {
68+
return err
69+
}
70+
tmp := "/tmp/lima-guestagent"
71+
bin := prefix + "/bin/lima-guestagent"
72+
logrus.Infof("Copying %q to %s", guestAgentBinary, hostname)
73+
scpArgs := []string{guestAgentBinary, hostname + ":" + tmp}
74+
if err := runCmd(scpExe, scpFlags, scpArgs...); err != nil {
75+
return nil
76+
}
77+
logrus.Infof("Installing %s to %s", tmp, bin)
78+
sshArgs := []string{hostname, "sudo", "install", "-m", "755", tmp, bin}
79+
if err := runCmd(sshExe, sshFlags, sshArgs...); err != nil {
80+
return nil
81+
}
82+
83+
// nerdctl-full.tgz
84+
nerdctlFilename := cacheutil.NerdctlArchive(inst.Config)
85+
if nerdctlFilename != "" {
86+
nerdctlArchive, err := cacheutil.EnsureNerdctlArchiveCache(cmd.Context(), inst.Config, false)
87+
if err != nil {
88+
return err
89+
}
90+
tmp := "/tmp/nerdctl-full.tgz"
91+
logrus.Infof("Copying %q to %s", nerdctlFilename, hostname)
92+
scpArgs := []string{nerdctlArchive, hostname + ":" + tmp}
93+
if err := runCmd(scpExe, scpFlags, scpArgs...); err != nil {
94+
return nil
95+
}
96+
logrus.Infof("Installing %s in %s", tmp, prefix)
97+
sshArgs := []string{hostname, "sudo", "tar", "Cxzf", prefix, tmp}
98+
if err := runCmd(sshExe, sshFlags, sshArgs...); err != nil {
99+
return nil
100+
}
101+
}
102+
103+
return nil
104+
}

Diff for: cmd/limactl/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func newApp() *cobra.Command {
171171
newValidateCommand(),
172172
newPruneCommand(),
173173
newHostagentCommand(),
174+
newGuestInstallCommand(),
174175
newInfoCommand(),
175176
newShowSSHCommand(),
176177
newDebugCommand(),

Diff for: pkg/cacheutil/cacheutil.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,25 @@ package cacheutil
66
import (
77
"context"
88
"fmt"
9+
"path"
910

1011
"github.com/lima-vm/lima/pkg/downloader"
1112
"github.com/lima-vm/lima/pkg/fileutils"
1213
"github.com/lima-vm/lima/pkg/limayaml"
1314
)
1415

15-
// EnsureNerdctlArchiveCache prefetches the archive into the cache.
16-
//
16+
// NerdctlArchive returns the basename of the archive.
17+
func NerdctlArchive(y *limayaml.LimaYAML) string {
18+
if *y.Containerd.System || *y.Containerd.User {
19+
for _, f := range y.Containerd.Archives {
20+
if f.Arch == *y.Arch {
21+
return path.Base(f.Location)
22+
}
23+
}
24+
}
25+
return ""
26+
}
27+
1728
// EnsureNerdctlArchiveCache prefetches the nerdctl-full-VERSION-GOOS-GOARCH.tar.gz archive
1829
// into the cache before launching the hostagent process, so that we can show the progress in tty.
1930
// https://github.com/lima-vm/lima/issues/326

0 commit comments

Comments
 (0)