Skip to content

Commit 585d6e2

Browse files
authored
Merge pull request #1245 from AkihiroSuda/dev3
vz: fix nil pointer dereference with `mountType: reverse-sshfs`; validate disk format
2 parents 45cd9cf + 25cd97e commit 585d6e2

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

docs/vmtype.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Lima supports two ways of running guest machines:
44
- [qemu](#qemu)
55
- [vz](#vz)
66

7+
The vmType can be specified only on creating the instance.
8+
The vmType of existing instances cannot be changed.
9+
710
## QEMU
811
"qemu" option makes use of QEMU to run guest operating system.
912
This option is used by default if "vmType" is not set.

examples/default.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# so they can be overridden by the $LIMA_HOME/_config/default.yaml mechanism documented at the end of this file.
77

88
# VM type: "qemu" or "vz" (on macOS 13 and later).
9+
# The vmType can be specified only on creating the instance.
10+
# The vmType of existing instances cannot be changed.
911
# 🟢 Builtin default: "qemu"
1012
vmType: null
1113

pkg/vz/vm_darwin.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/lima-vm/lima/pkg/limayaml"
2121
"github.com/lima-vm/lima/pkg/localpathutil"
2222
"github.com/lima-vm/lima/pkg/networks"
23+
"github.com/lima-vm/lima/pkg/qemu/imgutil"
2324
"github.com/lima-vm/lima/pkg/store/filenames"
2425
"github.com/sirupsen/logrus"
2526
)
@@ -242,6 +243,18 @@ func attachNetwork(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigu
242243
return nil
243244
}
244245

246+
func validateDiskFormat(diskPath string) error {
247+
format, err := imgutil.DetectFormat(diskPath)
248+
if err != nil {
249+
return fmt.Errorf("failed to detect the format of %q: %w", diskPath, err)
250+
}
251+
if format != "raw" {
252+
return fmt.Errorf("expected the format of %q to be \"raw\", got %q", diskPath, format)
253+
}
254+
// TODO: ensure that the disk is formatted with GPT or ISO9660
255+
return nil
256+
}
257+
245258
func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfiguration) error {
246259
baseDiskPath := filepath.Join(driver.Instance.Dir, filenames.BaseDisk)
247260
diffDiskPath := filepath.Join(driver.Instance.Dir, filenames.DiffDisk)
@@ -253,6 +266,9 @@ func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigura
253266
var configurations []vz.StorageDeviceConfiguration
254267

255268
if isBaseDiskCDROM {
269+
if err = validateDiskFormat(baseDiskPath); err != nil {
270+
return err
271+
}
256272
baseDiskAttachment, err := vz.NewDiskImageStorageDeviceAttachment(baseDiskPath, true)
257273
if err != nil {
258274
return err
@@ -263,6 +279,9 @@ func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigura
263279
}
264280
configurations = append(configurations, baseDisk)
265281
}
282+
if err = validateDiskFormat(diffDiskPath); err != nil {
283+
return err
284+
}
266285
diffDiskAttachment, err := vz.NewDiskImageStorageDeviceAttachment(diffDiskPath, false)
267286
if err != nil {
268287
return err
@@ -273,6 +292,9 @@ func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigura
273292
}
274293
configurations = append(configurations, diffDisk)
275294

295+
if err = validateDiskFormat(ciDataPath); err != nil {
296+
return err
297+
}
276298
ciDataAttachment, err := vz.NewDiskImageStorageDeviceAttachment(ciDataPath, true)
277299
if err != nil {
278300
return err
@@ -335,7 +357,7 @@ func attachConsole(_ *driver.BaseDriver, vmConfig *vz.VirtualMachineConfiguratio
335357
}
336358

337359
func attachFolderMounts(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfiguration) error {
338-
mounts := make([]vz.DirectorySharingDeviceConfiguration, len(driver.Yaml.Mounts))
360+
var mounts []vz.DirectorySharingDeviceConfiguration
339361
if *driver.Yaml.MountType == limayaml.VIRTIOFS {
340362
for i, mount := range driver.Yaml.Mounts {
341363
expandedPath, err := localpathutil.Expand(mount.Location)
@@ -364,7 +386,7 @@ func attachFolderMounts(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineCo
364386
return err
365387
}
366388
config.SetDirectoryShare(share)
367-
mounts[i] = config
389+
mounts = append(mounts, config)
368390
}
369391
}
370392

@@ -378,7 +400,9 @@ func attachFolderMounts(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineCo
378400
}
379401
}
380402

381-
vmConfig.SetDirectorySharingDevicesVirtualMachineConfiguration(mounts)
403+
if len(mounts) > 0 {
404+
vmConfig.SetDirectorySharingDevicesVirtualMachineConfiguration(mounts)
405+
}
382406
return nil
383407
}
384408

0 commit comments

Comments
 (0)