Skip to content

Commit

Permalink
fix: unmarshal additional disks (#613)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ginglis13 authored Oct 6, 2023
1 parent c6a4d9a commit c3d95ac
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pkg/config/lima_config_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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",
})
Expand Down Expand Up @@ -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)
}

0 comments on commit c3d95ac

Please sign in to comment.