blob: 427cbaef57fc70a27088806105ba1088650c53a7 [file] [log] [blame]
aliguorie3aff4f2009-04-05 19:14:04 +00001/*
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 */
Peter Maydell80c71a22016-01-18 18:01:42 +000010#include "qemu/osdep.h"
aliguorie3aff4f2009-04-05 19:14:04 +000011#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020012#include <libgen.h>
aliguorie3aff4f2009-04-05 19:14:04 +000013
Markus Armbrusterda34e652016-03-14 09:01:28 +010014#include "qapi/error.h"
Kevin Wolf3d219942013-06-05 14:19:39 +020015#include "qemu-io.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010016#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010017#include "qemu/main-loop.h"
Max Reitzb543c5c2013-10-11 14:02:10 +020018#include "qemu/option.h"
19#include "qemu/config-file.h"
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010020#include "qemu/readline.h"
Denis V. Luneve9a80852016-06-17 17:44:11 +030021#include "qemu/log.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010022#include "qapi/qmp/qstring.h"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +000023#include "qom/object_interfaces.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020024#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010025#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000026#include "trace/control.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010027#include "crypto/init.h"
aliguorie3aff4f2009-04-05 19:14:04 +000028
Devin Nakamura43642b32011-07-11 11:22:16 -040029#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000030
Stefan Weilf9883882014-03-05 22:23:00 +010031static char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000032
Markus Armbruster26f54e92014-10-07 13:59:04 +020033static BlockBackend *qemuio_blk;
Kevin Wolf191c2892010-09-16 13:18:08 +020034
Kevin Wolfd1174f12013-06-05 14:19:37 +020035/* qemu-io commands passed using -c */
36static int ncmdline;
37static char **cmdline;
Daniel P. Berrange499afa22016-02-17 10:10:18 +000038static bool imageOpts;
Kevin Wolfd1174f12013-06-05 14:19:37 +020039
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010040static ReadLineState *readline_state;
41
Max Reitz4c7b7e92015-02-05 13:58:22 -050042static int close_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000043{
Markus Armbruster26f54e92014-10-07 13:59:04 +020044 blk_unref(qemuio_blk);
Markus Armbruster26f54e92014-10-07 13:59:04 +020045 qemuio_blk = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040046 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000047}
48
49static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040050 .name = "close",
51 .altname = "c",
52 .cfunc = close_f,
53 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000054};
55
Kevin Wolfe151fc12016-03-15 12:52:30 +010056static int openfile(char *name, int flags, bool writethrough, QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000057{
Max Reitz34b5d2c2013-09-05 14:45:29 +020058 Error *local_err = NULL;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010059 BlockDriverState *bs;
Max Reitz34b5d2c2013-09-05 14:45:29 +020060
Max Reitz1b58b432015-02-05 13:58:20 -050061 if (qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010062 error_report("file open already, try 'help close'");
Markus Armbruster29f26012014-05-28 11:16:59 +020063 QDECREF(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040064 return 1;
65 }
aliguorie3aff4f2009-04-05 19:14:04 +000066
Max Reitzefaa7c42016-03-16 19:54:38 +010067 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
Max Reitz1b58b432015-02-05 13:58:20 -050068 if (!qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010069 error_reportf_err(local_err, "can't open%s%s: ",
70 name ? " device " : "", name ?: "");
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020071 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -040072 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020073
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010074 bs = blk_bs(qemuio_blk);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +000075 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs)) {
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010076 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
Kevin Wolfe151fc12016-03-15 12:52:30 +010088 blk_set_enable_write_cache(qemuio_blk, !writethrough);
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010089
Devin Nakamura43642b32011-07-11 11:22:16 -040090 return 0;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010091
92 error:
93 blk_unref(qemuio_blk);
94 qemuio_blk = NULL;
95 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +000096}
97
Devin Nakamura43642b32011-07-11 11:22:16 -040098static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000099{
Devin Nakamura43642b32011-07-11 11:22:16 -0400100 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000101"\n"
102" opens a new file in the requested mode\n"
103"\n"
104" Example:\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600105" 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000106"\n"
107" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000108" -r, -- open file read-only\n"
109" -s, -- use snapshot file\n"
Eric Blakeb8d970f2016-05-07 21:16:41 -0600110" -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"
Max Reitzb543c5c2013-10-11 14:02:10 +0200114" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000115"\n");
116}
117
Max Reitz4c7b7e92015-02-05 13:58:22 -0500118static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000119
120static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400121 .name = "open",
122 .altname = "o",
123 .cfunc = open_f,
124 .argmin = 1,
125 .argmax = -1,
126 .flags = CMD_NOFILE_OK,
Eric Blakeb8d970f2016-05-07 21:16:41 -0600127 .args = "[-rsnk] [-t cache] [-d discard] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400128 .oneline = "open the file specified by path",
129 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000130};
aliguorie3aff4f2009-04-05 19:14:04 +0000131
Max Reitzb543c5c2013-10-11 14:02:10 +0200132static QemuOptsList empty_opts = {
133 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200134 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200135 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
136 .desc = {
137 /* no elements => accept any params */
138 { /* end of list */ }
139 },
140};
141
Max Reitz4c7b7e92015-02-05 13:58:22 -0500142static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000143{
Eric Blakeb8d970f2016-05-07 21:16:41 -0600144 int flags = BDRV_O_UNMAP;
Devin Nakamura43642b32011-07-11 11:22:16 -0400145 int readonly = 0;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100146 bool writethrough = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400147 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200148 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200149 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000150
Eric Blakeb8d970f2016-05-07 21:16:41 -0600151 while ((c = getopt(argc, argv, "snro:kt:d:")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400152 switch (c) {
153 case 's':
154 flags |= BDRV_O_SNAPSHOT;
155 break;
156 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100157 flags |= BDRV_O_NOCACHE;
158 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400159 break;
160 case 'r':
161 readonly = 1;
162 break;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600163 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;
Max Reitzb543c5c2013-10-11 14:02:10 +0200180 case 'o':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000181 if (imageOpts) {
182 printf("--image-opts and 'open -o' are mutually exclusive\n");
Eric Blakeb8d970f2016-05-07 21:16:41 -0600183 qemu_opts_reset(&empty_opts);
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000184 return 0;
185 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100186 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200187 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200188 return 0;
189 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200190 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400191 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200192 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200193 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200194 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400195 }
aliguorie3aff4f2009-04-05 19:14:04 +0000196
Devin Nakamura43642b32011-07-11 11:22:16 -0400197 if (!readonly) {
198 flags |= BDRV_O_RDWR;
199 }
aliguorie3aff4f2009-04-05 19:14:04 +0000200
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000201 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
Markus Armbruster443422f2014-05-28 11:16:58 +0200209 qopts = qemu_opts_find(&empty_opts, NULL);
210 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
211 qemu_opts_reset(&empty_opts);
212
Max Reitzfd0fee32013-12-20 19:28:20 +0100213 if (optind == argc - 1) {
Kevin Wolfe151fc12016-03-15 12:52:30 +0100214 return openfile(argv[optind], flags, writethrough, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100215 } else if (optind == argc) {
Kevin Wolfe151fc12016-03-15 12:52:30 +0100216 return openfile(NULL, flags, writethrough, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100217 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200218 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200219 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400220 }
aliguorie3aff4f2009-04-05 19:14:04 +0000221}
222
Max Reitz4c7b7e92015-02-05 13:58:22 -0500223static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200224{
225 return 1;
226}
227
228static 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
aliguorie3aff4f2009-04-05 19:14:04 +0000238static void usage(const char *name)
239{
Devin Nakamura43642b32011-07-11 11:22:16 -0400240 printf(
Eric Blakee4e12bb2016-05-07 21:16:40 -0600241"Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200242"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000243"\n"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000244" --object OBJECTDEF define an object such as 'secret' for\n"
245" passwords and/or encryption keys\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600246" --image-opts treat file as option string\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400247" -c, --cmd STRING execute command with its arguments\n"
248" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100249" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000250" -r, --read-only export read-only\n"
251" -s, --snapshot use snapshot file\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600252" -n, --nocache disable host cache, short for -t none\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000253" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200254" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200255" -t, --cache=MODE use the given cache mode for the image\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600256" -d, --discard=MODE use the given discard mode for the image\n"
Denis V. Luneve9a80852016-06-17 17:44:11 +0300257" -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"
aliguorie3aff4f2009-04-05 19:14:04 +0000260" -h, --help display this help and exit\n"
261" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400262"\n"
263"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000264"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400265 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000266}
267
Kevin Wolfd1174f12013-06-05 14:19:37 +0200268static 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
Stefan Weild5d15072014-01-25 18:18:23 +0100279static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
280 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200281{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100282 va_list ap;
283 va_start(ap, fmt);
284 vprintf(fmt, ap);
285 va_end(ap);
286}
287
288static void readline_flush_func(void *opaque)
289{
290 fflush(stdout);
291}
292
293static void readline_func(void *opaque, const char *str, void *readline_opaque)
294{
295 char **line = readline_opaque;
296 *line = g_strdup(str);
297}
298
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100299static void completion_match(const char *cmd, void *opaque)
300{
301 readline_add_completion(readline_state, cmd);
302}
303
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100304static void readline_completion_func(void *opaque, const char *str)
305{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100306 readline_set_completion_index(readline_state, strlen(str));
307 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100308}
309
310static 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;
319 }
320 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200321 }
322 return line;
323}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200324
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100325#define MAXREADLINESZ 1024
326static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200327{
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}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100342
343static char *fetchline(void)
344{
345 if (readline_state) {
346 return fetchline_readline();
347 } else {
348 return fetchline_fgets();
349 }
350}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200351
352static 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
360static 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++) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500366 done = qemuio_command(qemuio_blk, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200367 }
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 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500391 done = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200392 g_free(input);
393
394 prompted = 0;
395 fetchable = 0;
396 }
397 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
398}
399
400static void add_user_command(char *optarg)
401{
Markus Armbruster5839e532014-08-19 10:31:08 +0200402 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200403 cmdline[ncmdline-1] = optarg;
404}
405
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100406static void reenable_tty_echo(void)
407{
408 qemu_set_tty_echo(STDIN_FILENO, true);
409}
410
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000411enum {
412 OPTION_OBJECT = 256,
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000413 OPTION_IMAGE_OPTS = 257,
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000414};
415
416static 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
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000426static 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
aliguorie3aff4f2009-04-05 19:14:04 +0000436int main(int argc, char **argv)
437{
Devin Nakamura43642b32011-07-11 11:22:16 -0400438 int readonly = 0;
Eric Blakee4e12bb2016-05-07 21:16:40 -0600439 const char *sopt = "hVc:d:f:rsnmkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400440 const struct option lopt[] = {
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000441 { "help", no_argument, NULL, 'h' },
442 { "version", no_argument, NULL, 'V' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000443 { "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 },
Devin Nakamura43642b32011-07-11 11:22:16 -0400455 { NULL, 0, NULL, 0 }
456 };
457 int c;
458 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100459 int flags = BDRV_O_UNMAP;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100460 bool writethrough = true;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300461 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500462 QDict *opts = NULL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000463 const char *format = NULL;
Denis V. Luneve9a80852016-06-17 17:44:11 +0300464 char *trace_file = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000465
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900466#ifdef CONFIG_POSIX
467 signal(SIGPIPE, SIG_IGN);
468#endif
469
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100470 module_call_init(MODULE_INIT_TRACE);
Devin Nakamura43642b32011-07-11 11:22:16 -0400471 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800472 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000473
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300474 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100475
Daniel P. Berrange064097d2016-02-10 18:41:01 +0000476 module_call_init(MODULE_INIT_QOM);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000477 qemu_add_opts(&qemu_object_opts);
Denis V. Luneve9a80852016-06-17 17:44:11 +0300478 qemu_add_opts(&qemu_trace_opts);
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100479 bdrv_init();
480
Devin Nakamura43642b32011-07-11 11:22:16 -0400481 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':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100487 flags |= BDRV_O_NOCACHE;
488 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400489 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100490 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;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100496 case 'f':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000497 format = optarg;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100498 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400499 case 'c':
500 add_user_command(optarg);
501 break;
502 case 'r':
503 readonly = 1;
504 break;
505 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100506 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400507 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400508 case 'k':
509 flags |= BDRV_O_NATIVE_AIO;
510 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200511 case 't':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100512 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf592fa072012-04-18 12:07:39 +0200513 error_report("Invalid cache option: %s", optarg);
514 exit(1);
515 }
516 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000517 case 'T':
Denis V. Luneve9a80852016-06-17 17:44:11 +0300518 g_free(trace_file);
519 trace_file = trace_opt_parse(optarg);
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000520 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400521 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200522 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400523 exit(0);
524 case 'h':
525 usage(progname);
526 exit(0);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000527 case OPTION_OBJECT: {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000528 QemuOpts *qopts;
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000529 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
530 optarg, true);
531 if (!qopts) {
532 exit(1);
533 }
534 } break;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000535 case OPTION_IMAGE_OPTS:
536 imageOpts = true;
537 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400538 default:
539 usage(progname);
540 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200541 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400542 }
aliguorie3aff4f2009-04-05 19:14:04 +0000543
Devin Nakamura43642b32011-07-11 11:22:16 -0400544 if ((argc - optind) > 1) {
545 usage(progname);
546 exit(1);
547 }
aliguorie3aff4f2009-04-05 19:14:04 +0000548
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000549 if (format && imageOpts) {
550 error_report("--image-opts and -f are mutually exclusive");
551 exit(1);
552 }
553
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300554 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100555 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300556 exit(1);
557 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800558
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000559 if (qemu_opts_foreach(&qemu_object_opts,
560 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200561 NULL, NULL)) {
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000562 exit(1);
563 }
564
Denis V. Luneve9a80852016-06-17 17:44:11 +0300565 if (!trace_init_backends()) {
566 exit(1);
567 }
568 trace_init_file(trace_file);
569 qemu_set_log(LOG_TRACE);
570
Devin Nakamura43642b32011-07-11 11:22:16 -0400571 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200572 qemuio_add_command(&quit_cmd);
573 qemuio_add_command(&open_cmd);
574 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400575
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100576 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
Devin Nakamura43642b32011-07-11 11:22:16 -0400585 /* open the device */
586 if (!readonly) {
587 flags |= BDRV_O_RDWR;
588 }
589
590 if ((argc - optind) == 1) {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000591 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);
Nir Sofferb7aa1312017-02-01 02:31:18 +0200598 if (openfile(NULL, flags, writethrough, opts)) {
599 exit(1);
600 }
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000601 } else {
602 if (format) {
603 opts = qdict_new();
604 qdict_put(opts, "driver", qstring_from_str(format));
605 }
Nir Sofferb7aa1312017-02-01 02:31:18 +0200606 if (openfile(argv[optind], flags, writethrough, opts)) {
607 exit(1);
608 }
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000609 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400610 }
611 command_loop();
612
613 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000614 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400615 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000616 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400617
Markus Armbruster26f54e92014-10-07 13:59:04 +0200618 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100619 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400620 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000621}