qemu-storage-daemon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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-core.h"
  35. #include "qapi/qapi-visit-block-export.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 const char *pid_file;
  55. static volatile bool exit_requested = false;
  56. void qemu_system_killed(int signal, pid_t pid)
  57. {
  58. exit_requested = true;
  59. }
  60. void qmp_quit(Error **errp)
  61. {
  62. exit_requested = true;
  63. }
  64. static void help(void)
  65. {
  66. printf(
  67. "Usage: %s [options]\n"
  68. "QEMU storage daemon\n"
  69. "\n"
  70. " -h, --help display this help and exit\n"
  71. " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
  72. " specify tracing options\n"
  73. " -V, --version output version information and exit\n"
  74. "\n"
  75. " --blockdev [driver=]<driver>[,node-name=<N>][,discard=ignore|unmap]\n"
  76. " [,cache.direct=on|off][,cache.no-flush=on|off]\n"
  77. " [,read-only=on|off][,auto-read-only=on|off]\n"
  78. " [,force-share=on|off][,detect-zeroes=on|off|unmap]\n"
  79. " [,driver specific parameters...]\n"
  80. " configure a block backend\n"
  81. "\n"
  82. " --chardev <options> configure a character device backend\n"
  83. " (see the qemu(1) man page for possible options)\n"
  84. "\n"
  85. " --daemonize daemonize the process, and have the parent exit\n"
  86. " once startup is complete\n"
  87. "\n"
  88. " --export [type=]nbd,id=<id>,node-name=<node-name>[,name=<export-name>]\n"
  89. " [,writable=on|off][,bitmap=<name>]\n"
  90. " export the specified block node over NBD\n"
  91. " (requires --nbd-server)\n"
  92. "\n"
  93. #ifdef CONFIG_FUSE
  94. " --export [type=]fuse,id=<id>,node-name=<node-name>,mountpoint=<file>\n"
  95. " [,growable=on|off][,writable=on|off][,allow-other=on|off|auto]\n"
  96. " export the specified block node over FUSE\n"
  97. "\n"
  98. #endif /* CONFIG_FUSE */
  99. #ifdef CONFIG_VHOST_USER_BLK_SERVER
  100. " --export [type=]vhost-user-blk,id=<id>,node-name=<node-name>,\n"
  101. " addr.type=unix,addr.path=<socket-path>[,writable=on|off]\n"
  102. " [,logical-block-size=<block-size>][,num-queues=<num-queues>]\n"
  103. " export the specified block node as a\n"
  104. " vhost-user-blk device over UNIX domain socket\n"
  105. " --export [type=]vhost-user-blk,id=<id>,node-name=<node-name>,\n"
  106. " addr.type=fd,addr.str=<fd>[,writable=on|off]\n"
  107. " [,logical-block-size=<block-size>][,num-queues=<num-queues>]\n"
  108. " export the specified block node as a\n"
  109. " vhost-user-blk device over file descriptor\n"
  110. "\n"
  111. #endif /* CONFIG_VHOST_USER_BLK_SERVER */
  112. " --monitor [chardev=]name[,mode=control][,pretty[=on|off]]\n"
  113. " configure a QMP monitor\n"
  114. "\n"
  115. " --nbd-server addr.type=inet,addr.host=<host>,addr.port=<port>\n"
  116. " [,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]\n"
  117. " --nbd-server addr.type=unix,addr.path=<path>\n"
  118. " [,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]\n"
  119. " start an NBD server for exporting block nodes\n"
  120. "\n"
  121. " --object help list object types that can be added\n"
  122. " --object <type>,help list properties for the given object type\n"
  123. " --object <type>[,<property>=<value>...]\n"
  124. " create a new object of type <type>, setting\n"
  125. " properties in the order they are specified. Note\n"
  126. " that the 'id' property must be set.\n"
  127. " See the qemu(1) man page for documentation of the\n"
  128. " objects that can be added.\n"
  129. "\n"
  130. " --pidfile <path> write process ID to a file after startup\n"
  131. "\n"
  132. QEMU_HELP_BOTTOM "\n",
  133. error_get_progname());
  134. }
  135. enum {
  136. OPTION_BLOCKDEV = 256,
  137. OPTION_CHARDEV,
  138. OPTION_DAEMONIZE,
  139. OPTION_EXPORT,
  140. OPTION_MONITOR,
  141. OPTION_NBD_SERVER,
  142. OPTION_OBJECT,
  143. OPTION_PIDFILE,
  144. };
  145. extern QemuOptsList qemu_chardev_opts;
  146. static void init_qmp_commands(void)
  147. {
  148. qmp_init_marshal(&qmp_commands);
  149. QTAILQ_INIT(&qmp_cap_negotiation_commands);
  150. qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
  151. qmp_marshal_qmp_capabilities,
  152. QCO_ALLOW_PRECONFIG, 0);
  153. }
  154. static int getopt_set_loc(int argc, char **argv, const char *optstring,
  155. const struct option *longopts)
  156. {
  157. int c, save_index;
  158. optarg = NULL;
  159. save_index = optind;
  160. c = getopt_long(argc, argv, optstring, longopts, NULL);
  161. if (optarg) {
  162. loc_set_cmdline(argv, save_index, MAX(1, optind - save_index));
  163. }
  164. return c;
  165. }
  166. /**
  167. * Process QSD command-line arguments.
  168. *
  169. * This is done in two passes:
  170. *
  171. * First (@pre_init_pass is true), we do a pass where all global
  172. * arguments pertaining to the QSD process (like --help or --daemonize)
  173. * are processed. This pass is done before most of the QEMU-specific
  174. * initialization steps (e.g. initializing the block layer or QMP), and
  175. * so must only process arguments that are not really QEMU-specific.
  176. *
  177. * Second (@pre_init_pass is false), we (sequentially) process all
  178. * QEMU/QSD-specific arguments. Many of these arguments are effectively
  179. * translated to QMP commands (like --blockdev for blockdev-add, or
  180. * --export for block-export-add).
  181. */
  182. static void process_options(int argc, char *argv[], bool pre_init_pass)
  183. {
  184. int c;
  185. static const struct option long_options[] = {
  186. {"blockdev", required_argument, NULL, OPTION_BLOCKDEV},
  187. {"chardev", required_argument, NULL, OPTION_CHARDEV},
  188. {"daemonize", no_argument, NULL, OPTION_DAEMONIZE},
  189. {"export", required_argument, NULL, OPTION_EXPORT},
  190. {"help", no_argument, NULL, 'h'},
  191. {"monitor", required_argument, NULL, OPTION_MONITOR},
  192. {"nbd-server", required_argument, NULL, OPTION_NBD_SERVER},
  193. {"object", required_argument, NULL, OPTION_OBJECT},
  194. {"pidfile", required_argument, NULL, OPTION_PIDFILE},
  195. {"trace", required_argument, NULL, 'T'},
  196. {"version", no_argument, NULL, 'V'},
  197. {0, 0, 0, 0}
  198. };
  199. /*
  200. * In contrast to the system emulator, QEMU-specific options are processed
  201. * in the order they are given on the command lines. This means that things
  202. * must be defined first before they can be referenced in another option.
  203. */
  204. optind = 1;
  205. while ((c = getopt_set_loc(argc, argv, "-hT:V", long_options)) != -1) {
  206. bool handle_option_pre_init;
  207. /* Should this argument be processed in the pre-init pass? */
  208. handle_option_pre_init =
  209. c == '?' ||
  210. c == 'h' ||
  211. c == 'V' ||
  212. c == OPTION_DAEMONIZE ||
  213. c == OPTION_PIDFILE;
  214. /* Process every option only in its respective pass */
  215. if (pre_init_pass != handle_option_pre_init) {
  216. continue;
  217. }
  218. switch (c) {
  219. case '?':
  220. exit(EXIT_FAILURE);
  221. case 'h':
  222. help();
  223. exit(EXIT_SUCCESS);
  224. case 'T':
  225. trace_opt_parse(optarg);
  226. trace_init_file();
  227. break;
  228. case 'V':
  229. printf("qemu-storage-daemon version "
  230. QEMU_FULL_VERSION "\n" QEMU_COPYRIGHT "\n");
  231. exit(EXIT_SUCCESS);
  232. case OPTION_BLOCKDEV:
  233. {
  234. Visitor *v;
  235. BlockdevOptions *options;
  236. v = qobject_input_visitor_new_str(optarg, "driver",
  237. &error_fatal);
  238. visit_type_BlockdevOptions(v, NULL, &options, &error_fatal);
  239. visit_free(v);
  240. qmp_blockdev_add(options, &error_fatal);
  241. qapi_free_BlockdevOptions(options);
  242. break;
  243. }
  244. case OPTION_CHARDEV:
  245. {
  246. /* TODO This interface is not stable until we QAPIfy it */
  247. QemuOpts *opts = qemu_opts_parse_noisily(&qemu_chardev_opts,
  248. optarg, true);
  249. if (opts == NULL) {
  250. exit(EXIT_FAILURE);
  251. }
  252. if (!qemu_chr_new_from_opts(opts, NULL, &error_fatal)) {
  253. /* No error, but NULL returned means help was printed */
  254. exit(EXIT_SUCCESS);
  255. }
  256. qemu_opts_del(opts);
  257. break;
  258. }
  259. case OPTION_DAEMONIZE:
  260. if (os_set_daemonize(true) < 0) {
  261. error_report("--daemonize not supported in this build");
  262. exit(EXIT_FAILURE);
  263. }
  264. break;
  265. case OPTION_EXPORT:
  266. {
  267. Visitor *v;
  268. BlockExportOptions *export;
  269. v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
  270. visit_type_BlockExportOptions(v, NULL, &export, &error_fatal);
  271. visit_free(v);
  272. qmp_block_export_add(export, &error_fatal);
  273. qapi_free_BlockExportOptions(export);
  274. break;
  275. }
  276. case OPTION_MONITOR:
  277. {
  278. Visitor *v;
  279. MonitorOptions *monitor;
  280. v = qobject_input_visitor_new_str(optarg, "chardev",
  281. &error_fatal);
  282. visit_type_MonitorOptions(v, NULL, &monitor, &error_fatal);
  283. visit_free(v);
  284. /* TODO Catch duplicate monitor IDs */
  285. monitor_init(monitor, false, &error_fatal);
  286. qapi_free_MonitorOptions(monitor);
  287. break;
  288. }
  289. case OPTION_NBD_SERVER:
  290. {
  291. Visitor *v;
  292. NbdServerOptions *options;
  293. v = qobject_input_visitor_new_str(optarg, NULL, &error_fatal);
  294. visit_type_NbdServerOptions(v, NULL, &options, &error_fatal);
  295. visit_free(v);
  296. nbd_server_start_options(options, &error_fatal);
  297. qapi_free_NbdServerOptions(options);
  298. break;
  299. }
  300. case OPTION_OBJECT:
  301. user_creatable_process_cmdline(optarg);
  302. break;
  303. case OPTION_PIDFILE:
  304. pid_file = optarg;
  305. break;
  306. case 1:
  307. error_report("Unexpected argument");
  308. exit(EXIT_FAILURE);
  309. default:
  310. g_assert_not_reached();
  311. }
  312. }
  313. loc_set_none();
  314. }
  315. static void pid_file_cleanup(void)
  316. {
  317. unlink(pid_file);
  318. }
  319. static void pid_file_init(void)
  320. {
  321. Error *err = NULL;
  322. if (!pid_file) {
  323. return;
  324. }
  325. if (!qemu_write_pidfile(pid_file, &err)) {
  326. error_reportf_err(err, "cannot create PID file: ");
  327. exit(EXIT_FAILURE);
  328. }
  329. atexit(pid_file_cleanup);
  330. }
  331. int main(int argc, char *argv[])
  332. {
  333. #ifdef CONFIG_POSIX
  334. signal(SIGPIPE, SIG_IGN);
  335. #endif
  336. error_init(argv[0]);
  337. qemu_init_exec_dir(argv[0]);
  338. os_setup_signal_handling();
  339. process_options(argc, argv, true);
  340. os_daemonize();
  341. module_call_init(MODULE_INIT_QOM);
  342. module_call_init(MODULE_INIT_TRACE);
  343. qemu_add_opts(&qemu_trace_opts);
  344. qcrypto_init(&error_fatal);
  345. bdrv_init();
  346. monitor_init_globals_core();
  347. init_qmp_commands();
  348. if (!trace_init_backends()) {
  349. return EXIT_FAILURE;
  350. }
  351. qemu_set_log(LOG_TRACE);
  352. qemu_init_main_loop(&error_fatal);
  353. process_options(argc, argv, false);
  354. /*
  355. * Write the pid file after creating chardevs, exports, and NBD servers but
  356. * before accepting connections. This ordering is documented. Do not change
  357. * it.
  358. */
  359. pid_file_init();
  360. os_setup_post();
  361. while (!exit_requested) {
  362. main_loop_wait(false);
  363. }
  364. blk_exp_close_all();
  365. bdrv_drain_all_begin();
  366. job_cancel_sync_all();
  367. bdrv_close_all();
  368. monitor_cleanup();
  369. qemu_chr_cleanup();
  370. user_creatable_cleanup();
  371. return EXIT_SUCCESS;
  372. }