Skip to content

Commit 4af459c

Browse files
committed
Allow passing guestagent location to instance.StartWithPath
This is similar to passing in the full path to limactl to allow an embedding application stored in a different location to launch a VM. Simplified the internal calls by removing the limactl argument to instance.Start. Also changed the order to look for external drivers first in LIMA_DRIVERS_PATH before looking in libexec, allowing the user to override the drivers in the default location. This also makes sure that the LIMA_DRIVERS_PATH drivers are found even when libexec cannot be located. Signed-off-by: Jan Dubois <jan.dubois@suse.com>
1 parent 9d5f469 commit 4af459c

File tree

8 files changed

+43
-38
lines changed

8 files changed

+43
-38
lines changed

cmd/limactl/clone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func cloneAction(cmd *cobra.Command, args []string) error {
113113
if err != nil {
114114
return err
115115
}
116-
return instance.Start(ctx, newInst, "", false, false)
116+
return instance.Start(ctx, newInst, false, false)
117117
}
118118

119119
func cloneBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {

cmd/limactl/edit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func editAction(cmd *cobra.Command, args []string) error {
166166
if err != nil {
167167
return err
168168
}
169-
return instance.Start(ctx, inst, "", false, false)
169+
return instance.Start(ctx, inst, false, false)
170170
}
171171

172172
func askWhetherToStart() (bool, error) {

cmd/limactl/shell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
106106
return err
107107
}
108108

109-
err = instance.Start(ctx, inst, "", false, false)
109+
err = instance.Start(ctx, inst, false, false)
110110
if err != nil {
111111
return err
112112
}

cmd/limactl/start.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ func createAction(cmd *cobra.Command, args []string) error {
548548
if len(inst.Errors) > 0 {
549549
return fmt.Errorf("errors inspecting instance: %+v", inst.Errors)
550550
}
551-
if _, err = instance.Prepare(cmd.Context(), inst); err != nil {
551+
if _, err = instance.Prepare(cmd.Context(), inst, ""); err != nil {
552552
return err
553553
}
554554
logrus.Infof("Run `limactl start %s` to start the instance.", inst.Name)
@@ -606,7 +606,7 @@ func startAction(cmd *cobra.Command, args []string) error {
606606
return err
607607
}
608608

609-
return instance.Start(ctx, inst, "", launchHostAgentForeground, progress)
609+
return instance.Start(ctx, inst, launchHostAgentForeground, progress)
610610
}
611611

612612
func createBashComplete(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {

pkg/instance/restart.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func Restart(ctx context.Context, inst *limatype.Instance, showProgress bool) er
2525
return err
2626
}
2727

28-
if err := Start(ctx, inst, "", launchHostAgentForeground, showProgress); err != nil {
28+
if err := Start(ctx, inst, launchHostAgentForeground, showProgress); err != nil {
2929
return err
3030
}
3131

@@ -40,7 +40,7 @@ func RestartForcibly(ctx context.Context, inst *limatype.Instance, showProgress
4040
return err
4141
}
4242

43-
if err := Start(ctx, inst, "", launchHostAgentForeground, showProgress); err != nil {
43+
if err := Start(ctx, inst, launchHostAgentForeground, showProgress); err != nil {
4444
return err
4545
}
4646

pkg/instance/start.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ type Prepared struct {
4444
}
4545

4646
// Prepare ensures the disk, the nerdctl archive, etc.
47-
func Prepare(ctx context.Context, inst *limatype.Instance) (*Prepared, error) {
48-
var guestAgent string
49-
if !*inst.Config.Plain {
47+
func Prepare(ctx context.Context, inst *limatype.Instance, guestAgent string) (*Prepared, error) {
48+
if !*inst.Config.Plain && guestAgent == "" {
5049
var err error
5150
guestAgent, err = usrlocalsharelima.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
5251
if err != nil {
@@ -131,20 +130,24 @@ func Prepare(ctx context.Context, inst *limatype.Instance) (*Prepared, error) {
131130
}, nil
132131
}
133132

134-
// Start starts the hostagent in the background, which in turn will start the instance.
135-
// Start will listen to hostagent events and log them to STDOUT until either the instance
133+
// StartWithPaths starts the hostagent in the background, which in turn will start the instance.
134+
// StartWithPaths will listen to hostagent events and log them to STDOUT until either the instance
136135
// is running, or has failed to start.
137136
//
138-
// The `limactl` argument allows the caller to specify the full path of the `limactl` executable.
139-
// When called from inside limactl itself it will always be the empty string which uses the name
140-
// of the current executable instead.
141-
//
142-
// The `launchHostAgentForeground` argument makes the hostagent run in the foreground.
137+
// The launchHostAgentForeground argument makes the hostagent run in the foreground.
143138
// The function will continue to listen and log hostagent events until the instance is
144139
// shut down again.
145140
//
146-
// Start calls Prepare by itself, so you do not need to call Prepare manually before calling Start.
147-
func Start(ctx context.Context, inst *limatype.Instance, limactl string, launchHostAgentForeground, showProgress bool) error {
141+
// The showProgress argument tells the hostagent to show provision script progress by tailing cloud-init logs.
142+
//
143+
// The limactl argument allows the caller to specify the full path of the limactl executable.
144+
// The guestAgent argument allows the caller to specify the full path of the guest agent executable.
145+
// Inside limactl this function is only called by Start, which passes empty strings for both
146+
// limactl and guestAgent, in which case the location of the current executable is used for
147+
// limactl and the guest agent is located from the corresponding <prefix>/share/lima directory.
148+
//
149+
// StartWithPaths calls Prepare by itself, so you do not need to call Prepare manually before calling Start.
150+
func StartWithPaths(ctx context.Context, inst *limatype.Instance, launchHostAgentForeground, showProgress bool, limactl, guestAgent string) error {
148151
haPIDPath := filepath.Join(inst.Dir, filenames.HostAgentPID)
149152
if _, err := os.Stat(haPIDPath); !errors.Is(err, os.ErrNotExist) {
150153
return fmt.Errorf("instance %q seems running (hint: remove %q if the instance is not actually running)", inst.Name, haPIDPath)
@@ -153,7 +156,7 @@ func Start(ctx context.Context, inst *limatype.Instance, limactl string, launchH
153156

154157
haSockPath := filepath.Join(inst.Dir, filenames.HostAgentSock)
155158

156-
prepared, err := Prepare(ctx, inst)
159+
prepared, err := Prepare(ctx, inst, guestAgent)
157160
if err != nil {
158161
return err
159162
}
@@ -251,6 +254,10 @@ func Start(ctx context.Context, inst *limatype.Instance, limactl string, launchH
251254
}
252255
}
253256

257+
func Start(ctx context.Context, inst *limatype.Instance, launchHostAgentForeground, showProgress bool) error {
258+
return StartWithPaths(ctx, inst, launchHostAgentForeground, showProgress, "", "")
259+
}
260+
254261
func waitHostAgentStart(_ context.Context, haPIDPath, haStderrPath string) error {
255262
begin := time.Now()
256263
deadlineDuration := 5 * time.Second

pkg/registry/registry.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@ func CheckInternalOrExternal(name string) string {
7070
}
7171

7272
func Get(name string) (*ExternalDriver, driver.Driver, bool) {
73-
if err := discoverDrivers(); err != nil {
74-
logrus.Warnf("Error discovering drivers: %v", err)
75-
}
76-
7773
internalDriver, exists := internalDrivers[name]
7874
if !exists {
75+
if err := discoverDrivers(); err != nil {
76+
logrus.Warnf("Error discovering drivers: %v", err)
77+
}
7978
externalDriver, exists := ExternalDrivers[name]
8079
if exists {
8180
return externalDriver, nil, exists
@@ -102,18 +101,6 @@ func registerExternalDriver(name, path string) {
102101
}
103102

104103
func discoverDrivers() error {
105-
stdDriverDir, err := usrlocalsharelima.LibexecLima()
106-
if err != nil {
107-
return err
108-
}
109-
110-
logrus.Debugf("Discovering external drivers in %s", stdDriverDir)
111-
if _, err := os.Stat(stdDriverDir); err == nil {
112-
if err := discoverDriversInDir(stdDriverDir); err != nil {
113-
logrus.Warnf("Error discovering external drivers in %q: %v", stdDriverDir, err)
114-
}
115-
}
116-
117104
if driverPaths := os.Getenv("LIMA_DRIVERS_PATH"); driverPaths != "" {
118105
paths := filepath.SplitList(driverPaths)
119106
for _, path := range paths {
@@ -137,6 +124,17 @@ func discoverDrivers() error {
137124
}
138125
}
139126

127+
stdDriverDir, err := usrlocalsharelima.LibexecLima()
128+
if err != nil {
129+
return err
130+
}
131+
132+
logrus.Debugf("Discovering external drivers in %s", stdDriverDir)
133+
if _, err := os.Stat(stdDriverDir); err == nil {
134+
if err := discoverDriversInDir(stdDriverDir); err != nil {
135+
logrus.Warnf("Error discovering external drivers in %q: %v", stdDriverDir, err)
136+
}
137+
}
140138
return nil
141139
}
142140

website/content/en/docs/dev/drivers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ This creates external driver binaries in `_output/libexec/lima/` with the naming
4040

4141
Lima discovers external drivers from these locations:
4242

43-
1. **Standard directory**: `<LIMA-PREFIX>/libexec/lima/`, where `<LIMA_PREFIX>` is the location path where the Lima binary is present
44-
2. **Custom directories**: Set path to the external driver's directory via `LIMA_DRIVERS_PATH` environment variable
43+
1. **Custom directories**: Set path to the external driver's directory via `LIMA_DRIVERS_PATH` environment variable
44+
2. **Standard directory**: `<LIMA-PREFIX>/libexec/lima/`, where `<LIMA_PREFIX>` is the location path where the Lima binary is present
4545

4646
The discovery process is handled by [`pkg/registry/registry.go`.](https://github.com/lima-vm/lima/blob/master/pkg/registry/registry.go)
4747

0 commit comments

Comments
 (0)