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