Skip to content

Commit 6a2f11c

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 <anders.f.bjorklund@gmail.com>
1 parent 452e94c commit 6a2f11c

File tree

3 files changed

+122
-2
lines changed

3 files changed

+122
-2
lines changed

cmd/limactl/guest-install.go

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

cmd/limactl/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ func newApp() *cobra.Command {
173173
newValidateCommand(),
174174
newPruneCommand(),
175175
newHostagentCommand(),
176+
newGuestInstallCommand(),
176177
newInfoCommand(),
177178
newShowSSHCommand(),
178179
newDebugCommand(),

pkg/cacheutil/cacheutil.go

Lines changed: 13 additions & 2 deletions
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/v2/pkg/downloader"
1112
"github.com/lima-vm/lima/v2/pkg/fileutils"
1213
"github.com/lima-vm/lima/v2/pkg/limatype"
1314
)
1415

15-
// EnsureNerdctlArchiveCache prefetches the archive into the cache.
16-
//
16+
// NerdctlArchive returns the basename of the archive.
17+
func NerdctlArchive(y *limatype.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)