Skip to content

Commit

Permalink
Merge pull request lima-vm#1245 from AkihiroSuda/dev3
Browse files Browse the repository at this point in the history
vz: fix nil pointer dereference with `mountType: reverse-sshfs`; validate disk format
  • Loading branch information
AkihiroSuda authored Dec 14, 2022
2 parents 45cd9cf + 25cd97e commit 585d6e2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/vmtype.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Lima supports two ways of running guest machines:
- [qemu](#qemu)
- [vz](#vz)

The vmType can be specified only on creating the instance.
The vmType of existing instances cannot be changed.

## QEMU
"qemu" option makes use of QEMU to run guest operating system.
This option is used by default if "vmType" is not set.
Expand Down
2 changes: 2 additions & 0 deletions examples/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# so they can be overridden by the $LIMA_HOME/_config/default.yaml mechanism documented at the end of this file.

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

Expand Down
30 changes: 27 additions & 3 deletions pkg/vz/vm_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/localpathutil"
"github.com/lima-vm/lima/pkg/networks"
"github.com/lima-vm/lima/pkg/qemu/imgutil"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -242,6 +243,18 @@ func attachNetwork(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigu
return nil
}

func validateDiskFormat(diskPath string) error {
format, err := imgutil.DetectFormat(diskPath)
if err != nil {
return fmt.Errorf("failed to detect the format of %q: %w", diskPath, err)
}
if format != "raw" {
return fmt.Errorf("expected the format of %q to be \"raw\", got %q", diskPath, format)
}
// TODO: ensure that the disk is formatted with GPT or ISO9660
return nil
}

func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfiguration) error {
baseDiskPath := filepath.Join(driver.Instance.Dir, filenames.BaseDisk)
diffDiskPath := filepath.Join(driver.Instance.Dir, filenames.DiffDisk)
Expand All @@ -253,6 +266,9 @@ func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigura
var configurations []vz.StorageDeviceConfiguration

if isBaseDiskCDROM {
if err = validateDiskFormat(baseDiskPath); err != nil {
return err
}
baseDiskAttachment, err := vz.NewDiskImageStorageDeviceAttachment(baseDiskPath, true)
if err != nil {
return err
Expand All @@ -263,6 +279,9 @@ func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigura
}
configurations = append(configurations, baseDisk)
}
if err = validateDiskFormat(diffDiskPath); err != nil {
return err
}
diffDiskAttachment, err := vz.NewDiskImageStorageDeviceAttachment(diffDiskPath, false)
if err != nil {
return err
Expand All @@ -273,6 +292,9 @@ func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigura
}
configurations = append(configurations, diffDisk)

if err = validateDiskFormat(ciDataPath); err != nil {
return err
}
ciDataAttachment, err := vz.NewDiskImageStorageDeviceAttachment(ciDataPath, true)
if err != nil {
return err
Expand Down Expand Up @@ -335,7 +357,7 @@ func attachConsole(_ *driver.BaseDriver, vmConfig *vz.VirtualMachineConfiguratio
}

func attachFolderMounts(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfiguration) error {
mounts := make([]vz.DirectorySharingDeviceConfiguration, len(driver.Yaml.Mounts))
var mounts []vz.DirectorySharingDeviceConfiguration
if *driver.Yaml.MountType == limayaml.VIRTIOFS {
for i, mount := range driver.Yaml.Mounts {
expandedPath, err := localpathutil.Expand(mount.Location)
Expand Down Expand Up @@ -364,7 +386,7 @@ func attachFolderMounts(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineCo
return err
}
config.SetDirectoryShare(share)
mounts[i] = config
mounts = append(mounts, config)
}
}

Expand All @@ -378,7 +400,9 @@ func attachFolderMounts(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineCo
}
}

vmConfig.SetDirectorySharingDevicesVirtualMachineConfiguration(mounts)
if len(mounts) > 0 {
vmConfig.SetDirectorySharingDevicesVirtualMachineConfiguration(mounts)
}
return nil
}

Expand Down

0 comments on commit 585d6e2

Please sign in to comment.