Skip to content

Commit 9aafc13

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 5e79e5b commit 9aafc13

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

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

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

Diff for: cmd/limactl/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ func newApp() *cobra.Command {
142142
newSudoersCommand(),
143143
newPruneCommand(),
144144
newHostagentCommand(),
145+
newGuestInstallCommand(),
145146
newInfoCommand(),
146147
newShowSSHCommand(),
147148
newDebugCommand(),

Diff for: pkg/cacheutil/cacheutil.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,25 @@ package cacheutil
33
import (
44
"context"
55
"fmt"
6+
"path"
67

78
"github.com/lima-vm/lima/pkg/downloader"
89
"github.com/lima-vm/lima/pkg/fileutils"
910
"github.com/lima-vm/lima/pkg/limayaml"
1011
)
1112

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

0 commit comments

Comments
 (0)