From 37a1f5003d4d6972c6f7932c2dc63b93da287b64 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Mon, 12 Sep 2016 15:02:17 +0200 Subject: [PATCH] qemu: Add configuration structure to simplify LaunchQemu LaunchQemu() now takes a Config structure that contains some more descriptive fields than raw qemu parameter strings. LaunchQemu is now simpler to call and more extensible as supporting more qemu parameters would mean expanding Config instead of changing the API. Signed-off-by: Samuel Ortiz --- examples_test.go | 4 +-- qemu.go | 77 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/examples_test.go b/examples_test.go index e7ed12abb6..45607a0444 100644 --- a/examples_test.go +++ b/examples_test.go @@ -38,10 +38,10 @@ func Example() { // resources params = append(params, "-m", "370", "-smp", "cpus=2") - // LaunchQemu should return as soon as the instance has launched as we + // LaunchCustomQemu should return as soon as the instance has launched as we // are using the --daemonize flag. It will set up a unix domain socket // called /tmp/qmp-socket that we can use to manage the instance. - _, err := qemu.LaunchQemu(context.Background(), params, nil, nil) + _, err := qemu.LaunchCustomQemu(context.Background(), "", params, nil, nil) if err != nil { panic(err) } diff --git a/qemu.go b/qemu.go index 4fe3b2b932..a0067a5458 100644 --- a/qemu.go +++ b/qemu.go @@ -26,14 +26,78 @@ package qemu import ( "bytes" + "fmt" "os" "os/exec" "context" ) -// LaunchQemu can be used to launch a new qemu instance by invoking the -// qemu-system-x86_64 binary. +// Config is the qemu configuration structure. +// It allows for passing custom settings and parameters to the qemu API. +type Config struct { + // Path is the qemu binary path. + Path string + + // Ctx is not used at the moment. + Ctx context.Context + + // MachineType is the machine type to be used by qemu. + MachineType string + + // MachineTypeAcceleration are the machine acceleration option to be used by qemu. + MachineTypeAcceleration string + + // CPUModel is the CPU model to be used by qemu. + CPUModel string + + // ExtraParams is a slice of options to pass to qemu. + ExtraParams []string + + // FDs is a list of open file descriptors to be passed to the spawned qemu process + FDs []*os.File +} + +func appendMachineParams(params []string, config Config) []string { + if config.MachineType != "" && config.MachineTypeAcceleration != "" { + params = append(params, "-machine") + params = append(params, fmt.Sprintf("%s,accel=%s", config.MachineType, config.MachineTypeAcceleration)) + } + + return params +} + +func appendCPUModel(params []string, config Config) []string { + if config.CPUModel != "" { + params = append(params, "-cpu") + params = append(params, config.CPUModel) + } + + return params +} + +// LaunchQemu can be used to launch a new qemu instance. +// +// The Config parameter contains a set of qemu parameters and settings. +// +// This function writes its log output via logger parameter. +// +// The function will block until the launched qemu process exits. "", nil +// will be returned if the launch succeeds. Otherwise a string containing +// the contents of stderr + a Go error object will be returned. +func LaunchQemu(config Config, logger QMPLog) (string, error) { + var params []string + + params = appendMachineParams(params, config) + params = appendCPUModel(params, config) + params = append(params, config.ExtraParams...) + + return LaunchCustomQemu(config.Ctx, config.Path, params, config.FDs, logger) +} + +// LaunchCustomQemu can be used to launch a new qemu instance. +// +// The path parameter is used to pass the qemu executable path. // // The ctx parameter is not currently used but has been added so that the // signature of this function will not need to change when launch cancellation @@ -48,13 +112,18 @@ import ( // The function will block until the launched qemu process exits. "", nil // will be returned if the launch succeeds. Otherwise a string containing // the contents of stderr + a Go error object will be returned. -func LaunchQemu(ctx context.Context, params []string, fds []*os.File, logger QMPLog) (string, error) { +func LaunchCustomQemu(ctx context.Context, path string, params []string, fds []*os.File, logger QMPLog) (string, error) { if logger == nil { logger = qmpNullLogger{} } errStr := "" - cmd := exec.Command("qemu-system-x86_64", params...) + + if path == "" { + path = "qemu-system-x86_64" + } + + cmd := exec.Command(path, params...) if len(fds) > 0 { logger.Infof("Adding extra file %v", fds) cmd.ExtraFiles = fds