From c3d95acf373c6ca0f1fab81ff4f77397be34d1ab Mon Sep 17 00:00:00 2001 From: Gavin Inglis <43075615+ginglis13@users.noreply.github.com> Date: Fri, 6 Oct 2023 22:36:20 +0000 Subject: [PATCH] fix: unmarshal additional disks (#613) Issue #, if available: *Description of changes:* disks in the list format in lima override.yaml were not being unmarshalled properly, causing a disk with the name "" to be mixed with finch lima yaml, resulting in startup failure. This only occurs on inits of a vm other than the very first init. init a vm the first time, _output/lima/data/_config/override.yaml: ```yaml additionalDisks: - name: finch ``` stop and remove the vm, then re-init: ```yaml additionalDisks: - name: "" - name: finch ``` to fix, copy upstream logic from lima to use a custom unmarshaler for the Disk type. *Testing done:* `make test-unit` manual testing - [x] I've reviewed the guidance in CONTRIBUTING.md #### License Acceptance By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. Signed-off-by: Gavin Inglis --- pkg/config/lima_config_applier.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/config/lima_config_applier.go b/pkg/config/lima_config_applier.go index 95743ce43..db92e1168 100644 --- a/pkg/config/lima_config_applier.go +++ b/pkg/config/lima_config_applier.go @@ -7,6 +7,7 @@ import ( "fmt" "strings" + goyaml "github.com/goccy/go-yaml" "github.com/lima-vm/lima/pkg/limayaml" "github.com/spf13/afero" "github.com/xorcare/pointer" @@ -102,6 +103,12 @@ func (lca *limaConfigApplier) Apply(isInit bool) error { return fmt.Errorf("failed to unmarshal the lima config file: %w", err) } + // Unmarshall with custom unmarshaler for Disk: + // https://github.com/lima-vm/lima/blob/v0.17.2/pkg/limayaml/load.go#L16 + if err := goyaml.UnmarshalWithOptions(b, &limaCfg, goyaml.DisallowDuplicateKey(), goyaml.CustomUnmarshaler[limayaml.Disk](unmarshalDisk)); err != nil { + return fmt.Errorf("failed to unmarshal the lima config file: %w", err) + } + limaCfg.CPUs = lca.cfg.CPUs limaCfg.Memory = lca.cfg.Memory limaCfg.Mounts = []limayaml.Mount{} @@ -141,7 +148,7 @@ func (lca *limaConfigApplier) Apply(isInit bool) error { toggleSnaphotters(&limaCfg, snapshotters) - if *lca.cfg.VMType != "wsl2" { + if *lca.cfg.VMType != "wsl2" && len(limaCfg.AdditionalDisks) == 0 { limaCfg.AdditionalDisks = append(limaCfg.AdditionalDisks, limayaml.Disk{ Name: "finch", }) @@ -244,3 +251,13 @@ func findWslDiskFormatScript(limaCfg *limayaml.LimaYAML) bool { return hasWslDiskFormatScript } + +// https://github.com/lima-vm/lima/blob/v0.17.2/pkg/limayaml/load.go#L16 +func unmarshalDisk(dst *limayaml.Disk, b []byte) error { + var s string + if err := goyaml.Unmarshal(b, &s); err == nil { + *dst = limayaml.Disk{Name: s} + return nil + } + return goyaml.Unmarshal(b, dst) +}