2016-05-31 21:35:14 +00:00
|
|
|
/*
|
|
|
|
* virqemu.c: utilities for working with qemu and its tools
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "virbuffer.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virqemu.h"
|
2016-07-22 15:19:28 +00:00
|
|
|
#include "virstring.h"
|
|
|
|
#include "viralloc.h"
|
2016-05-31 21:35:14 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
VIR_LOG_INIT("util.qemu");
|
|
|
|
|
2016-07-22 15:19:28 +00:00
|
|
|
struct virQEMUCommandLineJSONIteratorData {
|
|
|
|
const char *prefix;
|
|
|
|
virBufferPtr buf;
|
2016-07-22 15:50:03 +00:00
|
|
|
virQEMUBuildCommandLineJSONArrayFormatFunc arrayFunc;
|
2016-07-22 15:19:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virQEMUBuildCommandLineJSONRecurse(const char *key,
|
2016-12-14 14:25:31 +00:00
|
|
|
virJSONValuePtr value,
|
2016-07-22 15:19:28 +00:00
|
|
|
virBufferPtr buf,
|
2016-07-22 15:50:03 +00:00
|
|
|
virQEMUBuildCommandLineJSONArrayFormatFunc arrayFunc,
|
2016-07-22 15:19:28 +00:00
|
|
|
bool nested);
|
|
|
|
|
2016-07-22 15:50:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virQEMUBuildCommandLineJSONArrayBitmap(const char *key,
|
2016-12-14 14:25:31 +00:00
|
|
|
virJSONValuePtr array,
|
2016-07-22 15:50:03 +00:00
|
|
|
virBufferPtr buf)
|
|
|
|
{
|
|
|
|
ssize_t pos = -1;
|
|
|
|
ssize_t end;
|
|
|
|
virBitmapPtr bitmap = NULL;
|
|
|
|
|
|
|
|
if (virJSONValueGetArrayAsBitmap(array, &bitmap) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while ((pos = virBitmapNextSetBit(bitmap, pos)) > -1) {
|
|
|
|
if ((end = virBitmapNextClearBit(bitmap, pos)) < 0)
|
|
|
|
end = virBitmapLastSetBit(bitmap) + 1;
|
|
|
|
|
|
|
|
if (end - 1 > pos) {
|
2016-07-25 12:59:19 +00:00
|
|
|
virBufferAsprintf(buf, "%s=%zd-%zd,", key, pos, end - 1);
|
2016-07-22 15:50:03 +00:00
|
|
|
pos = end;
|
|
|
|
} else {
|
2016-07-25 12:59:19 +00:00
|
|
|
virBufferAsprintf(buf, "%s=%zd,", key, pos);
|
2016-07-22 15:50:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virBitmapFree(bitmap);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-25 17:47:40 +00:00
|
|
|
int
|
|
|
|
virQEMUBuildCommandLineJSONArrayNumbered(const char *key,
|
2016-12-14 14:25:31 +00:00
|
|
|
virJSONValuePtr array,
|
2016-07-25 17:47:40 +00:00
|
|
|
virBufferPtr buf)
|
|
|
|
{
|
2016-12-14 14:25:31 +00:00
|
|
|
virJSONValuePtr member;
|
2016-07-25 17:47:40 +00:00
|
|
|
char *prefix = NULL;
|
|
|
|
size_t i;
|
|
|
|
int ret = 0;
|
|
|
|
|
2016-10-10 20:08:39 +00:00
|
|
|
for (i = 0; i < virJSONValueArraySize(array); i++) {
|
2016-07-25 17:47:40 +00:00
|
|
|
member = virJSONValueArrayGet((virJSONValuePtr) array, i);
|
|
|
|
|
|
|
|
if (virAsprintf(&prefix, "%s.%zu", key, i) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virQEMUBuildCommandLineJSONRecurse(prefix, member, buf,
|
|
|
|
virQEMUBuildCommandLineJSONArrayNumbered,
|
|
|
|
true) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
VIR_FREE(prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(prefix);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-22 15:19:28 +00:00
|
|
|
/* internal iterator to handle nested object formatting */
|
|
|
|
static int
|
|
|
|
virQEMUBuildCommandLineJSONIterate(const char *key,
|
2016-12-14 14:25:31 +00:00
|
|
|
virJSONValuePtr value,
|
2016-07-22 15:19:28 +00:00
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
struct virQEMUCommandLineJSONIteratorData *data = opaque;
|
|
|
|
char *tmpkey = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (data->prefix) {
|
|
|
|
if (virAsprintf(&tmpkey, "%s.%s", data->prefix, key) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2016-07-22 15:50:03 +00:00
|
|
|
ret = virQEMUBuildCommandLineJSONRecurse(tmpkey, value, data->buf,
|
|
|
|
data->arrayFunc, false);
|
2016-07-22 15:19:28 +00:00
|
|
|
|
|
|
|
VIR_FREE(tmpkey);
|
|
|
|
} else {
|
2016-07-22 15:50:03 +00:00
|
|
|
ret = virQEMUBuildCommandLineJSONRecurse(key, value, data->buf,
|
|
|
|
data->arrayFunc, false);
|
2016-07-22 15:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-31 21:35:14 +00:00
|
|
|
|
|
|
|
static int
|
2016-07-22 13:54:57 +00:00
|
|
|
virQEMUBuildCommandLineJSONRecurse(const char *key,
|
2016-12-14 14:25:31 +00:00
|
|
|
virJSONValuePtr value,
|
2016-07-22 13:54:57 +00:00
|
|
|
virBufferPtr buf,
|
2016-07-22 15:50:03 +00:00
|
|
|
virQEMUBuildCommandLineJSONArrayFormatFunc arrayFunc,
|
2016-07-22 13:54:57 +00:00
|
|
|
bool nested)
|
2016-05-31 21:35:14 +00:00
|
|
|
{
|
2016-07-22 15:50:03 +00:00
|
|
|
struct virQEMUCommandLineJSONIteratorData data = { key, buf, arrayFunc };
|
2016-05-31 21:35:14 +00:00
|
|
|
virJSONValuePtr elem;
|
|
|
|
size_t i;
|
|
|
|
|
2016-07-22 15:19:28 +00:00
|
|
|
if (!key && value->type != VIR_JSON_TYPE_OBJECT) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("only JSON objects can be top level"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-31 21:35:14 +00:00
|
|
|
switch ((virJSONType) value->type) {
|
|
|
|
case VIR_JSON_TYPE_STRING:
|
2016-07-25 12:59:19 +00:00
|
|
|
virBufferAsprintf(buf, "%s=", key);
|
2016-07-25 12:37:47 +00:00
|
|
|
virQEMUBuildBufferEscapeComma(buf, value->data.string);
|
2016-07-25 12:59:19 +00:00
|
|
|
virBufferAddLit(buf, ",");
|
2016-05-31 21:35:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_JSON_TYPE_NUMBER:
|
2016-07-25 12:59:19 +00:00
|
|
|
virBufferAsprintf(buf, "%s=%s,", key, value->data.number);
|
2016-05-31 21:35:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_JSON_TYPE_BOOLEAN:
|
|
|
|
if (value->data.boolean)
|
2016-07-25 12:59:19 +00:00
|
|
|
virBufferAsprintf(buf, "%s=yes,", key);
|
2016-05-31 21:35:14 +00:00
|
|
|
else
|
2016-07-25 12:59:19 +00:00
|
|
|
virBufferAsprintf(buf, "%s=no,", key);
|
2016-05-31 21:35:14 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_JSON_TYPE_ARRAY:
|
|
|
|
if (nested) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2016-07-22 13:54:57 +00:00
|
|
|
_("nested JSON array to commandline conversion is "
|
|
|
|
"not supported"));
|
2016-05-31 21:35:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-22 15:50:03 +00:00
|
|
|
if (!arrayFunc || arrayFunc(key, value, buf) < 0) {
|
2016-05-31 21:35:14 +00:00
|
|
|
/* fallback, treat the array as a non-bitmap, adding the key
|
|
|
|
* for each member */
|
|
|
|
for (i = 0; i < virJSONValueArraySize(value); i++) {
|
|
|
|
elem = virJSONValueArrayGet((virJSONValuePtr)value, i);
|
|
|
|
|
|
|
|
/* recurse to avoid duplicating code */
|
2016-07-22 15:50:03 +00:00
|
|
|
if (virQEMUBuildCommandLineJSONRecurse(key, elem, buf,
|
|
|
|
arrayFunc, true) < 0)
|
2016-05-31 21:35:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_JSON_TYPE_OBJECT:
|
2016-07-22 15:19:28 +00:00
|
|
|
if (virJSONValueObjectForeachKeyValue(value,
|
|
|
|
virQEMUBuildCommandLineJSONIterate,
|
|
|
|
&data) < 0)
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
|
2016-05-31 21:35:14 +00:00
|
|
|
case VIR_JSON_TYPE_NULL:
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
2016-07-22 15:19:28 +00:00
|
|
|
_("NULL JSON type can't be converted to commandline"));
|
2016-05-31 21:35:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-22 13:54:57 +00:00
|
|
|
/**
|
|
|
|
* virQEMUBuildCommandLineJSON:
|
|
|
|
* @value: json object containing the value
|
|
|
|
* @buf: otuput buffer
|
2016-07-22 15:50:03 +00:00
|
|
|
* @arrayFunc: array formatter function to allow for different syntax
|
2016-07-22 13:54:57 +00:00
|
|
|
*
|
|
|
|
* Formats JSON value object into command line parameters suitable for use with
|
|
|
|
* qemu.
|
|
|
|
*
|
|
|
|
* Returns 0 on success -1 on error.
|
|
|
|
*/
|
|
|
|
int
|
2016-12-14 14:25:31 +00:00
|
|
|
virQEMUBuildCommandLineJSON(virJSONValuePtr value,
|
2016-07-22 15:50:03 +00:00
|
|
|
virBufferPtr buf,
|
|
|
|
virQEMUBuildCommandLineJSONArrayFormatFunc array)
|
2016-07-22 13:54:57 +00:00
|
|
|
{
|
2016-07-25 12:59:19 +00:00
|
|
|
if (virQEMUBuildCommandLineJSONRecurse(NULL, value, buf, array, false) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
virBufferTrim(buf, ",", -1);
|
|
|
|
|
|
|
|
return 0;
|
2016-05-31 21:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
virQEMUBuildObjectCommandlineFromJSON(const char *type,
|
|
|
|
const char *alias,
|
|
|
|
virJSONValuePtr props)
|
|
|
|
{
|
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
|
|
|
char *ret = NULL;
|
|
|
|
|
2016-07-25 12:59:19 +00:00
|
|
|
virBufferAsprintf(&buf, "%s,id=%s,", type, alias);
|
2016-05-31 21:35:14 +00:00
|
|
|
|
2016-07-22 15:50:03 +00:00
|
|
|
if (virQEMUBuildCommandLineJSON(props, &buf,
|
|
|
|
virQEMUBuildCommandLineJSONArrayBitmap) < 0)
|
2016-05-31 21:35:14 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virBufferCheckError(&buf) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = virBufferContentAndReset(&buf);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virBufferFreeAndReset(&buf);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-07-15 11:02:05 +00:00
|
|
|
|
|
|
|
|
2016-07-25 17:51:18 +00:00
|
|
|
char *
|
2016-12-14 14:25:31 +00:00
|
|
|
virQEMUBuildDriveCommandlineFromJSON(virJSONValuePtr srcdef)
|
2016-07-25 17:51:18 +00:00
|
|
|
{
|
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
if (virQEMUBuildCommandLineJSON(srcdef, &buf,
|
|
|
|
virQEMUBuildCommandLineJSONArrayNumbered) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virBufferCheckError(&buf) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = virBufferContentAndReset(&buf);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virBufferFreeAndReset(&buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-15 11:02:05 +00:00
|
|
|
/**
|
|
|
|
* virQEMUBuildBufferEscapeComma:
|
|
|
|
* @buf: buffer to append the escaped string
|
|
|
|
* @str: the string to escape
|
|
|
|
*
|
|
|
|
* qemu requires that any values passed on the command line which contain
|
|
|
|
* a ',' must escape it using an extra ',' as the escape character
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virQEMUBuildBufferEscapeComma(virBufferPtr buf, const char *str)
|
|
|
|
{
|
|
|
|
virBufferEscape(buf, ',', ",", "%s", str);
|
|
|
|
}
|
2016-06-02 15:33:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virQEMUBuildLuksOpts:
|
|
|
|
* @buf: buffer to build the string into
|
|
|
|
* @enc: pointer to encryption info
|
|
|
|
* @alias: alias to use
|
|
|
|
*
|
|
|
|
* Generate the string for id=$alias and any encryption options for
|
|
|
|
* into the buffer.
|
|
|
|
*
|
|
|
|
* Important note, a trailing comma (",") is built into the return since
|
|
|
|
* it's expected other arguments are appended after the id=$alias string.
|
|
|
|
* So either turn something like:
|
|
|
|
*
|
|
|
|
* "key-secret=$alias,"
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
* "key-secret=$alias,cipher-alg=twofish-256,cipher-mode=cbc,
|
|
|
|
* hash-alg=sha256,ivgen-alg=plain64,igven-hash-alg=sha256,"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virQEMUBuildLuksOpts(virBufferPtr buf,
|
|
|
|
virStorageEncryptionInfoDefPtr enc,
|
|
|
|
const char *alias)
|
|
|
|
{
|
|
|
|
virBufferAsprintf(buf, "key-secret=%s,", alias);
|
|
|
|
|
|
|
|
if (!enc->cipher_name)
|
|
|
|
return;
|
|
|
|
|
|
|
|
virBufferAddLit(buf, "cipher-alg=");
|
|
|
|
virQEMUBuildBufferEscapeComma(buf, enc->cipher_name);
|
|
|
|
virBufferAsprintf(buf, "-%u,", enc->cipher_size);
|
|
|
|
if (enc->cipher_mode) {
|
|
|
|
virBufferAddLit(buf, "cipher-mode=");
|
|
|
|
virQEMUBuildBufferEscapeComma(buf, enc->cipher_mode);
|
|
|
|
virBufferAddLit(buf, ",");
|
|
|
|
}
|
|
|
|
if (enc->cipher_hash) {
|
|
|
|
virBufferAddLit(buf, "hash-alg=");
|
|
|
|
virQEMUBuildBufferEscapeComma(buf, enc->cipher_hash);
|
|
|
|
virBufferAddLit(buf, ",");
|
|
|
|
}
|
|
|
|
if (!enc->ivgen_name)
|
|
|
|
return;
|
|
|
|
|
|
|
|
virBufferAddLit(buf, "ivgen-alg=");
|
|
|
|
virQEMUBuildBufferEscapeComma(buf, enc->ivgen_name);
|
|
|
|
virBufferAddLit(buf, ",");
|
|
|
|
|
|
|
|
if (enc->ivgen_hash) {
|
|
|
|
virBufferAddLit(buf, "ivgen-hash-alg=");
|
|
|
|
virQEMUBuildBufferEscapeComma(buf, enc->ivgen_hash);
|
|
|
|
virBufferAddLit(buf, ",");
|
|
|
|
}
|
|
|
|
}
|