Skip to content

Commit 83e46d5

Browse files
committed
Add support for toggling audio driver flag based on VBox version
The latest change to replace the VBox 7.0 deprecated flag `--audio` with the new flag `--audio-driver` resulted in a regression for 6.x versions. This change adds version detection logic to determine the appropriate flag to use for configuring sound on the running VM.
1 parent 34b7f1c commit 83e46d5

2 files changed

Lines changed: 129 additions & 2 deletions

File tree

builder/virtualbox/iso/step_create_vm.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ package iso
66
import (
77
"context"
88
"fmt"
9+
"log"
910
"strconv"
1011
"strings"
1112

13+
versionUtil "github.com/hashicorp/go-version"
1214
"github.com/hashicorp/packer-plugin-sdk/multistep"
1315
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
1416
vboxcommon "github.com/hashicorp/packer-plugin-virtualbox/builder/virtualbox/common"
@@ -46,11 +48,13 @@ func (s *stepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis
4648
commands = append(commands, []string{"modifyvm", name, "--memory", strconv.Itoa(config.HWConfig.MemorySize)})
4749
commands = append(commands, []string{"modifyvm", name, "--usb", map[bool]string{true: "on", false: "off"}[config.HWConfig.USB]})
4850

51+
vboxVersion, _ := driver.Version()
52+
audioDriverArg := audioDriverConfigurationArg(vboxVersion)
4953
if strings.ToLower(config.HWConfig.Sound) == "none" {
50-
commands = append(commands, []string{"modifyvm", name, "--audio-driver", config.HWConfig.Sound,
54+
commands = append(commands, []string{"modifyvm", name, audioDriverArg, config.HWConfig.Sound,
5155
"--audiocontroller", config.AudioController})
5256
} else {
53-
commands = append(commands, []string{"modifyvm", name, "--audio-driver", config.HWConfig.Sound, "--audioin", "on", "--audioout", "on",
57+
commands = append(commands, []string{"modifyvm", name, audioDriverArg, config.HWConfig.Sound, "--audioin", "on", "--audioout", "on",
5458
"--audiocontroller", config.AudioController})
5559
}
5660

@@ -131,3 +135,21 @@ func (s *stepCreateVM) Cleanup(state multistep.StateBag) {
131135
ui.Error(fmt.Sprintf("Error deleting VM: %s", err))
132136
}
133137
}
138+
139+
func audioDriverConfigurationArg(vboxVersion string) string {
140+
// The '--audio' argument was deprecated in v7.0.x giving it
141+
// the highest level of compatibility.
142+
compatibleAudioArg := "--audio"
143+
currentVersion, err := versionUtil.NewVersion(vboxVersion)
144+
if err != nil {
145+
log.Printf("[TRACE] attempt to read VBox version %q resulted in an error; using deprecated --audio argument: %s", vboxVersion, err)
146+
return compatibleAudioArg
147+
}
148+
149+
constraints, _ := versionUtil.NewConstraint(">= 7.0")
150+
if currentVersion != nil && constraints.Check(currentVersion) {
151+
compatibleAudioArg = "--audio-driver"
152+
}
153+
154+
return compatibleAudioArg
155+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package iso
5+
6+
import (
7+
"testing"
8+
9+
versionUtil "github.com/hashicorp/go-version"
10+
)
11+
12+
func TestAudioDriverConfigurationArgForVersionsUsingAudioArg(t *testing.T) {
13+
versions := []string{
14+
"4.3",
15+
"4.3.1",
16+
"6.0.0",
17+
"6.0.2",
18+
"6.0.4",
19+
"6.0.6",
20+
"6.0.8",
21+
"6.0.10",
22+
"6.0.20",
23+
"6.0.22",
24+
"6.0.24",
25+
"6.0.28",
26+
"6.1.0",
27+
"6.1.2",
28+
"6.1.8",
29+
"6.1.10",
30+
"6.1.12",
31+
"6.1.14",
32+
"6.1.20",
33+
"6.1.22",
34+
"6.1.30",
35+
"6.1.32",
36+
"6.1.34",
37+
"6.1.38",
38+
"6.1.40",
39+
"6.1.42",
40+
}
41+
42+
for _, in := range versions {
43+
in := in
44+
t.Run(in, func(t *testing.T) {
45+
got := audioDriverConfigurationArg(in)
46+
if got != "--audio" {
47+
t.Errorf("audioDriverConfigurationArg for with version %q returned %s but expected --audio", in, got)
48+
}
49+
50+
})
51+
}
52+
}
53+
func TestAudioDriverConfigurationArgForVersionsUsingAudioDriverArg(t *testing.T) {
54+
versions := []string{
55+
"7.0.0",
56+
"7.0.2",
57+
"7.0.4",
58+
"7.0.6",
59+
"7.0.8",
60+
"7.0.10",
61+
"7.0.12",
62+
"7.0.14",
63+
"7.0.16",
64+
}
65+
66+
for _, in := range versions {
67+
in := in
68+
t.Run(in, func(t *testing.T) {
69+
got := audioDriverConfigurationArg(in)
70+
if got != "--audio-driver" {
71+
t.Errorf("audioDriverConfigurationArg for with version %q returned %s but expected --audio-driver", in, got)
72+
}
73+
})
74+
}
75+
}
76+
func FuzzAudioDriverConfigurationArg(f *testing.F) {
77+
inputs := []string{
78+
"4.0",
79+
"4.1",
80+
"4.2",
81+
"4.3",
82+
"5.0",
83+
"5.1",
84+
"5.2",
85+
"6.0",
86+
"6.1",
87+
"7.0",
88+
}
89+
for _, in := range inputs {
90+
f.Add(in)
91+
}
92+
f.Fuzz(func(t *testing.T, input string) {
93+
expected := "--audio"
94+
95+
got := audioDriverConfigurationArg(input)
96+
versionIn, _ := versionUtil.NewVersion(input)
97+
constraints, _ := versionUtil.NewConstraint(">= 7.0")
98+
if versionIn != nil && constraints.Check(versionIn) {
99+
expected = "--audio-driver"
100+
}
101+
if got != expected {
102+
t.Errorf("audioDriverConfigurationArg for with version %q returned %s but expected %s", versionIn, got, expected)
103+
}
104+
})
105+
}

0 commit comments

Comments
 (0)