Skip to content

Commit a6e17e9

Browse files
committed
Store Lima version in the instance directory
That way future Lima releases can make decisions about default settings based on the Lima version that created the instance instead of the current instance, and settings for existing instances will not change just because the user updated Lima. Signed-off-by: Jan Dubois <[email protected]>
1 parent f3dc6ed commit a6e17e9

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

cmd/limactl/start.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/lima-vm/lima/pkg/store/filenames"
2424
"github.com/lima-vm/lima/pkg/templatestore"
2525
"github.com/lima-vm/lima/pkg/uiutil"
26+
"github.com/lima-vm/lima/pkg/version"
2627
"github.com/lima-vm/lima/pkg/yqutil"
2728
"github.com/sirupsen/logrus"
2829
"github.com/spf13/cobra"
@@ -332,6 +333,9 @@ func createInstance(ctx context.Context, st *creatorState, saveBrokenEditorBuffe
332333
if err := os.WriteFile(filePath, st.yBytes, 0o644); err != nil {
333334
return nil, err
334335
}
336+
if err := os.WriteFile(filepath.Join(instDir, filenames.LimaVersion), []byte(version.Version), 0o644); err != nil {
337+
return nil, err
338+
}
335339

336340
inst, err := store.Inspect(st.instName)
337341
if err != nil {

pkg/store/filenames/filenames.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727

2828
const (
2929
LimaYAML = "lima.yaml"
30+
LimaVersion = "lima-version" // Lima version used to create instance
3031
CIDataISO = "cidata.iso"
3132
CIDataISODir = "cidata"
3233
BaseDisk = "basedisk"

pkg/store/instance.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"github.com/sirupsen/logrus"
78
"io"
89
"os"
910
"os/user"
@@ -16,6 +17,7 @@ import (
1617
"text/template"
1718
"time"
1819

20+
"github.com/coreos/go-semver/semver"
1921
"github.com/docker/go-units"
2022
hostagentclient "github.com/lima-vm/lima/pkg/hostagent/api/client"
2123
"github.com/lima-vm/lima/pkg/limayaml"
@@ -35,6 +37,8 @@ const (
3537
StatusRunning Status = "Running"
3638
)
3739

40+
const DefaultLimaVersion = "v0.19.1"
41+
3842
type Instance struct {
3943
Name string `json:"name"`
4044
Status Status `json:"status"`
@@ -56,6 +60,7 @@ type Instance struct {
5660
Config *limayaml.LimaYAML `json:"config,omitempty"`
5761
SSHAddress string `json:"sshAddress,omitempty"`
5862
Protected bool `json:"protected"`
63+
LimaVersion string `json:"limaVersion"`
5964
}
6065

6166
func (inst *Instance) LoadYAML() (*limayaml.LimaYAML, error) {
@@ -167,6 +172,14 @@ func Inspect(instName string) (*Instance, error) {
167172
}
168173
}
169174
}
175+
if version, err := os.ReadFile(filepath.Join(instDir, filenames.LimaVersion)); err == nil {
176+
inst.LimaVersion = strings.TrimSpace(string(version))
177+
} else {
178+
if !errors.Is(err, os.ErrNotExist) {
179+
inst.Errors = append(inst.Errors, err)
180+
}
181+
inst.LimaVersion = DefaultLimaVersion
182+
}
170183
return inst, nil
171184
}
172185

@@ -423,3 +436,22 @@ func (inst *Instance) Unprotect() error {
423436
inst.Protected = false
424437
return nil
425438
}
439+
440+
// MinVersion checks if the instance has been created by the specified minimum version of Lima.
441+
// Prerelease versions are treated as the previous version, not the next, so "0.19.1-16-gf3dc6ed.m"
442+
// is considered the same as "0.19.1" and not "0.20.0".
443+
func MinVersion(instanceVersion, minVersion string) bool {
444+
v := strings.TrimPrefix(instanceVersion, "v")
445+
v, _, _ = strings.Cut(v, "-")
446+
instVers, err := semver.NewVersion(v)
447+
if err != nil {
448+
logrus.Warnf("cannot parse instance version %q", instanceVersion)
449+
return false
450+
}
451+
minVers, err := semver.NewVersion(minVersion)
452+
if err != nil {
453+
logrus.Warnf("cannot parse minimum version %q", minVersion)
454+
return false
455+
}
456+
return !instVers.LessThan(*minVers)
457+
}

0 commit comments

Comments
 (0)