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