diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index ba2507ef1..0643370de 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -7,6 +7,7 @@ import ( "context" "errors" "fmt" + "regexp" "github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/packer-plugin-sdk/bootcommand" @@ -83,6 +84,17 @@ type Config struct { // When set to vmsvga, the graphics controller is VMware SVGA. // When set to none, the graphics controller is disabled. GfxController string `mapstructure:"gfx_controller" required:"false"` + // The VRAM size to be used. By default, this is 4 MiB. + GfxVramSize uint `mapstructure:"gfx_vram_size" required:"false"` + // 3D acceleration: true or false. + // When set to true, 3D acceleration is enabled. + // When set to false, 3D acceleration is disabled. This is the default. + GfxAccelerate3D bool `mapstructure:"gfx_accelerate_3d" required:"false"` + // Screen resolution in EFI mode: WIDTHxHEIGHT. + // When set to WIDTHxHEIGHT, it provides the given width and height as screen resolution + // to EFI, for example 1920x1080 for Full-HD resolution. By default, no screen resolution + // is set. Note, that this option only affects EFI boot, not the (default) BIOS boot. + GfxEFIResolution string `mapstructure:"gfx_efi_resolution" required:"false"` // The guest OS type being installed. By default this is other, but you can // get dramatic performance improvements by setting this to the proper // value. To view all available values for this run VBoxManage list @@ -261,6 +273,24 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) { errs, errors.New("Graphics controller type can only be vboxvga, vboxsvga, vmsvga, none")) } + if b.config.GfxVramSize == 0 { + b.config.GfxVramSize = 4 + } else { + if b.config.GfxVramSize < 1 || b.config.GfxVramSize > 128 { + errs = packersdk.MultiErrorAppend( + errs, errors.New("VGRAM size must be from 0 (use default) to 128")) + } + } + + if b.config.GfxEFIResolution != "" { + re := regexp.MustCompile(`^[\d]+x[\d]+$`) + matched := re.MatchString(b.config.GfxEFIResolution) + if !matched { + errs = packersdk.MultiErrorAppend( + errs, errors.New("EFI resolution must be in the format WIDTHxHEIGHT, e.g. 1920x1080")) + } + } + if b.config.AudioController == "" { b.config.AudioController = "ac97" } diff --git a/builder/virtualbox/iso/step_create_vm.go b/builder/virtualbox/iso/step_create_vm.go index f0f9c4f27..75203f93a 100644 --- a/builder/virtualbox/iso/step_create_vm.go +++ b/builder/virtualbox/iso/step_create_vm.go @@ -26,7 +26,7 @@ func (s *stepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis name := config.VMName - commands := make([][]string, 12) + commands := make([][]string, 14) commands[0] = []string{ "createvm", "--name", name, "--ostype", config.GuestOSType, "--register", @@ -59,7 +59,7 @@ func (s *stepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis "--nictype6", config.NICType, "--nictype7", config.NICType, "--nictype8", config.NICType} - commands[9] = []string{"modifyvm", name, "--graphicscontroller", config.GfxController} + commands[9] = []string{"modifyvm", name, "--graphicscontroller", config.GfxController, "--vram", strconv.FormatUint(uint64(config.GfxVramSize), 10)} if config.RTCTimeBase == "UTC" { commands[10] = []string{"modifyvm", name, "--rtcuseutc", "on"} } else { @@ -71,6 +71,15 @@ func (s *stepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis commands[11] = []string{"modifyvm", name, "--nested-hw-virt", "off"} } + if config.GfxAccelerate3D { + commands[12] = []string{"modifyvm", name, "--accelerate3d", "on"} + } else { + commands[12] = []string{"modifyvm", name, "--accelerate3d", "off"} + } + if config.GfxEFIResolution != "" { + commands[13] = []string{"setextradata", name, "VBoxInternal2/EfiGraphicsResolution", config.GfxEFIResolution} + } + ui.Say("Creating virtual machine...") for _, command := range commands { err := driver.VBoxManage(command...)