qemu: Embed the qemu parameters into the Config structure

It is a private field now, and all append*() routines are now
Config methods instead of private qemu functions.

Since we will have to carry a kernelParams private field as well,
this change will keep all built parameters internal and make things
consistent.

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
This commit is contained in:
Samuel Ortiz 2016-09-19 12:07:00 +02:00
parent e193a77b8d
commit 12f6ebe389
2 changed files with 82 additions and 134 deletions

146
qemu.go
View File

@ -732,18 +732,18 @@ type Config struct {
// FDs is a list of open file descriptors to be passed to the spawned qemu process // FDs is a list of open file descriptors to be passed to the spawned qemu process
FDs []*os.File FDs []*os.File
qemuParams []string
} }
func appendName(params []string, config Config) []string { func (config *Config) appendName() {
if config.Name != "" { if config.Name != "" {
params = append(params, "-name") config.qemuParams = append(config.qemuParams, "-name")
params = append(params, config.Name) config.qemuParams = append(config.qemuParams, config.Name)
} }
return params
} }
func appendMachine(params []string, config Config) []string { func (config *Config) appendMachine() {
if config.Machine.Type != "" { if config.Machine.Type != "" {
var machineParams []string var machineParams []string
@ -753,25 +753,21 @@ func appendMachine(params []string, config Config) []string {
machineParams = append(machineParams, fmt.Sprintf(",accel=%s", config.Machine.Acceleration)) machineParams = append(machineParams, fmt.Sprintf(",accel=%s", config.Machine.Acceleration))
} }
params = append(params, "-machine") config.qemuParams = append(config.qemuParams, "-machine")
params = append(params, strings.Join(machineParams, "")) config.qemuParams = append(config.qemuParams, strings.Join(machineParams, ""))
} }
return params
} }
func appendCPUModel(params []string, config Config) []string { func (config *Config) appendCPUModel() {
if config.CPUModel != "" { if config.CPUModel != "" {
params = append(params, "-cpu") config.qemuParams = append(config.qemuParams, "-cpu")
params = append(params, config.CPUModel) config.qemuParams = append(config.qemuParams, config.CPUModel)
} }
return params
} }
func appendQMPSocket(params []string, config Config) []string { func (config *Config) appendQMPSocket() {
if config.QMPSocket.Valid() == false { if config.QMPSocket.Valid() == false {
return nil return
} }
var qmpParams []string var qmpParams []string
@ -785,34 +781,28 @@ func appendQMPSocket(params []string, config Config) []string {
} }
} }
params = append(params, "-qmp") config.qemuParams = append(config.qemuParams, "-qmp")
params = append(params, strings.Join(qmpParams, "")) config.qemuParams = append(config.qemuParams, strings.Join(qmpParams, ""))
return params
} }
func appendDevices(params []string, config Config) []string { func (config *Config) appendDevices() {
for _, d := range config.Devices { for _, d := range config.Devices {
if d.Valid() == false { if d.Valid() == false {
continue continue
} }
params = append(params, d.QemuParams()...) config.qemuParams = append(config.qemuParams, d.QemuParams()...)
} }
return params
} }
func appendUUID(params []string, config Config) []string { func (config *Config) appendUUID() {
if config.UUID != "" { if config.UUID != "" {
params = append(params, "-uuid") config.qemuParams = append(config.qemuParams, "-uuid")
params = append(params, config.UUID) config.qemuParams = append(config.qemuParams, config.UUID)
} }
return params
} }
func appendMemory(params []string, config Config) []string { func (config *Config) appendMemory() {
if config.Memory.Size != "" { if config.Memory.Size != "" {
var memoryParams []string var memoryParams []string
@ -826,14 +816,12 @@ func appendMemory(params []string, config Config) []string {
memoryParams = append(memoryParams, fmt.Sprintf(",maxmem=%s", config.Memory.MaxMem)) memoryParams = append(memoryParams, fmt.Sprintf(",maxmem=%s", config.Memory.MaxMem))
} }
params = append(params, "-m") config.qemuParams = append(config.qemuParams, "-m")
params = append(params, strings.Join(memoryParams, "")) config.qemuParams = append(config.qemuParams, strings.Join(memoryParams, ""))
} }
return params
} }
func appendCPUs(params []string, config Config) []string { func (config *Config) appendCPUs() {
if config.SMP.CPUs > 0 { if config.SMP.CPUs > 0 {
var SMPParams []string var SMPParams []string
@ -851,16 +839,14 @@ func appendCPUs(params []string, config Config) []string {
SMPParams = append(SMPParams, fmt.Sprintf(",sockets=%d", config.SMP.Sockets)) SMPParams = append(SMPParams, fmt.Sprintf(",sockets=%d", config.SMP.Sockets))
} }
params = append(params, "-smp") config.qemuParams = append(config.qemuParams, "-smp")
params = append(params, strings.Join(SMPParams, "")) config.qemuParams = append(config.qemuParams, strings.Join(SMPParams, ""))
} }
return params
} }
func appendRTC(params []string, config Config) []string { func (config *Config) appendRTC() {
if config.RTC.Valid() == false { if config.RTC.Valid() == false {
return nil return
} }
var RTCParams []string var RTCParams []string
@ -875,58 +861,48 @@ func appendRTC(params []string, config Config) []string {
RTCParams = append(RTCParams, fmt.Sprintf(",clock=%s", config.RTC.Clock)) RTCParams = append(RTCParams, fmt.Sprintf(",clock=%s", config.RTC.Clock))
} }
params = append(params, "-rtc") config.qemuParams = append(config.qemuParams, "-rtc")
params = append(params, strings.Join(RTCParams, "")) config.qemuParams = append(config.qemuParams, strings.Join(RTCParams, ""))
return params
} }
func appendGlobalParam(params []string, config Config) []string { func (config *Config) appendGlobalParam() {
if config.GlobalParam != "" { if config.GlobalParam != "" {
params = append(params, "-global") config.qemuParams = append(config.qemuParams, "-global")
params = append(params, config.GlobalParam) config.qemuParams = append(config.qemuParams, config.GlobalParam)
} }
return params
} }
func appendVGA(params []string, config Config) []string { func (config *Config) appendVGA() {
if config.VGA != "" { if config.VGA != "" {
params = append(params, "-vga") config.qemuParams = append(config.qemuParams, "-vga")
params = append(params, config.VGA) config.qemuParams = append(config.qemuParams, config.VGA)
} }
return params
} }
func appendKernel(params []string, config Config) []string { func (config *Config) appendKernel() {
if config.Kernel.Path != "" { if config.Kernel.Path != "" {
params = append(params, "-kernel") config.qemuParams = append(config.qemuParams, "-kernel")
params = append(params, config.Kernel.Path) config.qemuParams = append(config.qemuParams, config.Kernel.Path)
if config.Kernel.Params != "" { if config.Kernel.Params != "" {
params = append(params, "-append") config.qemuParams = append(config.qemuParams, "-append")
params = append(params, config.Kernel.Params) config.qemuParams = append(config.qemuParams, config.Kernel.Params)
} }
} }
return params
} }
func appendKnobs(params []string, config Config) []string { func (config *Config) appendKnobs() {
if config.Knobs.NoUserConfig == true { if config.Knobs.NoUserConfig == true {
params = append(params, "-no-user-config") config.qemuParams = append(config.qemuParams, "-no-user-config")
} }
if config.Knobs.NoDefaults == true { if config.Knobs.NoDefaults == true {
params = append(params, "-nodefaults") config.qemuParams = append(config.qemuParams, "-nodefaults")
} }
if config.Knobs.NoGraphic == true { if config.Knobs.NoGraphic == true {
params = append(params, "-nographic") config.qemuParams = append(config.qemuParams, "-nographic")
} }
return params
} }
// LaunchQemu can be used to launch a new qemu instance. // LaunchQemu can be used to launch a new qemu instance.
@ -939,23 +915,21 @@ func appendKnobs(params []string, config Config) []string {
// will be returned if the launch succeeds. Otherwise a string containing // will be returned if the launch succeeds. Otherwise a string containing
// the contents of stderr + a Go error object will be returned. // the contents of stderr + a Go error object will be returned.
func LaunchQemu(config Config, logger QMPLog) (string, error) { func LaunchQemu(config Config, logger QMPLog) (string, error) {
var params []string config.appendName()
config.appendUUID()
config.appendMachine()
config.appendCPUModel()
config.appendQMPSocket()
config.appendMemory()
config.appendCPUs()
config.appendDevices()
config.appendRTC()
config.appendGlobalParam()
config.appendVGA()
config.appendKnobs()
config.appendKernel()
params = appendName(params, config) return LaunchCustomQemu(config.Ctx, config.Path, config.qemuParams, config.FDs, logger)
params = appendUUID(params, config)
params = appendMachine(params, config)
params = appendCPUModel(params, config)
params = appendQMPSocket(params, config)
params = appendMemory(params, config)
params = appendCPUs(params, config)
params = appendDevices(params, config)
params = appendRTC(params, config)
params = appendGlobalParam(params, config)
params = appendVGA(params, config)
params = appendKnobs(params, config)
params = appendKernel(params, config)
return LaunchCustomQemu(config.Ctx, config.Path, params, config.FDs, logger)
} }
// LaunchCustomQemu can be used to launch a new qemu instance. // LaunchCustomQemu can be used to launch a new qemu instance.
@ -966,7 +940,7 @@ func LaunchQemu(config Config, logger QMPLog) (string, error) {
// signature of this function will not need to change when launch cancellation // signature of this function will not need to change when launch cancellation
// is implemented. // is implemented.
// //
// params is a slice of options to pass to qemu-system-x86_64 and fds is a // config.qemuParams is a slice of options to pass to qemu-system-x86_64 and fds is a
// list of open file descriptors that are to be passed to the spawned qemu // list of open file descriptors that are to be passed to the spawned qemu
// process. // process.
// //

View File

@ -24,67 +24,43 @@ import (
) )
func testAppend(structure interface{}, expected string, t *testing.T) { func testAppend(structure interface{}, expected string, t *testing.T) {
var params []string var config Config
switch s := structure.(type) { switch s := structure.(type) {
case Machine: case Machine:
config := Config{ config.Machine = s
Machine: s, config.appendMachine()
}
params = appendMachine([]string{}, config)
case Device: case Device:
config := Config{ config.Devices = []Device{s}
Devices: []Device{s}, config.appendDevices()
}
params = appendDevices([]string{}, config)
case Knobs: case Knobs:
config := Config{ config.Knobs = s
Knobs: s, config.appendKnobs()
}
params = appendKnobs([]string{}, config)
case Kernel: case Kernel:
config := Config{ config.Kernel = s
Kernel: s, config.appendKernel()
}
params = appendKernel([]string{}, config)
case Memory: case Memory:
config := Config{ config.Memory = s
Memory: s, config.appendMemory()
}
params = appendMemory([]string{}, config)
case SMP: case SMP:
config := Config{ config.SMP = s
SMP: s, config.appendCPUs()
}
params = appendCPUs([]string{}, config)
case QMPSocket: case QMPSocket:
config := Config{ config.QMPSocket = s
QMPSocket: s, config.appendQMPSocket()
}
params = appendQMPSocket([]string{}, config)
case RTC: case RTC:
config := Config{ config.RTC = s
RTC: s, config.appendRTC()
}
params = appendRTC([]string{}, config)
} }
result := strings.Join(params, " ") result := strings.Join(config.qemuParams, " ")
if result != expected { if result != expected {
t.Fatalf("Failed to append parameters [%s] != [%s]", result, expected) t.Fatalf("Failed to append parameters [%s] != [%s]", result, expected)
} }
@ -274,8 +250,6 @@ func TestAppendQMPSocket(t *testing.T) {
var qemuString = "-name cc-qemu -cpu host -uuid " + testutil.AgentUUID var qemuString = "-name cc-qemu -cpu host -uuid " + testutil.AgentUUID
func TestAppendStrings(t *testing.T) { func TestAppendStrings(t *testing.T) {
var params []string
config := Config{ config := Config{
Path: "qemu", Path: "qemu",
Name: "cc-qemu", Name: "cc-qemu",
@ -283,11 +257,11 @@ func TestAppendStrings(t *testing.T) {
CPUModel: "host", CPUModel: "host",
} }
params = appendName(params, config) config.appendName()
params = appendCPUModel(params, config) config.appendCPUModel()
params = appendUUID(params, config) config.appendUUID()
result := strings.Join(params, " ") result := strings.Join(config.qemuParams, " ")
if result != qemuString { if result != qemuString {
t.Fatalf("Failed to append parameters [%s] != [%s]", result, qemuString) t.Fatalf("Failed to append parameters [%s] != [%s]", result, qemuString)
} }