Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vz: implement auto save/restore #2900

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,32 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
y.NestedVirtualization = ptr.Of(false)
}

if y.SaveOnStop == nil {
y.SaveOnStop = d.SaveOnStop
}
if o.SaveOnStop != nil {
y.SaveOnStop = o.SaveOnStop
}
if y.SaveOnStop == nil {
y.SaveOnStop = ptr.Of(false)
}
if *y.SaveOnStop {
y.SaveOnStop = ptr.Of(false)
if *y.VMType != VZ {
logrus.Warn("saveOnStop is only supported for VM type vz")
} else if runtime.GOARCH != "arm64" {
logrus.Warn("saveOnStop is only supported for arm64 VM type vz")
} else if runtime.GOOS != "darwin" {
logrus.Warn("saveOnStop is only supported on macOS")
} else if macOSProductVersion, err := osutil.ProductVersion(); err != nil {
logrus.WithError(err).Warn("Failed to get macOS product version")
} else if macOSProductVersion.LessThan(*semver.New("14.0.0")) {
logrus.Warn("saveOnStop is not supported on macOS prior to 14.0")
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will more clear if we extract all the checks to a helper like:

if *y.SaveOnStop {
    y.SaveOnStop = validateSaveOnStop(y)
}

In the helper can do something like:

func validateSaveOnStop() *bool {
    notSupported := ptr.Of(false)
    if condition {
        log ...
        return notSupported
    }
    if other condition {
        log ...
        return notSupported
    }
    ...
    return ptr.Of(true)
}

y.SaveOnStop = ptr.Of(true)
}
}

if y.Plain == nil {
y.Plain = d.Plain
}
Expand All @@ -878,6 +904,7 @@ func fixUpForPlainMode(y *LimaYAML) {
y.Containerd.User = ptr.Of(false)
y.Rosetta.BinFmt = ptr.Of(false)
y.Rosetta.Enabled = ptr.Of(false)
y.SaveOnStop = ptr.Of(false)
y.TimeZone = ptr.Of("")
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/limayaml/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func TestFillDefault(t *testing.T) {
RemoveDefaults: ptr.Of(false),
},
NestedVirtualization: ptr.Of(false),
SaveOnStop: ptr.Of(false),
Plain: ptr.Of(false),
User: User{
Name: ptr.Of(user.Username),
Expand Down Expand Up @@ -437,6 +438,7 @@ func TestFillDefault(t *testing.T) {
BinFmt: ptr.Of(true),
},
NestedVirtualization: ptr.Of(true),
SaveOnStop: ptr.Of(true),
User: User{
Name: ptr.Of("xxx"),
Comment: ptr.Of("Foo Bar"),
Expand Down Expand Up @@ -474,11 +476,13 @@ func TestFillDefault(t *testing.T) {
Enabled: ptr.Of(true),
BinFmt: ptr.Of(true),
}
expect.SaveOnStop = ptr.Of(true)
} else {
expect.Rosetta = Rosetta{
Enabled: ptr.Of(false),
BinFmt: ptr.Of(true),
}
expect.SaveOnStop = ptr.Of(false)
}
expect.Plain = ptr.Of(false)

Expand Down Expand Up @@ -660,6 +664,7 @@ func TestFillDefault(t *testing.T) {
BinFmt: ptr.Of(false),
},
NestedVirtualization: ptr.Of(false),
SaveOnStop: ptr.Of(false),
User: User{
Name: ptr.Of("foo"),
Comment: ptr.Of("foo bar baz"),
Expand Down Expand Up @@ -723,6 +728,7 @@ func TestFillDefault(t *testing.T) {
expect.Plain = ptr.Of(false)

expect.NestedVirtualization = ptr.Of(false)
expect.SaveOnStop = ptr.Of(false)

FillDefault(&y, &d, &o, filePath, false)
assert.DeepEqual(t, &y, &expect, opts...)
Expand Down
1 change: 1 addition & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type LimaYAML struct {
TimeZone *string `yaml:"timezone,omitempty" json:"timezone,omitempty" jsonschema:"nullable"`
NestedVirtualization *bool `yaml:"nestedVirtualization,omitempty" json:"nestedVirtualization,omitempty" jsonschema:"nullable"`
User User `yaml:"user,omitempty" json:"user,omitempty"`
SaveOnStop *bool `yaml:"saveOnStop,omitempty" json:"saveOnStop,omitempty" jsonschema:"nullable"`
}

type (
Expand Down
24 changes: 19 additions & 5 deletions pkg/vz/vz_driver_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var knownYamlProperties = []string{
"PropagateProxyEnv",
"Provision",
"Rosetta",
"SaveOnStop",
"SSH",
"TimeZone",
"UpgradePackages",
Expand All @@ -67,11 +68,22 @@ type LimaVzDriver struct {
*driver.BaseDriver

machine *virtualMachineWrapper

// Runtime configuration
config LimaVzDriverRuntimeConfig
}

type LimaVzDriverRuntimeConfig struct {
// SaveOnStop is a flag to save the VM state on stop
SaveOnStop bool `json:"saveOnStop"`
}

func New(driver *driver.BaseDriver) *LimaVzDriver {
return &LimaVzDriver{
BaseDriver: driver,
config: LimaVzDriverRuntimeConfig{
SaveOnStop: *driver.Instance.Config.SaveOnStop,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preparing for more config options?

}
}

Expand Down Expand Up @@ -193,11 +205,13 @@ func (l *LimaVzDriver) RunGUI() error {
}

func (l *LimaVzDriver) Stop(_ context.Context) error {
machineStatePath := filepath.Join(l.Instance.Dir, filenames.VzMachineState)
if err := saveVM(l.machine.VirtualMachine, machineStatePath); err != nil {
logrus.WithError(err).Warn("Failed to save VZ. Falling back to shutdown")
} else {
return nil
if l.config.SaveOnStop {
machineStatePath := filepath.Join(l.Instance.Dir, filenames.VzMachineState)
if err := saveVM(l.machine.VirtualMachine, machineStatePath); err != nil {
logrus.WithError(err).Warn("Failed to save VZ. Falling back to shutdown")
} else {
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code to add saveOnStop must be added before you change Stop(). Each commit should be correct so we can easily bisect issues.

The order of commits should be:

  • Add saveOnStop option (it can do nothing at this point)
  • Save when stopping, considering the new option

If you don't want to add unused option, add the internal flag defaulting to false in the first commit, and expose the option in the second.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code to add saveOnStop must be added before you change Stop().

That should already be the case. Could you double-check to ensure you're not looking at an outdated commit?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still the case - the first commit ignore the option added in the second commit. If we need to revert the second commit we change the behavior to always save.

}

logrus.Info("Shutting down VZ")
Expand Down
5 changes: 5 additions & 0 deletions templates/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,11 @@ hostResolver:
# 🟢 Builtin default: /usr/local
guestInstallPrefix: null

# When "saveOnStop" is enabled, Lima saves the VM instead of shutting it down.
# The VM can be restored with `limactl start` if the saved state is available.
# 🟢 Builtin default: false
saveOnStop: null

# When the "plain" mode is enabled:
# - the YAML properties for mounts, port forwarding, containerd, etc. will be ignored
# - guest agent will not be running
Expand Down