Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vz: fix nil pointer dereference with mountType: reverse-sshfs; validate disk format #1245

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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