From f48d7e3990f66181e60cee1300e19778be7ac3a0 Mon Sep 17 00:00:00 2001 From: Thomas Dreibholz Date: Sun, 14 Feb 2021 23:24:59 +0100 Subject: [PATCH] VirtualBox: added support for "virtio" storage. --- builder/virtualbox/common/driver.go | 3 +++ builder/virtualbox/common/driver_4_2.go | 12 +++++++++++- builder/virtualbox/common/driver_mock.go | 10 ++++++++++ builder/virtualbox/iso/builder.go | 4 ++-- builder/virtualbox/iso/step_create_disk.go | 17 +++++++++++------ 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/builder/virtualbox/common/driver.go b/builder/virtualbox/common/driver.go index dcb036a26..9f09dcf6a 100644 --- a/builder/virtualbox/common/driver.go +++ b/builder/virtualbox/common/driver.go @@ -22,6 +22,9 @@ type Driver interface { // Create a SCSI controller. CreateSCSIController(vm string, controller string) error + // Create a VirtIO controller. + CreateVirtIOController(vm string, controller string) error + // Create an NVME controller CreateNVMeController(vm string, controller string, portcount int) error diff --git a/builder/virtualbox/common/driver_4_2.go b/builder/virtualbox/common/driver_4_2.go index 92ba12acf..56d0b5e49 100644 --- a/builder/virtualbox/common/driver_4_2.go +++ b/builder/virtualbox/common/driver_4_2.go @@ -64,7 +64,6 @@ func (d *VBox42Driver) CreateNVMeController(vmName string, name string, portcoun } func (d *VBox42Driver) CreateSCSIController(vmName string, name string) error { - command := []string{ "storagectl", vmName, "--name", name, @@ -75,6 +74,17 @@ func (d *VBox42Driver) CreateSCSIController(vmName string, name string) error { return d.VBoxManage(command...) } +func (d *VBox42Driver) CreateVirtIOController(vmName string, name string) error { + command := []string{ + "storagectl", vmName, + "--name", name, + "--add", "VirtIO", + "--controller", "VirtIO", + } + + return d.VBoxManage(command...) +} + func (d *VBox42Driver) RemoveFloppyControllers(vmName string) error { var stdout bytes.Buffer diff --git a/builder/virtualbox/common/driver_mock.go b/builder/virtualbox/common/driver_mock.go index 58dc5f749..a8e73a44f 100644 --- a/builder/virtualbox/common/driver_mock.go +++ b/builder/virtualbox/common/driver_mock.go @@ -13,6 +13,10 @@ type DriverMock struct { CreateSCSIControllerController string CreateSCSIControllerErr error + CreateVirtIOControllerVM string + CreateVirtIOControllerController string + CreateVirtIOControllerErr error + CreateNVMeControllerVM string CreateNVMeControllerController string CreateNVMeControllerErr error @@ -78,6 +82,12 @@ func (d *DriverMock) CreateSCSIController(vm string, controller string) error { return d.CreateSCSIControllerErr } +func (d *DriverMock) CreateVirtIOController(vm string, controller string) error { + d.CreateVirtIOControllerVM = vm + d.CreateVirtIOControllerController = vm + return d.CreateVirtIOControllerErr +} + func (d *DriverMock) CreateNVMeController(vm string, controller string, portcount int) error { d.CreateNVMeControllerVM = vm d.CreateNVMeControllerController = vm diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index 5cc5541f1..562410f50 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -186,11 +186,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) { } switch b.config.HardDriveInterface { - case "ide", "sata", "scsi", "pcie": + case "ide", "sata", "scsi", "pcie", "virtio": // do nothing default: errs = packersdk.MultiErrorAppend( - errs, errors.New("hard_drive_interface can only be ide, sata, pcie or scsi")) + errs, errors.New("hard_drive_interface can only be ide, sata, pcie, scsi or virtio")) } if b.config.SATAPortCount == 0 { diff --git a/builder/virtualbox/iso/step_create_disk.go b/builder/virtualbox/iso/step_create_disk.go index 6b3c620dc..3826fa3d1 100644 --- a/builder/virtualbox/iso/step_create_disk.go +++ b/builder/virtualbox/iso/step_create_disk.go @@ -73,6 +73,13 @@ func (s *stepCreateDisk) Run(ctx context.Context, state multistep.StateBag) mult ui.Error(err.Error()) return multistep.ActionHalt } + } else if config.HardDriveInterface == "virtio" { + if err := driver.CreateVirtIOController(vmName, "VirtIO Controller"); err != nil { + err := fmt.Errorf("Error creating disk controller: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } } else if config.HardDriveInterface == "pcie" { if err := driver.CreateNVMeController(vmName, "NVMe Controller", config.NVMePortCount); err != nil { err := fmt.Errorf("Error creating NVMe controller: %s", err) @@ -86,13 +93,11 @@ func (s *stepCreateDisk) Run(ctx context.Context, state multistep.StateBag) mult controllerName := "IDE Controller" if config.HardDriveInterface == "sata" { controllerName = "SATA Controller" - } - - if config.HardDriveInterface == "scsi" { + } else if config.HardDriveInterface == "scsi" { controllerName = "SCSI Controller" - } - - if config.HardDriveInterface == "pcie" { + } else if config.HardDriveInterface == "virtio" { + controllerName = "VirtIO Controller" + } else if config.HardDriveInterface == "pcie" { controllerName = "NVMe Controller" }