VirtualBox: added support for "virtio" storage.

This commit is contained in:
Thomas Dreibholz 2021-02-14 23:24:59 +01:00
parent c9c65383e9
commit f48d7e3990
5 changed files with 37 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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"
}