]>
Commit | Line | Data |
---|---|---|
e3aff4f6 AL |
1 | /* |
2 | * Command line utility to exercise the QEMU I/O path. | |
3 | * | |
4 | * Copyright (C) 2009 Red Hat, Inc. | |
5 | * Copyright (c) 2003-2005 Silicon Graphics, Inc. | |
6 | * | |
7 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
8 | * See the COPYING file in the top-level directory. | |
9 | */ | |
80c71a24 | 10 | #include "qemu/osdep.h" |
e3aff4f6 | 11 | #include <getopt.h> |
c32d766a | 12 | #include <libgen.h> |
e3aff4f6 | 13 | |
da34e65c | 14 | #include "qapi/error.h" |
3d21994f | 15 | #include "qemu-io.h" |
d49b6836 | 16 | #include "qemu/error-report.h" |
1de7afc9 | 17 | #include "qemu/main-loop.h" |
b543c5cd HR |
18 | #include "qemu/option.h" |
19 | #include "qemu/config-file.h" | |
0cf17e18 | 20 | #include "qemu/readline.h" |
e9a80859 | 21 | #include "qemu/log.h" |
d49b6836 | 22 | #include "qapi/qmp/qstring.h" |
459571f7 | 23 | #include "qapi/qmp/qbool.h" |
9ba371b6 | 24 | #include "qom/object_interfaces.h" |
26f54e9a | 25 | #include "sysemu/block-backend.h" |
737e150e | 26 | #include "block/block_int.h" |
d7bb72c8 | 27 | #include "trace/control.h" |
c2297088 | 28 | #include "crypto/init.h" |
4face32a | 29 | #include "qemu-version.h" |
e3aff4f6 | 30 | |
43642b38 | 31 | #define CMD_NOFILE_OK 0x01 |
e3aff4f6 | 32 | |
f9883880 | 33 | static char *progname; |
e3aff4f6 | 34 | |
26f54e9a | 35 | static BlockBackend *qemuio_blk; |
191c2890 | 36 | |
d1174f13 KW |
37 | /* qemu-io commands passed using -c */ |
38 | static int ncmdline; | |
39 | static char **cmdline; | |
499afa25 | 40 | static bool imageOpts; |
d1174f13 | 41 | |
0cf17e18 SH |
42 | static ReadLineState *readline_state; |
43 | ||
4c7b7e9b | 44 | static int close_f(BlockBackend *blk, int argc, char **argv) |
e3aff4f6 | 45 | { |
26f54e9a | 46 | blk_unref(qemuio_blk); |
26f54e9a | 47 | qemuio_blk = NULL; |
43642b38 | 48 | return 0; |
e3aff4f6 AL |
49 | } |
50 | ||
51 | static const cmdinfo_t close_cmd = { | |
43642b38 DN |
52 | .name = "close", |
53 | .altname = "c", | |
54 | .cfunc = close_f, | |
55 | .oneline = "close the current open file", | |
e3aff4f6 AL |
56 | }; |
57 | ||
459571f7 FZ |
58 | static int openfile(char *name, int flags, bool writethrough, bool force_share, |
59 | QDict *opts) | |
e3aff4f6 | 60 | { |
34b5d2c6 HR |
61 | Error *local_err = NULL; |
62 | ||
1b58b438 | 63 | if (qemuio_blk) { |
b9884681 | 64 | error_report("file open already, try 'help close'"); |
29f2601a | 65 | QDECREF(opts); |
43642b38 DN |
66 | return 1; |
67 | } | |
68 | ||
459571f7 FZ |
69 | if (force_share) { |
70 | if (!opts) { | |
71 | opts = qdict_new(); | |
72 | } | |
73 | if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE) | |
74 | && !qdict_get_bool(opts, BDRV_OPT_FORCE_SHARE)) { | |
75 | error_report("-U conflicts with image options"); | |
76 | QDECREF(opts); | |
77 | return 1; | |
78 | } | |
579cf1d1 | 79 | qdict_put_bool(opts, BDRV_OPT_FORCE_SHARE, true); |
459571f7 | 80 | } |
efaa7c4e | 81 | qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err); |
1b58b438 | 82 | if (!qemuio_blk) { |
b9884681 MA |
83 | error_reportf_err(local_err, "can't open%s%s: ", |
84 | name ? " device " : "", name ?: ""); | |
dbb651c4 | 85 | return 1; |
43642b38 DN |
86 | } |
87 | ||
e151fc16 | 88 | blk_set_enable_write_cache(qemuio_blk, !writethrough); |
8caf0212 | 89 | |
43642b38 | 90 | return 0; |
e3aff4f6 AL |
91 | } |
92 | ||
43642b38 | 93 | static void open_help(void) |
e3aff4f6 | 94 | { |
43642b38 | 95 | printf( |
e3aff4f6 AL |
96 | "\n" |
97 | " opens a new file in the requested mode\n" | |
98 | "\n" | |
99 | " Example:\n" | |
e4e12bb2 | 100 | " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n" |
e3aff4f6 AL |
101 | "\n" |
102 | " Opens a file for subsequent use by all of the other qemu-io commands.\n" | |
e3aff4f6 AL |
103 | " -r, -- open file read-only\n" |
104 | " -s, -- use snapshot file\n" | |
b8d970f1 | 105 | " -n, -- disable host cache, short for -t none\n" |
459571f7 | 106 | " -U, -- force shared permissions\n" |
b8d970f1 EB |
107 | " -k, -- use kernel AIO implementation (on Linux only)\n" |
108 | " -t, -- use the given cache mode for the image\n" | |
109 | " -d, -- use the given discard mode for the image\n" | |
b543c5cd | 110 | " -o, -- options to be given to the block driver" |
e3aff4f6 AL |
111 | "\n"); |
112 | } | |
113 | ||
4c7b7e9b | 114 | static int open_f(BlockBackend *blk, int argc, char **argv); |
22a2bdcb BS |
115 | |
116 | static const cmdinfo_t open_cmd = { | |
43642b38 DN |
117 | .name = "open", |
118 | .altname = "o", | |
119 | .cfunc = open_f, | |
120 | .argmin = 1, | |
121 | .argmax = -1, | |
122 | .flags = CMD_NOFILE_OK, | |
459571f7 | 123 | .args = "[-rsnkU] [-t cache] [-d discard] [-o options] [path]", |
43642b38 DN |
124 | .oneline = "open the file specified by path", |
125 | .help = open_help, | |
22a2bdcb | 126 | }; |
e3aff4f6 | 127 | |
b543c5cd HR |
128 | static QemuOptsList empty_opts = { |
129 | .name = "drive", | |
443422fd | 130 | .merge_lists = true, |
b543c5cd HR |
131 | .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head), |
132 | .desc = { | |
133 | /* no elements => accept any params */ | |
134 | { /* end of list */ } | |
135 | }, | |
136 | }; | |
137 | ||
4c7b7e9b | 138 | static int open_f(BlockBackend *blk, int argc, char **argv) |
e3aff4f6 | 139 | { |
b8d970f1 | 140 | int flags = BDRV_O_UNMAP; |
43642b38 | 141 | int readonly = 0; |
e151fc16 | 142 | bool writethrough = true; |
43642b38 | 143 | int c; |
b543c5cd | 144 | QemuOpts *qopts; |
443422fd | 145 | QDict *opts; |
459571f7 | 146 | bool force_share = false; |
43642b38 | 147 | |
459571f7 | 148 | while ((c = getopt(argc, argv, "snro:kt:d:U")) != -1) { |
43642b38 DN |
149 | switch (c) { |
150 | case 's': | |
151 | flags |= BDRV_O_SNAPSHOT; | |
152 | break; | |
153 | case 'n': | |
e151fc16 KW |
154 | flags |= BDRV_O_NOCACHE; |
155 | writethrough = false; | |
43642b38 DN |
156 | break; |
157 | case 'r': | |
158 | readonly = 1; | |
159 | break; | |
b8d970f1 EB |
160 | case 'k': |
161 | flags |= BDRV_O_NATIVE_AIO; | |
162 | break; | |
163 | case 't': | |
164 | if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) { | |
165 | error_report("Invalid cache option: %s", optarg); | |
166 | qemu_opts_reset(&empty_opts); | |
167 | return 0; | |
168 | } | |
169 | break; | |
170 | case 'd': | |
171 | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { | |
172 | error_report("Invalid discard option: %s", optarg); | |
173 | qemu_opts_reset(&empty_opts); | |
174 | return 0; | |
175 | } | |
176 | break; | |
b543c5cd | 177 | case 'o': |
499afa25 DB |
178 | if (imageOpts) { |
179 | printf("--image-opts and 'open -o' are mutually exclusive\n"); | |
b8d970f1 | 180 | qemu_opts_reset(&empty_opts); |
499afa25 DB |
181 | return 0; |
182 | } | |
70b94331 | 183 | if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) { |
443422fd | 184 | qemu_opts_reset(&empty_opts); |
b543c5cd HR |
185 | return 0; |
186 | } | |
b543c5cd | 187 | break; |
459571f7 FZ |
188 | case 'U': |
189 | force_share = true; | |
190 | break; | |
43642b38 | 191 | default: |
443422fd | 192 | qemu_opts_reset(&empty_opts); |
c2cdf5c5 | 193 | return qemuio_command_usage(&open_cmd); |
f5edb014 | 194 | } |
43642b38 DN |
195 | } |
196 | ||
197 | if (!readonly) { | |
198 | flags |= BDRV_O_RDWR; | |
199 | } | |
e3aff4f6 | 200 | |
499afa25 DB |
201 | if (imageOpts && (optind == argc - 1)) { |
202 | if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) { | |
203 | qemu_opts_reset(&empty_opts); | |
204 | return 0; | |
205 | } | |
206 | optind++; | |
207 | } | |
208 | ||
443422fd MA |
209 | qopts = qemu_opts_find(&empty_opts, NULL); |
210 | opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL; | |
211 | qemu_opts_reset(&empty_opts); | |
212 | ||
fd0fee34 | 213 | if (optind == argc - 1) { |
64ebf556 | 214 | openfile(argv[optind], flags, writethrough, force_share, opts); |
fd0fee34 | 215 | } else if (optind == argc) { |
64ebf556 | 216 | openfile(NULL, flags, writethrough, force_share, opts); |
fd0fee34 | 217 | } else { |
29f2601a | 218 | QDECREF(opts); |
64ebf556 | 219 | qemuio_command_usage(&open_cmd); |
43642b38 | 220 | } |
64ebf556 | 221 | return 0; |
e3aff4f6 AL |
222 | } |
223 | ||
4c7b7e9b | 224 | static int quit_f(BlockBackend *blk, int argc, char **argv) |
e681be7e KW |
225 | { |
226 | return 1; | |
227 | } | |
228 | ||
229 | static const cmdinfo_t quit_cmd = { | |
230 | .name = "quit", | |
231 | .altname = "q", | |
232 | .cfunc = quit_f, | |
233 | .argmin = -1, | |
234 | .argmax = -1, | |
235 | .flags = CMD_FLAG_GLOBAL, | |
236 | .oneline = "exit the program", | |
237 | }; | |
238 | ||
e3aff4f6 AL |
239 | static void usage(const char *name) |
240 | { | |
43642b38 | 241 | printf( |
e4e12bb2 | 242 | "Usage: %s [OPTIONS]... [-c STRING]... [file]\n" |
84844a20 | 243 | "QEMU Disk exerciser\n" |
e3aff4f6 | 244 | "\n" |
9ba371b6 DB |
245 | " --object OBJECTDEF define an object such as 'secret' for\n" |
246 | " passwords and/or encryption keys\n" | |
e4e12bb2 | 247 | " --image-opts treat file as option string\n" |
d208cc35 MK |
248 | " -c, --cmd STRING execute command with its arguments\n" |
249 | " from the given string\n" | |
be6273da | 250 | " -f, --format FMT specifies the block driver to use\n" |
e3aff4f6 AL |
251 | " -r, --read-only export read-only\n" |
252 | " -s, --snapshot use snapshot file\n" | |
e4e12bb2 | 253 | " -n, --nocache disable host cache, short for -t none\n" |
e3aff4f6 | 254 | " -m, --misalign misalign allocations for O_DIRECT\n" |
5c6c3a6c | 255 | " -k, --native-aio use kernel AIO implementation (on Linux only)\n" |
592fa070 | 256 | " -t, --cache=MODE use the given cache mode for the image\n" |
e4e12bb2 | 257 | " -d, --discard=MODE use the given discard mode for the image\n" |
e9a80859 DL |
258 | " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
259 | " specify tracing options\n" | |
260 | " see qemu-img(1) man page for full description\n" | |
459571f7 | 261 | " -U, --force-share force shared permissions\n" |
e3aff4f6 AL |
262 | " -h, --help display this help and exit\n" |
263 | " -V, --version output version information and exit\n" | |
d208cc35 | 264 | "\n" |
f5048cb7 EB |
265 | "See '%s -c help' for information on available commands.\n" |
266 | "\n" | |
267 | QEMU_HELP_BOTTOM "\n", | |
d208cc35 | 268 | name, name); |
e3aff4f6 AL |
269 | } |
270 | ||
d1174f13 KW |
271 | static char *get_prompt(void) |
272 | { | |
273 | static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ]; | |
274 | ||
275 | if (!prompt[0]) { | |
276 | snprintf(prompt, sizeof(prompt), "%s> ", progname); | |
277 | } | |
278 | ||
279 | return prompt; | |
280 | } | |
281 | ||
d5d1507b SW |
282 | static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, |
283 | const char *fmt, ...) | |
d1174f13 | 284 | { |
0cf17e18 SH |
285 | va_list ap; |
286 | va_start(ap, fmt); | |
287 | vprintf(fmt, ap); | |
288 | va_end(ap); | |
d1174f13 | 289 | } |
0cf17e18 SH |
290 | |
291 | static void readline_flush_func(void *opaque) | |
d1174f13 | 292 | { |
0cf17e18 | 293 | fflush(stdout); |
d1174f13 KW |
294 | } |
295 | ||
0cf17e18 | 296 | static void readline_func(void *opaque, const char *str, void *readline_opaque) |
d1174f13 | 297 | { |
0cf17e18 SH |
298 | char **line = readline_opaque; |
299 | *line = g_strdup(str); | |
300 | } | |
301 | ||
4694020d SH |
302 | static void completion_match(const char *cmd, void *opaque) |
303 | { | |
304 | readline_add_completion(readline_state, cmd); | |
305 | } | |
306 | ||
0cf17e18 SH |
307 | static void readline_completion_func(void *opaque, const char *str) |
308 | { | |
4694020d SH |
309 | readline_set_completion_index(readline_state, strlen(str)); |
310 | qemuio_complete_command(str, completion_match, NULL); | |
0cf17e18 SH |
311 | } |
312 | ||
313 | static char *fetchline_readline(void) | |
314 | { | |
315 | char *line = NULL; | |
316 | ||
317 | readline_start(readline_state, get_prompt(), 0, readline_func, &line); | |
318 | while (!line) { | |
319 | int ch = getchar(); | |
320 | if (ch == EOF) { | |
321 | break; | |
d1174f13 | 322 | } |
0cf17e18 | 323 | readline_handle_byte(readline_state, ch); |
d1174f13 KW |
324 | } |
325 | return line; | |
326 | } | |
0cf17e18 SH |
327 | |
328 | #define MAXREADLINESZ 1024 | |
329 | static char *fetchline_fgets(void) | |
d1174f13 KW |
330 | { |
331 | char *p, *line = g_malloc(MAXREADLINESZ); | |
332 | ||
333 | if (!fgets(line, MAXREADLINESZ, stdin)) { | |
334 | g_free(line); | |
335 | return NULL; | |
336 | } | |
337 | ||
338 | p = line + strlen(line); | |
339 | if (p != line && p[-1] == '\n') { | |
340 | p[-1] = '\0'; | |
341 | } | |
342 | ||
343 | return line; | |
344 | } | |
0cf17e18 SH |
345 | |
346 | static char *fetchline(void) | |
347 | { | |
348 | if (readline_state) { | |
349 | return fetchline_readline(); | |
350 | } else { | |
351 | return fetchline_fgets(); | |
352 | } | |
353 | } | |
d1174f13 KW |
354 | |
355 | static void prep_fetchline(void *opaque) | |
356 | { | |
357 | int *fetchable = opaque; | |
358 | ||
359 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); | |
360 | *fetchable= 1; | |
361 | } | |
362 | ||
363 | static void command_loop(void) | |
364 | { | |
365 | int i, done = 0, fetchable = 0, prompted = 0; | |
366 | char *input; | |
367 | ||
368 | for (i = 0; !done && i < ncmdline; i++) { | |
4c7b7e9b | 369 | done = qemuio_command(qemuio_blk, cmdline[i]); |
d1174f13 KW |
370 | } |
371 | if (cmdline) { | |
372 | g_free(cmdline); | |
373 | return; | |
374 | } | |
375 | ||
376 | while (!done) { | |
377 | if (!prompted) { | |
378 | printf("%s", get_prompt()); | |
379 | fflush(stdout); | |
380 | qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable); | |
381 | prompted = 1; | |
382 | } | |
383 | ||
384 | main_loop_wait(false); | |
385 | ||
386 | if (!fetchable) { | |
387 | continue; | |
388 | } | |
389 | ||
390 | input = fetchline(); | |
391 | if (input == NULL) { | |
392 | break; | |
393 | } | |
4c7b7e9b | 394 | done = qemuio_command(qemuio_blk, input); |
d1174f13 KW |
395 | g_free(input); |
396 | ||
397 | prompted = 0; | |
398 | fetchable = 0; | |
399 | } | |
400 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); | |
401 | } | |
402 | ||
403 | static void add_user_command(char *optarg) | |
404 | { | |
5839e53b | 405 | cmdline = g_renew(char *, cmdline, ++ncmdline); |
d1174f13 KW |
406 | cmdline[ncmdline-1] = optarg; |
407 | } | |
408 | ||
0cf17e18 SH |
409 | static void reenable_tty_echo(void) |
410 | { | |
411 | qemu_set_tty_echo(STDIN_FILENO, true); | |
412 | } | |
413 | ||
9ba371b6 DB |
414 | enum { |
415 | OPTION_OBJECT = 256, | |
499afa25 | 416 | OPTION_IMAGE_OPTS = 257, |
9ba371b6 DB |
417 | }; |
418 | ||
419 | static QemuOptsList qemu_object_opts = { | |
420 | .name = "object", | |
421 | .implied_opt_name = "qom-type", | |
422 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), | |
423 | .desc = { | |
424 | { } | |
425 | }, | |
426 | }; | |
427 | ||
428 | ||
499afa25 DB |
429 | static QemuOptsList file_opts = { |
430 | .name = "file", | |
431 | .implied_opt_name = "file", | |
432 | .head = QTAILQ_HEAD_INITIALIZER(file_opts.head), | |
433 | .desc = { | |
434 | /* no elements => accept any params */ | |
435 | { /* end of list */ } | |
436 | }, | |
437 | }; | |
438 | ||
e3aff4f6 AL |
439 | int main(int argc, char **argv) |
440 | { | |
43642b38 | 441 | int readonly = 0; |
459571f7 | 442 | const char *sopt = "hVc:d:f:rsnmkt:T:U"; |
43642b38 | 443 | const struct option lopt[] = { |
a513416e DB |
444 | { "help", no_argument, NULL, 'h' }, |
445 | { "version", no_argument, NULL, 'V' }, | |
a513416e DB |
446 | { "cmd", required_argument, NULL, 'c' }, |
447 | { "format", required_argument, NULL, 'f' }, | |
448 | { "read-only", no_argument, NULL, 'r' }, | |
449 | { "snapshot", no_argument, NULL, 's' }, | |
450 | { "nocache", no_argument, NULL, 'n' }, | |
451 | { "misalign", no_argument, NULL, 'm' }, | |
452 | { "native-aio", no_argument, NULL, 'k' }, | |
453 | { "discard", required_argument, NULL, 'd' }, | |
454 | { "cache", required_argument, NULL, 't' }, | |
455 | { "trace", required_argument, NULL, 'T' }, | |
456 | { "object", required_argument, NULL, OPTION_OBJECT }, | |
457 | { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS }, | |
459571f7 | 458 | { "force-share", no_argument, 0, 'U'}, |
43642b38 DN |
459 | { NULL, 0, NULL, 0 } |
460 | }; | |
461 | int c; | |
462 | int opt_index = 0; | |
9e8f1835 | 463 | int flags = BDRV_O_UNMAP; |
e151fc16 | 464 | bool writethrough = true; |
2f78e491 | 465 | Error *local_error = NULL; |
1b58b438 | 466 | QDict *opts = NULL; |
499afa25 | 467 | const char *format = NULL; |
e9a80859 | 468 | char *trace_file = NULL; |
459571f7 | 469 | bool force_share = false; |
43642b38 | 470 | |
526eda14 MK |
471 | #ifdef CONFIG_POSIX |
472 | signal(SIGPIPE, SIG_IGN); | |
473 | #endif | |
474 | ||
fe4db84d | 475 | module_call_init(MODULE_INIT_TRACE); |
43642b38 | 476 | progname = basename(argv[0]); |
10f5bff6 | 477 | qemu_init_exec_dir(argv[0]); |
43642b38 | 478 | |
e8f2d272 | 479 | qcrypto_init(&error_fatal); |
c2297088 | 480 | |
064097d9 | 481 | module_call_init(MODULE_INIT_QOM); |
9ba371b6 | 482 | qemu_add_opts(&qemu_object_opts); |
e9a80859 | 483 | qemu_add_opts(&qemu_trace_opts); |
be6273da KW |
484 | bdrv_init(); |
485 | ||
43642b38 DN |
486 | while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { |
487 | switch (c) { | |
488 | case 's': | |
489 | flags |= BDRV_O_SNAPSHOT; | |
490 | break; | |
491 | case 'n': | |
e151fc16 KW |
492 | flags |= BDRV_O_NOCACHE; |
493 | writethrough = false; | |
43642b38 | 494 | break; |
9e8f1835 PB |
495 | case 'd': |
496 | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { | |
497 | error_report("Invalid discard option: %s", optarg); | |
498 | exit(1); | |
499 | } | |
500 | break; | |
be6273da | 501 | case 'f': |
499afa25 | 502 | format = optarg; |
be6273da | 503 | break; |
43642b38 DN |
504 | case 'c': |
505 | add_user_command(optarg); | |
506 | break; | |
507 | case 'r': | |
508 | readonly = 1; | |
509 | break; | |
510 | case 'm': | |
f9883880 | 511 | qemuio_misalign = true; |
43642b38 | 512 | break; |
43642b38 DN |
513 | case 'k': |
514 | flags |= BDRV_O_NATIVE_AIO; | |
515 | break; | |
592fa070 | 516 | case 't': |
e151fc16 | 517 | if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) { |
592fa070 KW |
518 | error_report("Invalid cache option: %s", optarg); |
519 | exit(1); | |
520 | } | |
521 | break; | |
d7bb72c8 | 522 | case 'T': |
e9a80859 DL |
523 | g_free(trace_file); |
524 | trace_file = trace_opt_parse(optarg); | |
d7bb72c8 | 525 | break; |
43642b38 | 526 | case 'V': |
4face32a EB |
527 | printf("%s version " QEMU_VERSION QEMU_PKGVERSION "\n" |
528 | QEMU_COPYRIGHT "\n", progname); | |
43642b38 DN |
529 | exit(0); |
530 | case 'h': | |
531 | usage(progname); | |
532 | exit(0); | |
459571f7 FZ |
533 | case 'U': |
534 | force_share = true; | |
535 | break; | |
9ba371b6 | 536 | case OPTION_OBJECT: { |
499afa25 | 537 | QemuOpts *qopts; |
9ba371b6 DB |
538 | qopts = qemu_opts_parse_noisily(&qemu_object_opts, |
539 | optarg, true); | |
540 | if (!qopts) { | |
541 | exit(1); | |
542 | } | |
543 | } break; | |
499afa25 DB |
544 | case OPTION_IMAGE_OPTS: |
545 | imageOpts = true; | |
546 | break; | |
43642b38 DN |
547 | default: |
548 | usage(progname); | |
549 | exit(1); | |
f5edb014 | 550 | } |
43642b38 DN |
551 | } |
552 | ||
553 | if ((argc - optind) > 1) { | |
554 | usage(progname); | |
555 | exit(1); | |
556 | } | |
e3aff4f6 | 557 | |
499afa25 DB |
558 | if (format && imageOpts) { |
559 | error_report("--image-opts and -f are mutually exclusive"); | |
560 | exit(1); | |
561 | } | |
562 | ||
2f78e491 | 563 | if (qemu_init_main_loop(&local_error)) { |
565f65d2 | 564 | error_report_err(local_error); |
2f78e491 CN |
565 | exit(1); |
566 | } | |
a57d1143 | 567 | |
9ba371b6 DB |
568 | if (qemu_opts_foreach(&qemu_object_opts, |
569 | user_creatable_add_opts_foreach, | |
51b9b478 | 570 | NULL, NULL)) { |
9ba371b6 DB |
571 | exit(1); |
572 | } | |
573 | ||
e9a80859 DL |
574 | if (!trace_init_backends()) { |
575 | exit(1); | |
576 | } | |
577 | trace_init_file(trace_file); | |
578 | qemu_set_log(LOG_TRACE); | |
579 | ||
43642b38 | 580 | /* initialize commands */ |
c2cdf5c5 KW |
581 | qemuio_add_command(&quit_cmd); |
582 | qemuio_add_command(&open_cmd); | |
583 | qemuio_add_command(&close_cmd); | |
43642b38 | 584 | |
0cf17e18 SH |
585 | if (isatty(STDIN_FILENO)) { |
586 | readline_state = readline_init(readline_printf_func, | |
587 | readline_flush_func, | |
588 | NULL, | |
589 | readline_completion_func); | |
590 | qemu_set_tty_echo(STDIN_FILENO, false); | |
591 | atexit(reenable_tty_echo); | |
592 | } | |
593 | ||
43642b38 DN |
594 | /* open the device */ |
595 | if (!readonly) { | |
596 | flags |= BDRV_O_RDWR; | |
597 | } | |
598 | ||
599 | if ((argc - optind) == 1) { | |
499afa25 DB |
600 | if (imageOpts) { |
601 | QemuOpts *qopts = NULL; | |
602 | qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false); | |
603 | if (!qopts) { | |
604 | exit(1); | |
605 | } | |
606 | opts = qemu_opts_to_qdict(qopts, NULL); | |
459571f7 | 607 | if (openfile(NULL, flags, writethrough, force_share, opts)) { |
b7aa1315 NS |
608 | exit(1); |
609 | } | |
499afa25 DB |
610 | } else { |
611 | if (format) { | |
612 | opts = qdict_new(); | |
46f5ac20 | 613 | qdict_put_str(opts, "driver", format); |
499afa25 | 614 | } |
459571f7 FZ |
615 | if (openfile(argv[optind], flags, writethrough, |
616 | force_share, opts)) { | |
b7aa1315 NS |
617 | exit(1); |
618 | } | |
499afa25 | 619 | } |
43642b38 DN |
620 | } |
621 | command_loop(); | |
e3aff4f6 | 622 | |
43642b38 | 623 | /* |
922453bc | 624 | * Make sure all outstanding requests complete before the program exits. |
43642b38 | 625 | */ |
922453bc | 626 | bdrv_drain_all(); |
95533d5f | 627 | |
26f54e9a | 628 | blk_unref(qemuio_blk); |
0cf17e18 | 629 | g_free(readline_state); |
43642b38 | 630 | return 0; |
e3aff4f6 | 631 | } |