2
0

qemu-storage-daemon.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #include "qemu/osdep.h"
  26. #include <getopt.h>
  27. #include "block/block.h"
  28. #include "block/nbd.h"
  29. #include "chardev/char.h"
  30. #include "crypto/init.h"
  31. #include "monitor/monitor.h"
  32. #include "monitor/monitor-internal.h"
  33. #include "qapi/error.h"
  34. #include "qapi/qapi-visit-block.h"
  35. #include "qapi/qapi-visit-block-core.h"
  36. #include "qapi/qapi-visit-control.h"
  37. #include "qapi/qmp/qdict.h"
  38. #include "qapi/qmp/qstring.h"
  39. #include "qapi/qobject-input-visitor.h"
  40. #include "qemu-common.h"
  41. #include "qemu-version.h"
  42. #include "qemu/config-file.h"
  43. #include "qemu/error-report.h"
  44. #include "qemu/help_option.h"
  45. #include "qemu/log.h"
  46. #include "qemu/main-loop.h"
  47. #include "qemu/module.h"
  48. #include "qemu/option.h"
  49. #include "qom/object_interfaces.h"
  50. #include "storage-daemon/qapi/qapi-commands.h"
  51. #include "storage-daemon/qapi/qapi-init-commands.h"
  52. #include "sysemu/runstate.h"
  53. #include "trace/control.h"
  54. static volatile bool exit_requested = false;
  55. void qemu_system_killed(int signal, pid_t pid)
  56. {
  57. exit_requested = true;
  58. }
  59. void qmp_quit(Error **errp)
  60. {
  61. exit_requested = true;
  62. }
  63. static void help(void)
  64. {
  65. printf(
  66. "Usage: %s [options]\n"
  67. "QEMU storage daemon\n"
  68. "\n"
  69. " -h, --help display this help and exit\n"
  70. " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
  71. " specify tracing options\n"
  72. " -V, --version output version information and exit\n"
  73. "\n"
  74. " --blockdev [driver=]<driver>[,node-name=<N>][,discard=ignore|unmap]\n"
  75. " [,cache.direct=on|off][,cache.no-flush=on|off]\n"
  76. " [,read-only=on|off][,auto-read-only=on|off]\n"
  77. " [,force-share=on|off][,detect-zeroes=on|off|unmap]\n"
  78. " [,driver specific parameters...]\n"
  79. " configure a block backend\n"
  80. "\n"
  81. " --chardev <options> configure a character device backend\n"
  82. " (see the qemu(1) man page for possible options)\n"
  83. "\n"
  84. " --export [type=]nbd,device=<node-name>[,name=<export-name>]\n"
  85. " [,writable=on|off][,bitmap=<name>]\n"
  86. " export the specified block node over NBD\n"
  87. " (requires --nbd-server)\n"
  88. "\n"
  89. " --monitor [chardev=]name[,mode=control][,pretty[=on|off]]\n"
  90. " configure a QMP monitor\n"
  91. "\n"
  92. " --nbd-server addr.type=inet,addr.host=<host>,addr.port=<port>\n"
  93. " [,tls-creds=<id>][,tls-authz=<id>]\n"
  94. " --nbd-server addr.type=unix,addr.path=<path>\n"
  95. " [,tls-creds=<id>][,tls-authz=<id>]\n"
  96. " start an NBD server for exporting block nodes\n"
  97. "\n"
  98. " --object help list object types that can be added\n"
  99. " --object <type>,help list properties for the given object type\n"
  100. " --object <type>[,<property>=<value>...]\n"
  101. " create a new object of type <type>, setting\n"
  102. " properties in the order they are specified. Note\n"
  103. " that the 'id' property must be set.\n"
  104. " See the qemu(1) man page for documentation of the\n"
  105. " objects that can be added.\n"
  106. "\n"
  107. QEMU_HELP_BOTTOM "\n",
  108. error_get_progname());
  109. }
  110. enum {
  111. OPTION_BLOCKDEV = 256,
  112. OPTION_CHARDEV,
  113. OPTION_EXPORT,
  114. OPTION_MONITOR,
  115. OPTION_NBD_SERVER,
  116. OPTION_OBJECT,
  117. };
  118. extern QemuOptsList qemu_chardev_opts;
  119. static QemuOptsList qemu_object_opts = {
  120. .name = "object",
  121. .implied_opt_name = "qom-type",
  122. .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
  123. .desc = {
  124. { }
  125. },
  126. };
  127. static void init_qmp_commands(void)
  128. {
  129. qmp_init_marshal(&qmp_commands);
  130. qmp_register_command(&qmp_commands, "query-qmp-schema",
  131. qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG);
  132. QTAILQ_INIT(&qmp_cap_negotiation_commands);
  133. qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
  134. qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
  135. }
  136. static void init_export(BlockExport *export, Error **errp)
  137. {
  138. switch (export->type) {
  139. case BLOCK_EXPORT_TYPE_NBD:
  140. qmp_nbd_server_add(&export->u.nbd, errp);
  141. break;
  142. default:
  143. g_assert_not_reached();
  144. }
  145. }
  146. static void process_options(int argc, char *argv[])
  147. {
  148. int c;
  149. static const struct option long_options[] = {
  150. {"blockdev", required_argument, NULL, OPTION_BLOCKDEV},
  151. {"chardev", required_argument, NULL, OPTION_CHARDEV},
  152. {"export", required_argument, NULL, OPTION_EXPORT},
  153. {"help", no_argument, NULL, 'h'},
  154. {"monitor", required_argument, NULL, OPTION_MONITOR},
  155. {"nbd-server", required_argument, NULL, OPTION_NBD_SERVER},
  156. {"object", required_argument, NULL, OPTION_OBJECT},
  157. {"trace", required_argument, NULL, 'T'},
  158. {"version", no_argument, NULL, 'V'},
  159. {0, 0, 0, 0}
  160. };
  161. /*
  162. * In contrast to the system emulator, options are processed in the order
  163. * they are given on the command lines. This means that things must be
  164. * defined first before they can be referenced in another option.
  165. */
  166. while ((c = getopt_long(argc, argv, "hT:V", long_options, NULL)) != -1) {
  167. switch (c) {
  168. case '?':
  169. exit(EXIT_FAILURE);
  170. case 'h':
  171. help();
  172. exit(EXIT_SUCCESS);
  173. case 'T':
  174. {
  175. char *trace_file = trace_opt_parse(optarg);
  176. trace_init_file(trace_file);
  177. g_free(trace_file);
  178. break;
  179. }
  180. case 'V':
  181. printf("qemu-storage-daemon version "
  182. QEMU_FULL_VERSION "\n" QEMU_COPYRIGHT "\n");
  183. exit(EXIT_SUCCESS);
  184. case OPTION_BLOCKDEV:
  185. {
  186. Visitor *v;
  187. BlockdevOptions *options;
  188. v = qobject_input_visitor_new_str(optarg, "driver",
  189. &error_fatal);
  190. visit_type_BlockdevOptions(v, NULL, &options, &error_fatal);
  191. visit_free(v);
  192. qmp_blockdev_add(options, &error_fatal);
  193. qapi_free_BlockdevOptions(options);
  194. break;
  195. }
  196. case OPTION_CHARDEV:
  197. {
  198. /* TODO This interface is not stable until we QAPIfy it */
  199. QemuOpts *opts = qemu_opts_parse_noisily(&qemu_chardev_opts,
  200. optarg, true);
  201. if (opts == NULL) {
  202. exit(EXIT_FAILURE);
  203. }
  204. if (!qemu_chr_new_from_opts(opts, NULL, &error_fatal)) {
  205. /* No error, but NULL returned means help was printed */
  206. exit(EXIT_SUCCESS);
  207. }
  208. qemu_opts_del(opts);
  209. break;
  210. }
  211. case OPTION_EXPORT:
  212. {
  213. Visitor *v;
  214. BlockExport *export;
  215. v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
  216. visit_type_BlockExport(v, NULL, &export, &error_fatal);
  217. visit_free(v);
  218. init_export(export, &error_fatal);
  219. qapi_free_BlockExport(export);
  220. break;
  221. }
  222. case OPTION_MONITOR:
  223. {
  224. Visitor *v;
  225. MonitorOptions *monitor;
  226. v = qobject_input_visitor_new_str(optarg, "chardev",
  227. &error_fatal);
  228. visit_type_MonitorOptions(v, NULL, &monitor, &error_fatal);
  229. visit_free(v);
  230. /* TODO Catch duplicate monitor IDs */
  231. monitor_init(monitor, false, &error_fatal);
  232. qapi_free_MonitorOptions(monitor);
  233. break;
  234. }
  235. case OPTION_NBD_SERVER:
  236. {
  237. Visitor *v;
  238. NbdServerOptions *options;
  239. v = qobject_input_visitor_new_str(optarg, NULL, &error_fatal);
  240. visit_type_NbdServerOptions(v, NULL, &options, &error_fatal);
  241. visit_free(v);
  242. nbd_server_start_options(options, &error_fatal);
  243. qapi_free_NbdServerOptions(options);
  244. break;
  245. }
  246. case OPTION_OBJECT:
  247. {
  248. QemuOpts *opts;
  249. const char *type;
  250. QDict *args;
  251. /* FIXME The keyval parser rejects 'help' arguments, so we must
  252. * unconditionall try QemuOpts first. */
  253. opts = qemu_opts_parse(&qemu_object_opts,
  254. optarg, true, &error_fatal);
  255. type = qemu_opt_get(opts, "qom-type");
  256. if (type && user_creatable_print_help(type, opts)) {
  257. exit(EXIT_SUCCESS);
  258. }
  259. qemu_opts_del(opts);
  260. args = keyval_parse(optarg, "qom-type", &error_fatal);
  261. user_creatable_add_dict(args, true, &error_fatal);
  262. qobject_unref(args);
  263. break;
  264. }
  265. default:
  266. g_assert_not_reached();
  267. }
  268. }
  269. if (optind != argc) {
  270. error_report("Unexpected argument: %s", argv[optind]);
  271. exit(EXIT_FAILURE);
  272. }
  273. }
  274. int main(int argc, char *argv[])
  275. {
  276. #ifdef CONFIG_POSIX
  277. signal(SIGPIPE, SIG_IGN);
  278. #endif
  279. error_init(argv[0]);
  280. qemu_init_exec_dir(argv[0]);
  281. os_setup_signal_handling();
  282. module_call_init(MODULE_INIT_QOM);
  283. module_call_init(MODULE_INIT_TRACE);
  284. qemu_add_opts(&qemu_object_opts);
  285. qemu_add_opts(&qemu_trace_opts);
  286. qcrypto_init(&error_fatal);
  287. bdrv_init();
  288. monitor_init_globals_core();
  289. init_qmp_commands();
  290. if (!trace_init_backends()) {
  291. return EXIT_FAILURE;
  292. }
  293. qemu_set_log(LOG_TRACE);
  294. qemu_init_main_loop(&error_fatal);
  295. process_options(argc, argv);
  296. while (!exit_requested) {
  297. main_loop_wait(false);
  298. }
  299. monitor_cleanup();
  300. qemu_chr_cleanup();
  301. user_creatable_cleanup();
  302. return EXIT_SUCCESS;
  303. }