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