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