Skip to content

Commit 3c60401

Browse files
committed
VirtualBox: support mounting additional disks
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
1 parent c3d5292 commit 3c60401

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

pkg/cidata/cidata.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
235235
args.MountType = "virtiofs"
236236
}
237237

238+
switch *y.VMType {
239+
case limayaml.QEMU:
240+
args.DiskType = "virtio"
241+
case limayaml.VZ:
242+
args.DiskType = "virtio"
243+
case limayaml.VBOX:
244+
args.DiskType = "sata"
245+
}
246+
238247
for i, d := range y.AdditionalDisks {
239248
format := true
240249
if d.Format != nil {
@@ -246,7 +255,7 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
246255
}
247256
args.Disks = append(args.Disks, Disk{
248257
Name: d.Name,
249-
Device: diskDeviceNameFromOrder(i),
258+
Device: diskDeviceNameFromOrder(args.DiskType, i),
250259
Format: format,
251260
FSType: fstype,
252261
FSArgs: d.FSArgs,
@@ -413,7 +422,10 @@ func getBootCmds(p []limayaml.Provision) []BootCmds {
413422
return bootCmds
414423
}
415424

416-
func diskDeviceNameFromOrder(order int) string {
425+
func diskDeviceNameFromOrder(diskType string, order int) string {
426+
if diskType == "sata" {
427+
return fmt.Sprintf("sd%c", int('b')+order)
428+
} // diskType == "virtio"
417429
return fmt.Sprintf("vd%c", int('b')+order)
418430
}
419431

pkg/cidata/template.go

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type TemplateArgs struct {
6262
Mounts []Mount
6363
MountType string
6464
Disks []Disk
65+
DiskType string
6566
GuestInstallPrefix string
6667
Containerd Containerd
6768
Networks []Network

pkg/vbox/vbox_driver.go

+36
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/lima-vm/lima/pkg/driver"
1616
"github.com/lima-vm/lima/pkg/iso9660util"
1717
"github.com/lima-vm/lima/pkg/limayaml"
18+
"github.com/lima-vm/lima/pkg/store"
1819
"github.com/lima-vm/lima/pkg/store/filenames"
1920
"github.com/sirupsen/logrus"
2021
)
@@ -61,6 +62,30 @@ func (l *LimaVBoxDriver) create(ctx context.Context, name string) error {
6162

6263
baseDisk := filepath.Join(l.Instance.Dir, filenames.BaseDisk)
6364
diffDisk := filepath.Join(l.Instance.Dir, filenames.DiffDisk)
65+
extraDisks := []string{}
66+
if len(l.Instance.AdditionalDisks) > 0 {
67+
for _, d := range l.Instance.AdditionalDisks {
68+
diskName := d.Name
69+
disk, err := store.InspectDisk(diskName)
70+
if err != nil {
71+
logrus.Errorf("could not load disk %q: %q", diskName, err)
72+
return err
73+
}
74+
75+
if disk.Instance != "" {
76+
logrus.Errorf("could not attach disk %q, in use by instance %q", diskName, disk.Instance)
77+
return err
78+
}
79+
logrus.Infof("Mounting disk %q on %q", diskName, disk.MountPoint)
80+
err = disk.Lock(l.Instance.Dir)
81+
if err != nil {
82+
logrus.Errorf("could not lock disk %q: %q", diskName, err)
83+
return err
84+
}
85+
dataDisk := filepath.Join(disk.Dir, filenames.DataDisk)
86+
extraDisks = append(extraDisks, dataDisk)
87+
}
88+
}
6489
isBaseDiskISO, err := iso9660util.IsISO9660(baseDisk)
6590
if err != nil {
6691
return err
@@ -128,6 +153,17 @@ func (l *LimaVBoxDriver) create(ctx context.Context, name string) error {
128153
logrus.Debugf("diffdisk %v", err)
129154
return err
130155
}
156+
for i, extraDisk := range extraDisks {
157+
if err := exec.CommandContext(ctx, qExe, "storageattach", name,
158+
"--storagectl", "SATA",
159+
"--port", "3",
160+
"--device", fmt.Sprintf("%d", i),
161+
"--type", "hdd",
162+
"--medium", extraDisk).Run(); err != nil {
163+
logrus.Debugf("extradisk %v", err)
164+
return err
165+
}
166+
}
131167

132168
if err := exec.CommandContext(ctx, qExe, "storageattach", name,
133169
"--storagectl", "SATA",

0 commit comments

Comments
 (0)