From b973bc59fb82ab8d273de6493a65af043163491d Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Mon, 12 Sep 2016 18:06:14 +0200 Subject: [PATCH] qemu: Add an Object slice field to the Config structure The Object slice tells qemu which specific object to create. Qemu objects can represent memory backend files, random number generators, TLS credentials, etc... Signed-off-by: Samuel Ortiz --- qemu.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/qemu.go b/qemu.go index 8ce5f959f1..0908e78194 100644 --- a/qemu.go +++ b/qemu.go @@ -56,6 +56,22 @@ type Device struct { CharDev string } +// Object is a qemu object representation. +type Object struct { + // Type is the qemu object type + Type string + + // ID is the user defined object ID. + ID string + + // MemPath is the object's memory path. + // This is only relevant for memory objects + MemPath string + + // Size is the object size in bytes + Size uint64 +} + // QMPSocket represents a qemu QMP socket configuration. type QMPSocket struct { // Type is the socket type (e.g. "unix"). @@ -98,6 +114,9 @@ type Config struct { // Devices is a list of devices for qemu to create. Devices []Device + // Objects is a list of objects for qemu to create. + Objects []Object + // ExtraParams is a slice of options to pass to qemu. ExtraParams []string @@ -187,6 +206,33 @@ func appendDevices(params []string, config Config) []string { return params } +func appendObjects(params []string, config Config) []string { + for _, o := range config.Objects { + if o.Type != "" { + var objectParams []string + + objectParams = append(objectParams, o.Type) + + if o.ID != "" { + objectParams = append(objectParams, fmt.Sprintf(",id=%s", o.ID)) + } + + if o.MemPath != "" { + objectParams = append(objectParams, fmt.Sprintf(",mem-path=%s", o.MemPath)) + } + + if o.Size > 0 { + objectParams = append(objectParams, fmt.Sprintf(",size=%d", o.Size)) + } + + params = append(params, "-object") + params = append(params, strings.Join(objectParams, "")) + } + } + + return params +} + // LaunchQemu can be used to launch a new qemu instance. // // The Config parameter contains a set of qemu parameters and settings. @@ -204,6 +250,7 @@ func LaunchQemu(config Config, logger QMPLog) (string, error) { params = appendCPUModel(params, config) params = appendQMPSocket(params, config) params = appendDevices(params, config) + params = appendObjects(params, config) params = append(params, config.ExtraParams...)