Skip to content

Commit 18addd2

Browse files
committed
Allow extra parameters for additional disks
Default to a simple string with name, just like before. Signed-off-by: Anders F Björklund <[email protected]>
1 parent f682bd5 commit 18addd2

File tree

11 files changed

+75
-26
lines changed

11 files changed

+75
-26
lines changed

cmd/limactl/disk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func diskDeleteAction(cmd *cobra.Command, args []string) error {
245245
for _, inst := range instances {
246246
if len(inst.AdditionalDisks) > 0 {
247247
for _, d := range inst.AdditionalDisks {
248-
if d == diskName {
248+
if d.Name == diskName {
249249
refInstances = append(refInstances, inst.Name)
250250
}
251251
}

cmd/limactl/stop.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ func stopInstanceForcibly(inst *store.Instance) {
113113
logrus.Infof("The %s driver process seems already stopped", inst.VMType)
114114
}
115115

116-
for _, diskName := range inst.AdditionalDisks {
116+
for _, d := range inst.AdditionalDisks {
117+
diskName := d.Name
117118
disk, err := store.InspectDisk(diskName)
118119
if err != nil {
119120
logrus.Warnf("Disk %q does not exist", diskName)

examples/default.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ mountType: null
105105
# `/mnt/lima-${VOLUME}`.
106106
# 🟢 Builtin default: null
107107
additionalDisks:
108-
# disks should be a list of disk name strings, for example:
108+
# disks should either be a list of disk name strings, for example:
109109
# - "data"
110+
# or a list of disk objects with extra parameters, for example:
111+
# - name: "data"
110112

111113
ssh:
112114
# A localhost port of the host. Forwarded to port 22 of the guest.

pkg/cidata/cidata.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
203203
args.MountType = "virtiofs"
204204
}
205205

206-
for i, disk := range y.AdditionalDisks {
206+
for i, d := range y.AdditionalDisks {
207207
args.Disks = append(args.Disks, Disk{
208-
Name: disk,
208+
Name: d.Name,
209209
Device: diskDeviceNameFromOrder(i),
210210
})
211211
}

pkg/hostagent/hostagent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ sudo chown -R "${USER}" /run/host-services`
436436
a.onClose = append(a.onClose, func() error {
437437
var unlockMErr error
438438
for _, d := range a.y.AdditionalDisks {
439-
disk, inspectErr := store.InspectDisk(d)
439+
disk, inspectErr := store.InspectDisk(d.Name)
440440
if inspectErr != nil {
441441
unlockMErr = multierror.Append(unlockMErr, inspectErr)
442442
continue

pkg/limayaml/defaults_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func TestFillDefault(t *testing.T) {
277277
Memory: pointer.String("5GiB"),
278278
Disk: pointer.String("105GiB"),
279279
AdditionalDisks: []Disk{
280-
"data",
280+
{Name: "data"},
281281
},
282282
GuestInstallPrefix: pointer.String("/opt"),
283283
Containerd: Containerd{
@@ -413,7 +413,7 @@ func TestFillDefault(t *testing.T) {
413413

414414
y = filledDefaults
415415
y.DNS = []net.IP{net.ParseIP("8.8.8.8")}
416-
y.AdditionalDisks = []Disk{"overridden"}
416+
y.AdditionalDisks = []Disk{{Name: "overridden"}}
417417

418418
expect = y
419419

@@ -454,7 +454,7 @@ func TestFillDefault(t *testing.T) {
454454
Memory: pointer.String("7GiB"),
455455
Disk: pointer.String("117GiB"),
456456
AdditionalDisks: []Disk{
457-
"test",
457+
{Name: "test"},
458458
},
459459
GuestInstallPrefix: pointer.String("/usr"),
460460
Containerd: Containerd{

pkg/limayaml/limayaml.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ type Image struct {
7979
Initrd *File `yaml:"initrd,omitempty" json:"initrd,omitempty"`
8080
}
8181

82-
type Disk = string
82+
type Disk struct {
83+
Name string `yaml:"name" json:"name"` // REQUIRED
84+
}
8385

8486
type Mount struct {
8587
Location string `yaml:"location" json:"location"` // REQUIRED

pkg/limayaml/load.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,36 @@ import (
1313
yamlv3 "gopkg.in/yaml.v3"
1414
)
1515

16+
func unmarshalDisk(dst *Disk, b []byte) error {
17+
var s string
18+
if err := yaml.Unmarshal(b, &s); err == nil {
19+
*dst = Disk{Name: s}
20+
return nil
21+
}
22+
return yaml.Unmarshal(b, dst)
23+
}
24+
25+
func (d *Disk) UnmarshalYAML(value *yamlv3.Node) error {
26+
var v interface{}
27+
if err := value.Decode(&v); err != nil {
28+
return err
29+
}
30+
if s, ok := v.(string); ok {
31+
*d = Disk{Name: s}
32+
}
33+
return nil
34+
}
35+
1636
func unmarshalYAML(data []byte, v interface{}, comment string) error {
17-
if err := yaml.UnmarshalWithOptions(data, v, yaml.DisallowDuplicateKey()); err != nil {
37+
if err := yaml.UnmarshalWithOptions(data, v, yaml.DisallowDuplicateKey(), yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil {
1838
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err)
1939
}
2040
// the go-yaml library doesn't catch all markup errors, unfortunately
2141
// make sure to get a "second opinion", using the same library as "yq"
2242
if err := yamlv3.Unmarshal(data, v); err != nil {
2343
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err)
2444
}
25-
if err := yaml.UnmarshalWithOptions(data, v, yaml.Strict()); err != nil {
45+
if err := yaml.UnmarshalWithOptions(data, v, yaml.Strict(), yaml.CustomUnmarshaler[Disk](unmarshalDisk)); err != nil {
2646
logrus.WithField("comment", comment).WithError(err).Warn("Non-strict YAML is deprecated and will be unsupported in a future version of Lima")
2747
// Non-strict YAML is known to be used by Rancher Desktop:
2848
// https://github.com/rancher-sandbox/rancher-desktop/blob/c7ea7508a0191634adf16f4675f64c73198e8d37/src/backend/lima.ts#L114-L117

pkg/limayaml/load_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,25 @@ provision:
3030
_, err := Load([]byte(s), "error.yaml")
3131
assert.ErrorContains(t, err, "failed to unmarshal YAML")
3232
}
33+
34+
func TestLoadDiskString(t *testing.T) {
35+
s := `
36+
additionalDisks:
37+
- name
38+
`
39+
y, err := Load([]byte(s), "disk.yaml")
40+
assert.NilError(t, err)
41+
assert.Equal(t, len(y.AdditionalDisks), 1)
42+
assert.Equal(t, y.AdditionalDisks[0].Name, "name")
43+
}
44+
45+
func TestLoadDiskStruct(t *testing.T) {
46+
s := `
47+
additionalDisks:
48+
- name: "name"
49+
`
50+
y, err := Load([]byte(s), "disk.yaml")
51+
assert.NilError(t, err)
52+
assert.Assert(t, len(y.AdditionalDisks) == 1)
53+
assert.Equal(t, y.AdditionalDisks[0].Name, "name")
54+
}

pkg/qemu/qemu.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -587,24 +587,25 @@ func Cmdline(cfg Config) (string, []string, error) {
587587
diffDisk := filepath.Join(cfg.InstanceDir, filenames.DiffDisk)
588588
extraDisks := []string{}
589589
if len(y.AdditionalDisks) > 0 {
590-
for _, diskName := range y.AdditionalDisks {
591-
d, err := store.InspectDisk(diskName)
590+
for _, d := range y.AdditionalDisks {
591+
diskName := d.Name
592+
disk, err := store.InspectDisk(diskName)
592593
if err != nil {
593594
logrus.Errorf("could not load disk %q: %q", diskName, err)
594595
return "", nil, err
595596
}
596597

597-
if d.Instance != "" {
598-
logrus.Errorf("could not attach disk %q, in use by instance %q", diskName, d.Instance)
598+
if disk.Instance != "" {
599+
logrus.Errorf("could not attach disk %q, in use by instance %q", diskName, disk.Instance)
599600
return "", nil, err
600601
}
601-
logrus.Infof("Mounting disk %q on %q", diskName, d.MountPoint)
602-
err = d.Lock(cfg.InstanceDir)
602+
logrus.Infof("Mounting disk %q on %q", diskName, disk.MountPoint)
603+
err = disk.Lock(cfg.InstanceDir)
603604
if err != nil {
604605
logrus.Errorf("could not lock disk %q: %q", diskName, err)
605606
return "", nil, err
606607
}
607-
dataDisk := filepath.Join(d.Dir, filenames.DataDisk)
608+
dataDisk := filepath.Join(disk.Dir, filenames.DataDisk)
608609
extraDisks = append(extraDisks, dataDisk)
609610
}
610611
}

0 commit comments

Comments
 (0)