qemu-storage-daemon.c 15 KB

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