Skip to content

Commit f356704

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 ca07442 commit f356704

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

Diff for: cmd/limactl/guest_install.go

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 guestInstallAction(_ *cobra.Command, args []string) error {
31+
instName := DefaultInstanceName
32+
if len(args) > 0 {
33+
instName = args[0]
34+
}
35+
36+
inst, err := store.Inspect(instName)
37+
if err != nil {
38+
return err
39+
}
40+
if inst.Status == store.StatusStopped {
41+
return fmt.Errorf("instance %q is stopped, run `limactl start %s` to start the instance", instName, instName)
42+
}
43+
44+
sshConfig := filepath.Join(inst.Dir, filenames.SSHConfig)
45+
sshFlags := []string{"-F", sshConfig}
46+
47+
y, err := inst.LoadYAML()
48+
if err != nil {
49+
return err
50+
}
51+
52+
hostname := fmt.Sprintf("lima-%s", inst.Name)
53+
prefix := *y.GuestInstallPrefix
54+
55+
// lima-guestagent
56+
guestAgentBinary, err := usrlocalsharelima.GuestAgentBinary(*y.OS, *y.Arch)
57+
if err != nil {
58+
return err
59+
}
60+
tmp := "/tmp/lima-guestagent"
61+
bin := prefix + "/bin/lima-guestagent"
62+
logrus.Infof("Copying %q to %s", guestAgentBinary, hostname)
63+
scpArgs := []string{guestAgentBinary, hostname + ":" + tmp}
64+
scpCmd := exec.Command("scp", append(sshFlags, scpArgs...)...)
65+
scpCmd.Stdout = os.Stdout
66+
scpCmd.Stderr = os.Stderr
67+
logrus.Debugf("executing %v", scpCmd.Args)
68+
if err := scpCmd.Run(); err != nil {
69+
return nil
70+
}
71+
logrus.Infof("Installing %s to %s", tmp, bin)
72+
sshArgs := []string{hostname, "sudo", "install", "-m", "755", tmp, bin}
73+
sshCmd := exec.Command("ssh", append(sshFlags, sshArgs...)...)
74+
sshCmd.Stdout = os.Stdout
75+
sshCmd.Stderr = os.Stderr
76+
logrus.Debugf("executing %v", sshCmd.Args)
77+
if err := sshCmd.Run(); err != nil {
78+
return nil
79+
}
80+
81+
// nerdctl-full.tgz
82+
nerdctlFilename := cacheutil.NerdctlArchive(y)
83+
if nerdctlFilename != "" {
84+
nerdctlArchive, err := cacheutil.EnsureNerdctlArchiveCache(y, false)
85+
if err != nil {
86+
return err
87+
}
88+
tmp := "/tmp/nerdctl-full.tgz"
89+
logrus.Infof("Copying %q to %s", nerdctlFilename, hostname)
90+
scpArgs := []string{nerdctlArchive, hostname + ":" + tmp}
91+
scpCmd := exec.Command("scp", append(sshFlags, scpArgs...)...)
92+
scpCmd.Stdout = os.Stdout
93+
scpCmd.Stderr = os.Stderr
94+
logrus.Debugf("executing %v", scpCmd.Args)
95+
if err := scpCmd.Run(); 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+
sshCmd := exec.Command("ssh", append(sshFlags, sshArgs...)...)
101+
sshCmd.Stdout = os.Stdout
102+
sshCmd.Stderr = os.Stderr
103+
logrus.Debugf("executing %v", sshCmd.Args)
104+
if err := sshCmd.Run(); err != nil {
105+
return nil
106+
}
107+
}
108+
109+
return nil
110+
}

Diff for: cmd/limactl/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func newApp() *cobra.Command {
107107
newSudoersCommand(),
108108
newPruneCommand(),
109109
newHostagentCommand(),
110+
newGuestInstallCommand(),
110111
newInfoCommand(),
111112
newShowSSHCommand(),
112113
newDebugCommand(),

Diff for: pkg/cacheutil/cacheutil.go

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ import (
99
"github.com/lima-vm/lima/pkg/limayaml"
1010
)
1111

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

0 commit comments

Comments
 (0)