Skip to content

Commit 643bfcd

Browse files
authored
Merge pull request #1677 from AkihiroSuda/limactl-create
limactl create: prepare images
2 parents bd7048f + a415ffa commit 643bfcd

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

cmd/limactl/start.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,10 @@ func createAction(cmd *cobra.Command, args []string) error {
437437
if len(inst.Errors) > 0 {
438438
return fmt.Errorf("errors inspecting instance: %+v", inst.Errors)
439439
}
440-
logrus.Infof("Run `limactl start %q` to start the instance.", inst.Name)
440+
if _, err = start.Prepare(cmd.Context(), inst); err != nil {
441+
return err
442+
}
443+
logrus.Infof("Run `limactl start %s` to start the instance.", inst.Name)
441444
return nil
442445
}
443446

pkg/start/start.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,16 @@ func ensureNerdctlArchiveCache(y *limayaml.LimaYAML) (string, error) {
5656
return "", fileutils.Errors(errs)
5757
}
5858

59-
func Start(ctx context.Context, inst *store.Instance) error {
60-
haPIDPath := filepath.Join(inst.Dir, filenames.HostAgentPID)
61-
if _, err := os.Stat(haPIDPath); !errors.Is(err, os.ErrNotExist) {
62-
return fmt.Errorf("instance %q seems running (hint: remove %q if the instance is not actually running)", inst.Name, haPIDPath)
63-
}
64-
65-
haSockPath := filepath.Join(inst.Dir, filenames.HostAgentSock)
59+
type Prepared struct {
60+
Driver driver.Driver
61+
NerdctlArchiveCache string
62+
}
6663

64+
// Prepare ensures the disk, the nerdctl archive, etc.
65+
func Prepare(_ context.Context, inst *store.Instance) (*Prepared, error) {
6766
y, err := inst.LoadYAML()
6867
if err != nil {
69-
return err
68+
return nil, err
7069
}
7170

7271
limaDriver := driverutil.CreateTargetDriverInstance(&driver.BaseDriver{
@@ -75,13 +74,34 @@ func Start(ctx context.Context, inst *store.Instance) error {
7574
})
7675

7776
if err := limaDriver.Validate(); err != nil {
78-
return err
77+
return nil, err
7978
}
8079

8180
if err := limaDriver.CreateDisk(); err != nil {
82-
return err
81+
return nil, err
8382
}
8483
nerdctlArchiveCache, err := ensureNerdctlArchiveCache(y)
84+
if err != nil {
85+
return nil, err
86+
}
87+
88+
return &Prepared{
89+
Driver: limaDriver,
90+
NerdctlArchiveCache: nerdctlArchiveCache,
91+
}, nil
92+
}
93+
94+
// Start starts the instance.
95+
// Start calls Prepare by itself, so you do not need to call Prepare manually before calling Start.
96+
func Start(ctx context.Context, inst *store.Instance) error {
97+
haPIDPath := filepath.Join(inst.Dir, filenames.HostAgentPID)
98+
if _, err := os.Stat(haPIDPath); !errors.Is(err, os.ErrNotExist) {
99+
return fmt.Errorf("instance %q seems running (hint: remove %q if the instance is not actually running)", inst.Name, haPIDPath)
100+
}
101+
102+
haSockPath := filepath.Join(inst.Dir, filenames.HostAgentSock)
103+
104+
prepared, err := Prepare(ctx, inst)
85105
if err != nil {
86106
return err
87107
}
@@ -117,11 +137,11 @@ func Start(ctx context.Context, inst *store.Instance) error {
117137
"hostagent",
118138
"--pidfile", haPIDPath,
119139
"--socket", haSockPath)
120-
if limaDriver.CanRunGUI() {
140+
if prepared.Driver.CanRunGUI() {
121141
args = append(args, "--run-gui")
122142
}
123-
if nerdctlArchiveCache != "" {
124-
args = append(args, "--nerdctl-archive", nerdctlArchiveCache)
143+
if prepared.NerdctlArchiveCache != "" {
144+
args = append(args, "--nerdctl-archive", prepared.NerdctlArchiveCache)
125145
}
126146
args = append(args, inst.Name)
127147
haCmd := exec.CommandContext(ctx, self, args...)

0 commit comments

Comments
 (0)