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