Skip to content

Commit 9291305

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 ac70e20 commit 9291305

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

Diff for: cmd/limactl/guest_install.go

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
y, err := inst.LoadYAML()
60+
if err != nil {
61+
return err
62+
}
63+
64+
hostname := fmt.Sprintf("lima-%s", inst.Name)
65+
prefix := *y.GuestInstallPrefix
66+
67+
// lima-guestagent
68+
guestAgentBinary, err := usrlocalsharelima.GuestAgentBinary(*y.OS, *y.Arch)
69+
if err != nil {
70+
return err
71+
}
72+
tmp := "/tmp/lima-guestagent"
73+
bin := prefix + "/bin/lima-guestagent"
74+
logrus.Infof("Copying %q to %s", guestAgentBinary, hostname)
75+
scpArgs := []string{guestAgentBinary, hostname + ":" + tmp}
76+
if err := runCmd(scpExe, scpFlags, scpArgs...); err != nil {
77+
return nil
78+
}
79+
logrus.Infof("Installing %s to %s", tmp, bin)
80+
sshArgs := []string{hostname, "sudo", "install", "-m", "755", tmp, bin}
81+
if err := runCmd(sshExe, sshFlags, sshArgs...); err != nil {
82+
return nil
83+
}
84+
85+
// nerdctl-full.tgz
86+
nerdctlFilename := cacheutil.NerdctlArchive(y)
87+
if nerdctlFilename != "" {
88+
nerdctlArchive, err := cacheutil.EnsureNerdctlArchiveCache(cmd.Context(), y, false)
89+
if err != nil {
90+
return err
91+
}
92+
tmp := "/tmp/nerdctl-full.tgz"
93+
logrus.Infof("Copying %q to %s", nerdctlFilename, hostname)
94+
scpArgs := []string{nerdctlArchive, hostname + ":" + tmp}
95+
if err := runCmd(scpExe, scpFlags, scpArgs...); err != nil {
96+
return nil
97+
}
98+
logrus.Infof("Installing %s in %s", tmp, prefix)
99+
sshArgs := []string{hostname, "sudo", "tar", "Cxzf", prefix, tmp}
100+
if err := runCmd(sshExe, sshFlags, sshArgs...); err != nil {
101+
return nil
102+
}
103+
}
104+
105+
return nil
106+
}

Diff for: cmd/limactl/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func newApp() *cobra.Command {
111111
newSudoersCommand(),
112112
newPruneCommand(),
113113
newHostagentCommand(),
114+
newGuestInstallCommand(),
114115
newInfoCommand(),
115116
newShowSSHCommand(),
116117
newDebugCommand(),

Diff for: pkg/cacheutil/cacheutil.go

+13
Original file line numberDiff line numberDiff line change
@@ -3,12 +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

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+
1225
// EnsureNerdctlArchiveCache prefetches the nerdctl-full-VERSION-GOOS-GOARCH.tar.gz archive
1326
// into the cache before launching the hostagent process, so that we can show the progress in tty.
1427
// https://github.com/lima-vm/lima/issues/326

0 commit comments

Comments
 (0)