Skip to content

Commit 008aca9

Browse files
committed
hostagent: verify existence of guestagent earlier
Signed-off-by: Akihiro Suda <[email protected]>
1 parent a047e65 commit 008aca9

File tree

4 files changed

+57
-30
lines changed

4 files changed

+57
-30
lines changed

cmd/limactl/hostagent.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func newHostagentCommand() *cobra.Command {
3232
hostagentCommand.Flags().StringP("pidfile", "p", "", "Write PID to file")
3333
hostagentCommand.Flags().String("socket", "", "Path of hostagent socket")
3434
hostagentCommand.Flags().Bool("run-gui", false, "Run GUI synchronously within hostagent")
35+
hostagentCommand.Flags().String("guestagent", "", "Local file path (not URL) of lima-guestagent.OS-ARCH[.gz]")
3536
hostagentCommand.Flags().String("nerdctl-archive", "", "Local file path (not URL) of nerdctl-full-VERSION-GOOS-GOARCH.tar.gz")
3637
return hostagentCommand
3738
}
@@ -78,6 +79,13 @@ func hostagentAction(cmd *cobra.Command, args []string) error {
7879

7980
initLogrus(stderr)
8081
var opts []hostagent.Opt
82+
guestagentBinary, err := cmd.Flags().GetString("guestagent")
83+
if err != nil {
84+
return err
85+
}
86+
if guestagentBinary != "" {
87+
opts = append(opts, hostagent.WithGuestAgentBinary(guestagentBinary))
88+
}
8189
nerdctlArchive, err := cmd.Flags().GetString("nerdctl-archive")
8290
if err != nil {
8391
return err

pkg/cidata/cidata.go

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/lima-vm/lima/pkg/osutil"
3131
"github.com/lima-vm/lima/pkg/sshutil"
3232
"github.com/lima-vm/lima/pkg/store/filenames"
33-
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
3433
"github.com/sirupsen/logrus"
3534
)
3635

@@ -355,7 +354,7 @@ func GenerateCloudConfig(instDir, name string, instConfig *limayaml.LimaYAML) er
355354
return os.WriteFile(filepath.Join(instDir, filenames.CloudConfig), config, 0o444)
356355
}
357356

358-
func GenerateISO9660(instDir, name string, instConfig *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort int, nerdctlArchive string, vsockPort int, virtioPort string) error {
357+
func GenerateISO9660(instDir, name string, instConfig *limayaml.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort int, guestAgentBinary, nerdctlArchive string, vsockPort int, virtioPort string) error {
359358
args, err := templateArgs(true, instDir, name, instConfig, udpDNSLocalPort, tcpDNSLocalPort, vsockPort, virtioPort)
360359
if err != nil {
361360
return err
@@ -391,34 +390,32 @@ func GenerateISO9660(instDir, name string, instConfig *limayaml.LimaYAML, udpDNS
391390
}
392391
}
393392

394-
guestAgentBinary, err := usrlocalsharelima.GuestAgentBinary(*instConfig.OS, *instConfig.Arch)
395-
if err != nil {
396-
return err
397-
}
398-
var guestAgent io.ReadCloser
399-
if strings.HasSuffix(guestAgentBinary, ".gz") {
400-
logrus.Debugf("Decompressing %s", guestAgentBinary)
401-
guestAgentGz, err := os.Open(guestAgentBinary)
402-
if err != nil {
403-
return err
404-
}
405-
defer guestAgentGz.Close()
406-
guestAgent, err = gzip.NewReader(guestAgentGz)
407-
if err != nil {
408-
return err
409-
}
410-
} else {
411-
guestAgent, err = os.Open(guestAgentBinary)
412-
if err != nil {
413-
return err
393+
if guestAgentBinary != "" {
394+
var guestAgent io.ReadCloser
395+
if strings.HasSuffix(guestAgentBinary, ".gz") {
396+
logrus.Debugf("Decompressing %s", guestAgentBinary)
397+
guestAgentGz, err := os.Open(guestAgentBinary)
398+
if err != nil {
399+
return err
400+
}
401+
defer guestAgentGz.Close()
402+
guestAgent, err = gzip.NewReader(guestAgentGz)
403+
if err != nil {
404+
return err
405+
}
406+
} else {
407+
guestAgent, err = os.Open(guestAgentBinary)
408+
if err != nil {
409+
return err
410+
}
414411
}
415-
}
416412

417-
defer guestAgent.Close()
418-
layout = append(layout, iso9660util.Entry{
419-
Path: "lima-guestagent",
420-
Reader: guestAgent,
421-
})
413+
defer guestAgent.Close()
414+
layout = append(layout, iso9660util.Entry{
415+
Path: "lima-guestagent",
416+
Reader: guestAgent,
417+
})
418+
}
422419

423420
if nerdctlArchive != "" {
424421
nftgz := args.Containerd.Archive

pkg/hostagent/hostagent.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,19 @@ type HostAgent struct {
7575
}
7676

7777
type options struct {
78-
nerdctlArchive string // local path, not URL
78+
guestAgentBinary string
79+
nerdctlArchive string // local path, not URL
7980
}
8081

8182
type Opt func(*options) error
8283

84+
func WithGuestAgentBinary(s string) Opt {
85+
return func(o *options) error {
86+
o.guestAgentBinary = s
87+
return nil
88+
}
89+
}
90+
8391
func WithNerdctlArchive(s string) Opt {
8492
return func(o *options) error {
8593
o.nerdctlArchive = s
@@ -134,7 +142,7 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
134142
if err := cidata.GenerateCloudConfig(inst.Dir, instName, inst.Config); err != nil {
135143
return nil, err
136144
}
137-
if err := cidata.GenerateISO9660(inst.Dir, instName, inst.Config, udpDNSLocalPort, tcpDNSLocalPort, o.nerdctlArchive, vSockPort, virtioPort); err != nil {
145+
if err := cidata.GenerateISO9660(inst.Dir, instName, inst.Config, udpDNSLocalPort, tcpDNSLocalPort, o.guestAgentBinary, o.nerdctlArchive, vSockPort, virtioPort); err != nil {
138146
return nil, err
139147
}
140148

pkg/instance/start.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/lima-vm/lima/pkg/driverutil"
2121
"github.com/lima-vm/lima/pkg/executil"
2222
"github.com/lima-vm/lima/pkg/osutil"
23+
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
2324
"github.com/mattn/go-isatty"
2425

2526
"github.com/lima-vm/lima/pkg/downloader"
@@ -72,11 +73,20 @@ func ensureNerdctlArchiveCache(ctx context.Context, y *limayaml.LimaYAML, create
7273

7374
type Prepared struct {
7475
Driver driver.Driver
76+
GuestAgent string
7577
NerdctlArchiveCache string
7678
}
7779

7880
// Prepare ensures the disk, the nerdctl archive, etc.
7981
func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
82+
var guestAgent string
83+
if !*inst.Config.Plain {
84+
var err error
85+
guestAgent, err = usrlocalsharelima.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
86+
if err != nil {
87+
return nil, err
88+
}
89+
}
8090
limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
8191
Instance: inst,
8292
})
@@ -104,6 +114,7 @@ func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
104114

105115
return &Prepared{
106116
Driver: limaDriver,
117+
GuestAgent: guestAgent,
107118
NerdctlArchiveCache: nerdctlArchiveCache,
108119
}, nil
109120
}
@@ -171,6 +182,9 @@ func Start(ctx context.Context, inst *store.Instance, limactl string, launchHost
171182
if prepared.Driver.CanRunGUI() {
172183
args = append(args, "--run-gui")
173184
}
185+
if prepared.GuestAgent != "" {
186+
args = append(args, "--guestagent", prepared.GuestAgent)
187+
}
174188
if prepared.NerdctlArchiveCache != "" {
175189
args = append(args, "--nerdctl-archive", prepared.NerdctlArchiveCache)
176190
}

0 commit comments

Comments
 (0)