qemu-storage-daemon: Add --blockdev option
[qemu.git] / qemu-storage-daemon.c
1 /*
2 * QEMU storage daemon
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2019 Kevin Wolf <kwolf@redhat.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26 #include "qemu/osdep.h"
27
28 #include <getopt.h>
29
30 #include "block/block.h"
31 #include "crypto/init.h"
32
33 #include "qapi/error.h"
34 #include "qapi/qapi-visit-block-core.h"
35 #include "qapi/qapi-commands-block-core.h"
36 #include "qapi/qobject-input-visitor.h"
37
38 #include "qemu-common.h"
39 #include "qemu-version.h"
40 #include "qemu/config-file.h"
41 #include "qemu/error-report.h"
42 #include "qemu/log.h"
43 #include "qemu/main-loop.h"
44 #include "qemu/module.h"
45
46 #include "trace/control.h"
47
48 static void help(void)
49 {
50 printf(
51 "Usage: %s [options]\n"
52 "QEMU storage daemon\n"
53 "\n"
54 " -h, --help display this help and exit\n"
55 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
56 " specify tracing options\n"
57 " -V, --version output version information and exit\n"
58 "\n"
59 " --blockdev [driver=]<driver>[,node-name=<N>][,discard=ignore|unmap]\n"
60 " [,cache.direct=on|off][,cache.no-flush=on|off]\n"
61 " [,read-only=on|off][,auto-read-only=on|off]\n"
62 " [,force-share=on|off][,detect-zeroes=on|off|unmap]\n"
63 " [,driver specific parameters...]\n"
64 " configure a block backend\n"
65 "\n"
66 QEMU_HELP_BOTTOM "\n",
67 error_get_progname());
68 }
69
70 enum {
71 OPTION_BLOCKDEV = 256,
72 };
73
74 static void process_options(int argc, char *argv[])
75 {
76 int c;
77
78 static const struct option long_options[] = {
79 {"blockdev", required_argument, NULL, OPTION_BLOCKDEV},
80 {"help", no_argument, NULL, 'h'},
81 {"trace", required_argument, NULL, 'T'},
82 {"version", no_argument, NULL, 'V'},
83 {0, 0, 0, 0}
84 };
85
86 /*
87 * In contrast to the system emulator, options are processed in the order
88 * they are given on the command lines. This means that things must be
89 * defined first before they can be referenced in another option.
90 */
91 while ((c = getopt_long(argc, argv, "hT:V", long_options, NULL)) != -1) {
92 switch (c) {
93 case '?':
94 exit(EXIT_FAILURE);
95 case 'h':
96 help();
97 exit(EXIT_SUCCESS);
98 case 'T':
99 {
100 char *trace_file = trace_opt_parse(optarg);
101 trace_init_file(trace_file);
102 g_free(trace_file);
103 break;
104 }
105 case 'V':
106 printf("qemu-storage-daemon version "
107 QEMU_FULL_VERSION "\n" QEMU_COPYRIGHT "\n");
108 exit(EXIT_SUCCESS);
109 case OPTION_BLOCKDEV:
110 {
111 Visitor *v;
112 BlockdevOptions *options;
113
114 v = qobject_input_visitor_new_str(optarg, "driver",
115 &error_fatal);
116
117 visit_type_BlockdevOptions(v, NULL, &options, &error_fatal);
118 visit_free(v);
119
120 qmp_blockdev_add(options, &error_fatal);
121 qapi_free_BlockdevOptions(options);
122 break;
123 }
124 default:
125 g_assert_not_reached();
126 }
127 }
128 if (optind != argc) {
129 error_report("Unexpected argument: %s", argv[optind]);
130 exit(EXIT_FAILURE);
131 }
132 }
133
134 int main(int argc, char *argv[])
135 {
136 #ifdef CONFIG_POSIX
137 signal(SIGPIPE, SIG_IGN);
138 #endif
139
140 error_init(argv[0]);
141 qemu_init_exec_dir(argv[0]);
142
143 module_call_init(MODULE_INIT_QOM);
144 module_call_init(MODULE_INIT_TRACE);
145 qemu_add_opts(&qemu_trace_opts);
146 qcrypto_init(&error_fatal);
147 bdrv_init();
148
149 if (!trace_init_backends()) {
150 return EXIT_FAILURE;
151 }
152 qemu_set_log(LOG_TRACE);
153
154 qemu_init_main_loop(&error_fatal);
155 process_options(argc, argv);
156
157 return EXIT_SUCCESS;
158 }