2020-02-24 17:29:49 +03:00
|
|
|
/*
|
|
|
|
* QEMU storage daemon
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
|
|
* Copyright (c) 2019 Kevin Wolf <kwolf@redhat.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include "block/block.h"
|
2020-02-24 17:29:57 +03:00
|
|
|
#include "block/nbd.h"
|
2020-02-24 17:30:01 +03:00
|
|
|
#include "chardev/char.h"
|
2020-02-24 17:29:49 +03:00
|
|
|
#include "crypto/init.h"
|
2020-02-24 17:30:08 +03:00
|
|
|
#include "monitor/monitor.h"
|
|
|
|
#include "monitor/monitor-internal.h"
|
2020-02-24 17:29:49 +03:00
|
|
|
|
|
|
|
#include "qapi/error.h"
|
2020-02-24 17:29:57 +03:00
|
|
|
#include "qapi/qapi-visit-block-core.h"
|
2020-09-24 18:26:48 +03:00
|
|
|
#include "qapi/qapi-visit-block-export.h"
|
2020-02-24 17:30:08 +03:00
|
|
|
#include "qapi/qapi-visit-control.h"
|
2020-02-24 17:29:56 +03:00
|
|
|
#include "qapi/qmp/qdict.h"
|
2020-02-24 17:30:08 +03:00
|
|
|
#include "qapi/qmp/qstring.h"
|
2020-02-24 17:29:54 +03:00
|
|
|
#include "qapi/qobject-input-visitor.h"
|
|
|
|
|
2020-02-24 17:29:49 +03:00
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "qemu-version.h"
|
|
|
|
#include "qemu/config-file.h"
|
|
|
|
#include "qemu/error-report.h"
|
2020-02-24 17:29:56 +03:00
|
|
|
#include "qemu/help_option.h"
|
2020-02-24 17:29:49 +03:00
|
|
|
#include "qemu/log.h"
|
|
|
|
#include "qemu/main-loop.h"
|
|
|
|
#include "qemu/module.h"
|
2020-02-24 17:29:56 +03:00
|
|
|
#include "qemu/option.h"
|
|
|
|
#include "qom/object_interfaces.h"
|
2020-02-24 17:29:49 +03:00
|
|
|
|
2020-02-24 17:30:08 +03:00
|
|
|
#include "storage-daemon/qapi/qapi-commands.h"
|
|
|
|
#include "storage-daemon/qapi/qapi-init-commands.h"
|
|
|
|
|
2020-02-24 17:30:00 +03:00
|
|
|
#include "sysemu/runstate.h"
|
2020-02-24 17:29:49 +03:00
|
|
|
#include "trace/control.h"
|
|
|
|
|
2020-02-24 17:30:00 +03:00
|
|
|
static volatile bool exit_requested = false;
|
|
|
|
|
|
|
|
void qemu_system_killed(int signal, pid_t pid)
|
|
|
|
{
|
|
|
|
exit_requested = true;
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:30:08 +03:00
|
|
|
void qmp_quit(Error **errp)
|
|
|
|
{
|
|
|
|
exit_requested = true;
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:29:49 +03:00
|
|
|
static void help(void)
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"Usage: %s [options]\n"
|
|
|
|
"QEMU storage daemon\n"
|
|
|
|
"\n"
|
|
|
|
" -h, --help display this help and exit\n"
|
|
|
|
" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
|
|
|
|
" specify tracing options\n"
|
|
|
|
" -V, --version output version information and exit\n"
|
|
|
|
"\n"
|
2020-02-24 17:29:54 +03:00
|
|
|
" --blockdev [driver=]<driver>[,node-name=<N>][,discard=ignore|unmap]\n"
|
|
|
|
" [,cache.direct=on|off][,cache.no-flush=on|off]\n"
|
|
|
|
" [,read-only=on|off][,auto-read-only=on|off]\n"
|
|
|
|
" [,force-share=on|off][,detect-zeroes=on|off|unmap]\n"
|
|
|
|
" [,driver specific parameters...]\n"
|
|
|
|
" configure a block backend\n"
|
|
|
|
"\n"
|
2020-02-24 17:30:01 +03:00
|
|
|
" --chardev <options> configure a character device backend\n"
|
|
|
|
" (see the qemu(1) man page for possible options)\n"
|
|
|
|
"\n"
|
2020-09-30 16:39:09 +03:00
|
|
|
" --export [type=]nbd,id=<id>,node-name=<node-name>[,name=<export-name>]\n"
|
2020-02-24 17:29:59 +03:00
|
|
|
" [,writable=on|off][,bitmap=<name>]\n"
|
|
|
|
" export the specified block node over NBD\n"
|
|
|
|
" (requires --nbd-server)\n"
|
|
|
|
"\n"
|
2020-02-24 17:30:08 +03:00
|
|
|
" --monitor [chardev=]name[,mode=control][,pretty[=on|off]]\n"
|
|
|
|
" configure a QMP monitor\n"
|
|
|
|
"\n"
|
2020-02-24 17:29:57 +03:00
|
|
|
" --nbd-server addr.type=inet,addr.host=<host>,addr.port=<port>\n"
|
2020-09-24 18:26:54 +03:00
|
|
|
" [,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]\n"
|
2020-02-24 17:29:57 +03:00
|
|
|
" --nbd-server addr.type=unix,addr.path=<path>\n"
|
2020-09-24 18:26:54 +03:00
|
|
|
" [,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]\n"
|
2020-02-24 17:29:57 +03:00
|
|
|
" start an NBD server for exporting block nodes\n"
|
|
|
|
"\n"
|
2020-02-24 17:29:56 +03:00
|
|
|
" --object help list object types that can be added\n"
|
|
|
|
" --object <type>,help list properties for the given object type\n"
|
|
|
|
" --object <type>[,<property>=<value>...]\n"
|
|
|
|
" create a new object of type <type>, setting\n"
|
|
|
|
" properties in the order they are specified. Note\n"
|
|
|
|
" that the 'id' property must be set.\n"
|
|
|
|
" See the qemu(1) man page for documentation of the\n"
|
|
|
|
" objects that can be added.\n"
|
|
|
|
"\n"
|
2020-02-24 17:29:49 +03:00
|
|
|
QEMU_HELP_BOTTOM "\n",
|
|
|
|
error_get_progname());
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:29:54 +03:00
|
|
|
enum {
|
|
|
|
OPTION_BLOCKDEV = 256,
|
2020-02-24 17:30:01 +03:00
|
|
|
OPTION_CHARDEV,
|
2020-02-24 17:29:59 +03:00
|
|
|
OPTION_EXPORT,
|
2020-02-24 17:30:08 +03:00
|
|
|
OPTION_MONITOR,
|
2020-02-24 17:29:57 +03:00
|
|
|
OPTION_NBD_SERVER,
|
2020-02-24 17:29:56 +03:00
|
|
|
OPTION_OBJECT,
|
|
|
|
};
|
|
|
|
|
2020-02-24 17:30:01 +03:00
|
|
|
extern QemuOptsList qemu_chardev_opts;
|
|
|
|
|
2020-02-24 17:29:56 +03:00
|
|
|
static QemuOptsList qemu_object_opts = {
|
|
|
|
.name = "object",
|
|
|
|
.implied_opt_name = "qom-type",
|
|
|
|
.head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
|
|
|
|
.desc = {
|
|
|
|
{ }
|
|
|
|
},
|
2020-02-24 17:29:54 +03:00
|
|
|
};
|
|
|
|
|
2020-02-24 17:30:08 +03:00
|
|
|
static void init_qmp_commands(void)
|
|
|
|
{
|
|
|
|
qmp_init_marshal(&qmp_commands);
|
|
|
|
qmp_register_command(&qmp_commands, "query-qmp-schema",
|
|
|
|
qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG);
|
|
|
|
|
|
|
|
QTAILQ_INIT(&qmp_cap_negotiation_commands);
|
|
|
|
qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
|
|
|
|
qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:29:49 +03:00
|
|
|
static void process_options(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
static const struct option long_options[] = {
|
2020-02-24 17:29:54 +03:00
|
|
|
{"blockdev", required_argument, NULL, OPTION_BLOCKDEV},
|
2020-02-24 17:30:01 +03:00
|
|
|
{"chardev", required_argument, NULL, OPTION_CHARDEV},
|
2020-02-24 17:29:59 +03:00
|
|
|
{"export", required_argument, NULL, OPTION_EXPORT},
|
2020-02-24 17:29:49 +03:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
2020-02-24 17:30:08 +03:00
|
|
|
{"monitor", required_argument, NULL, OPTION_MONITOR},
|
2020-02-24 17:29:57 +03:00
|
|
|
{"nbd-server", required_argument, NULL, OPTION_NBD_SERVER},
|
2020-02-24 17:29:56 +03:00
|
|
|
{"object", required_argument, NULL, OPTION_OBJECT},
|
2020-02-24 17:29:49 +03:00
|
|
|
{"trace", required_argument, NULL, 'T'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In contrast to the system emulator, options are processed in the order
|
|
|
|
* they are given on the command lines. This means that things must be
|
|
|
|
* defined first before they can be referenced in another option.
|
|
|
|
*/
|
|
|
|
while ((c = getopt_long(argc, argv, "hT:V", long_options, NULL)) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case '?':
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
case 'h':
|
|
|
|
help();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case 'T':
|
|
|
|
{
|
|
|
|
char *trace_file = trace_opt_parse(optarg);
|
|
|
|
trace_init_file(trace_file);
|
|
|
|
g_free(trace_file);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'V':
|
|
|
|
printf("qemu-storage-daemon version "
|
|
|
|
QEMU_FULL_VERSION "\n" QEMU_COPYRIGHT "\n");
|
|
|
|
exit(EXIT_SUCCESS);
|
2020-02-24 17:29:54 +03:00
|
|
|
case OPTION_BLOCKDEV:
|
|
|
|
{
|
|
|
|
Visitor *v;
|
|
|
|
BlockdevOptions *options;
|
|
|
|
|
|
|
|
v = qobject_input_visitor_new_str(optarg, "driver",
|
|
|
|
&error_fatal);
|
|
|
|
|
|
|
|
visit_type_BlockdevOptions(v, NULL, &options, &error_fatal);
|
|
|
|
visit_free(v);
|
|
|
|
|
|
|
|
qmp_blockdev_add(options, &error_fatal);
|
|
|
|
qapi_free_BlockdevOptions(options);
|
|
|
|
break;
|
|
|
|
}
|
2020-02-24 17:30:01 +03:00
|
|
|
case OPTION_CHARDEV:
|
|
|
|
{
|
|
|
|
/* TODO This interface is not stable until we QAPIfy it */
|
|
|
|
QemuOpts *opts = qemu_opts_parse_noisily(&qemu_chardev_opts,
|
|
|
|
optarg, true);
|
|
|
|
if (opts == NULL) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!qemu_chr_new_from_opts(opts, NULL, &error_fatal)) {
|
|
|
|
/* No error, but NULL returned means help was printed */
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
qemu_opts_del(opts);
|
|
|
|
break;
|
|
|
|
}
|
2020-02-24 17:29:59 +03:00
|
|
|
case OPTION_EXPORT:
|
|
|
|
{
|
|
|
|
Visitor *v;
|
2020-09-24 18:26:49 +03:00
|
|
|
BlockExportOptions *export;
|
2020-02-24 17:29:59 +03:00
|
|
|
|
|
|
|
v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
|
2020-09-24 18:26:49 +03:00
|
|
|
visit_type_BlockExportOptions(v, NULL, &export, &error_fatal);
|
2020-02-24 17:29:59 +03:00
|
|
|
visit_free(v);
|
|
|
|
|
2020-09-24 18:26:51 +03:00
|
|
|
qmp_block_export_add(export, &error_fatal);
|
2020-09-24 18:26:49 +03:00
|
|
|
qapi_free_BlockExportOptions(export);
|
2020-02-24 17:29:59 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-02-24 17:30:08 +03:00
|
|
|
case OPTION_MONITOR:
|
|
|
|
{
|
|
|
|
Visitor *v;
|
|
|
|
MonitorOptions *monitor;
|
|
|
|
|
|
|
|
v = qobject_input_visitor_new_str(optarg, "chardev",
|
|
|
|
&error_fatal);
|
|
|
|
visit_type_MonitorOptions(v, NULL, &monitor, &error_fatal);
|
|
|
|
visit_free(v);
|
|
|
|
|
|
|
|
/* TODO Catch duplicate monitor IDs */
|
|
|
|
monitor_init(monitor, false, &error_fatal);
|
|
|
|
qapi_free_MonitorOptions(monitor);
|
|
|
|
break;
|
|
|
|
}
|
2020-02-24 17:29:57 +03:00
|
|
|
case OPTION_NBD_SERVER:
|
|
|
|
{
|
|
|
|
Visitor *v;
|
|
|
|
NbdServerOptions *options;
|
|
|
|
|
|
|
|
v = qobject_input_visitor_new_str(optarg, NULL, &error_fatal);
|
|
|
|
visit_type_NbdServerOptions(v, NULL, &options, &error_fatal);
|
|
|
|
visit_free(v);
|
|
|
|
|
|
|
|
nbd_server_start_options(options, &error_fatal);
|
|
|
|
qapi_free_NbdServerOptions(options);
|
|
|
|
break;
|
|
|
|
}
|
2020-02-24 17:29:56 +03:00
|
|
|
case OPTION_OBJECT:
|
|
|
|
{
|
|
|
|
QDict *args;
|
2020-10-07 19:49:03 +03:00
|
|
|
bool help;
|
2020-02-24 17:29:56 +03:00
|
|
|
|
2020-10-07 19:49:03 +03:00
|
|
|
args = keyval_parse(optarg, "qom-type", &help, &error_fatal);
|
|
|
|
if (help) {
|
|
|
|
user_creatable_print_help_from_qdict(args);
|
2020-02-24 17:29:56 +03:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
2020-04-16 18:26:06 +03:00
|
|
|
user_creatable_add_dict(args, true, &error_fatal);
|
2020-02-24 17:29:56 +03:00
|
|
|
qobject_unref(args);
|
|
|
|
break;
|
|
|
|
}
|
2020-02-24 17:29:49 +03:00
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (optind != argc) {
|
|
|
|
error_report("Unexpected argument: %s", argv[optind]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_POSIX
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
error_init(argv[0]);
|
|
|
|
qemu_init_exec_dir(argv[0]);
|
2020-02-24 17:30:00 +03:00
|
|
|
os_setup_signal_handling();
|
2020-02-24 17:29:49 +03:00
|
|
|
|
|
|
|
module_call_init(MODULE_INIT_QOM);
|
|
|
|
module_call_init(MODULE_INIT_TRACE);
|
2020-06-19 13:11:31 +03:00
|
|
|
qemu_add_opts(&qemu_object_opts);
|
2020-02-24 17:29:49 +03:00
|
|
|
qemu_add_opts(&qemu_trace_opts);
|
|
|
|
qcrypto_init(&error_fatal);
|
|
|
|
bdrv_init();
|
2020-02-24 17:30:08 +03:00
|
|
|
monitor_init_globals_core();
|
|
|
|
init_qmp_commands();
|
2020-02-24 17:29:49 +03:00
|
|
|
|
|
|
|
if (!trace_init_backends()) {
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
qemu_set_log(LOG_TRACE);
|
|
|
|
|
|
|
|
qemu_init_main_loop(&error_fatal);
|
|
|
|
process_options(argc, argv);
|
|
|
|
|
2020-02-24 17:30:00 +03:00
|
|
|
while (!exit_requested) {
|
|
|
|
main_loop_wait(false);
|
|
|
|
}
|
|
|
|
|
2020-06-19 13:11:32 +03:00
|
|
|
monitor_cleanup();
|
|
|
|
qemu_chr_cleanup();
|
|
|
|
user_creatable_cleanup();
|
|
|
|
|
2020-02-24 17:29:49 +03:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|