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