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