blob: 0a1a3df87f5c7b7c500ecd4222bec99828b5bac5 [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"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010021#include "qapi/qmp/qstring.h"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +000022#include "qom/object_interfaces.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020023#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010024#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000025#include "trace/control.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010026#include "crypto/init.h"
aliguorie3aff4f2009-04-05 19:14:04 +000027
Devin Nakamura43642b32011-07-11 11:22:16 -040028#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000029
Stefan Weilf9883882014-03-05 22:23:00 +010030static char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000031
Markus Armbruster26f54e92014-10-07 13:59:04 +020032static BlockBackend *qemuio_blk;
Kevin Wolf191c2892010-09-16 13:18:08 +020033
Kevin Wolfd1174f12013-06-05 14:19:37 +020034/* qemu-io commands passed using -c */
35static int ncmdline;
36static char **cmdline;
Daniel P. Berrange499afa22016-02-17 10:10:18 +000037static bool imageOpts;
Kevin Wolfd1174f12013-06-05 14:19:37 +020038
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010039static ReadLineState *readline_state;
40
Max Reitz4c7b7e92015-02-05 13:58:22 -050041static int close_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000042{
Markus Armbruster26f54e92014-10-07 13:59:04 +020043 blk_unref(qemuio_blk);
Markus Armbruster26f54e92014-10-07 13:59:04 +020044 qemuio_blk = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040045 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000046}
47
48static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040049 .name = "close",
50 .altname = "c",
51 .cfunc = close_f,
52 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000053};
54
Kevin Wolfe151fc12016-03-15 12:52:30 +010055static int openfile(char *name, int flags, bool writethrough, QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000056{
Max Reitz34b5d2c2013-09-05 14:45:29 +020057 Error *local_err = NULL;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010058 BlockDriverState *bs;
Max Reitz34b5d2c2013-09-05 14:45:29 +020059
Max Reitz1b58b432015-02-05 13:58:20 -050060 if (qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010061 error_report("file open already, try 'help close'");
Markus Armbruster29f26012014-05-28 11:16:59 +020062 QDECREF(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040063 return 1;
64 }
aliguorie3aff4f2009-04-05 19:14:04 +000065
Max Reitzefaa7c42016-03-16 19:54:38 +010066 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
Max Reitz1b58b432015-02-05 13:58:20 -050067 if (!qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010068 error_reportf_err(local_err, "can't open%s%s: ",
69 name ? " device " : "", name ?: "");
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020070 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -040071 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020072
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010073 bs = blk_bs(qemuio_blk);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +000074 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs)) {
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010075 char password[256];
76 printf("Disk image '%s' is encrypted.\n", name);
77 if (qemu_read_password(password, sizeof(password)) < 0) {
78 error_report("No password given");
79 goto error;
80 }
81 if (bdrv_set_key(bs, password) < 0) {
82 error_report("invalid password");
83 goto error;
84 }
85 }
86
Kevin Wolfe151fc12016-03-15 12:52:30 +010087 blk_set_enable_write_cache(qemuio_blk, !writethrough);
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010088
Devin Nakamura43642b32011-07-11 11:22:16 -040089 return 0;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010090
91 error:
92 blk_unref(qemuio_blk);
93 qemuio_blk = NULL;
94 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +000095}
96
Devin Nakamura43642b32011-07-11 11:22:16 -040097static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000098{
Devin Nakamura43642b32011-07-11 11:22:16 -040099 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000100"\n"
101" opens a new file in the requested mode\n"
102"\n"
103" Example:\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600104" 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000105"\n"
106" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000107" -r, -- open file read-only\n"
108" -s, -- use snapshot file\n"
109" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200110" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000111"\n");
112}
113
Max Reitz4c7b7e92015-02-05 13:58:22 -0500114static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000115
116static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400117 .name = "open",
118 .altname = "o",
119 .cfunc = open_f,
120 .argmin = 1,
121 .argmax = -1,
122 .flags = CMD_NOFILE_OK,
Eric Blakee4e12bb2016-05-07 21:16:40 -0600123 .args = "[-rsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400124 .oneline = "open the file specified by path",
125 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000126};
aliguorie3aff4f2009-04-05 19:14:04 +0000127
Max Reitzb543c5c2013-10-11 14:02:10 +0200128static QemuOptsList empty_opts = {
129 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200130 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200131 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
132 .desc = {
133 /* no elements => accept any params */
134 { /* end of list */ }
135 },
136};
137
Max Reitz4c7b7e92015-02-05 13:58:22 -0500138static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000139{
Devin Nakamura43642b32011-07-11 11:22:16 -0400140 int flags = 0;
141 int readonly = 0;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100142 bool writethrough = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400143 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200144 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200145 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000146
Eric Blakee4e12bb2016-05-07 21:16:40 -0600147 while ((c = getopt(argc, argv, "snro:")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400148 switch (c) {
149 case 's':
150 flags |= BDRV_O_SNAPSHOT;
151 break;
152 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100153 flags |= BDRV_O_NOCACHE;
154 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400155 break;
156 case 'r':
157 readonly = 1;
158 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200159 case 'o':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000160 if (imageOpts) {
161 printf("--image-opts and 'open -o' are mutually exclusive\n");
162 return 0;
163 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100164 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200165 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200166 return 0;
167 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200168 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400169 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200170 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200171 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200172 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400173 }
aliguorie3aff4f2009-04-05 19:14:04 +0000174
Devin Nakamura43642b32011-07-11 11:22:16 -0400175 if (!readonly) {
176 flags |= BDRV_O_RDWR;
177 }
aliguorie3aff4f2009-04-05 19:14:04 +0000178
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000179 if (imageOpts && (optind == argc - 1)) {
180 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
181 qemu_opts_reset(&empty_opts);
182 return 0;
183 }
184 optind++;
185 }
186
Markus Armbruster443422f2014-05-28 11:16:58 +0200187 qopts = qemu_opts_find(&empty_opts, NULL);
188 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
189 qemu_opts_reset(&empty_opts);
190
Max Reitzfd0fee32013-12-20 19:28:20 +0100191 if (optind == argc - 1) {
Kevin Wolfe151fc12016-03-15 12:52:30 +0100192 return openfile(argv[optind], flags, writethrough, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100193 } else if (optind == argc) {
Kevin Wolfe151fc12016-03-15 12:52:30 +0100194 return openfile(NULL, flags, writethrough, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100195 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200196 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200197 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400198 }
aliguorie3aff4f2009-04-05 19:14:04 +0000199}
200
Max Reitz4c7b7e92015-02-05 13:58:22 -0500201static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200202{
203 return 1;
204}
205
206static const cmdinfo_t quit_cmd = {
207 .name = "quit",
208 .altname = "q",
209 .cfunc = quit_f,
210 .argmin = -1,
211 .argmax = -1,
212 .flags = CMD_FLAG_GLOBAL,
213 .oneline = "exit the program",
214};
215
aliguorie3aff4f2009-04-05 19:14:04 +0000216static void usage(const char *name)
217{
Devin Nakamura43642b32011-07-11 11:22:16 -0400218 printf(
Eric Blakee4e12bb2016-05-07 21:16:40 -0600219"Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200220"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000221"\n"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000222" --object OBJECTDEF define an object such as 'secret' for\n"
223" passwords and/or encryption keys\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600224" --image-opts treat file as option string\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400225" -c, --cmd STRING execute command with its arguments\n"
226" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100227" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000228" -r, --read-only export read-only\n"
229" -s, --snapshot use snapshot file\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600230" -n, --nocache disable host cache, short for -t none\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000231" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200232" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200233" -t, --cache=MODE use the given cache mode for the image\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600234" -d, --discard=MODE use the given discard mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000235" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000236" -h, --help display this help and exit\n"
237" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400238"\n"
239"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000240"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400241 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000242}
243
Kevin Wolfd1174f12013-06-05 14:19:37 +0200244static char *get_prompt(void)
245{
246 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
247
248 if (!prompt[0]) {
249 snprintf(prompt, sizeof(prompt), "%s> ", progname);
250 }
251
252 return prompt;
253}
254
Stefan Weild5d15072014-01-25 18:18:23 +0100255static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
256 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200257{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100258 va_list ap;
259 va_start(ap, fmt);
260 vprintf(fmt, ap);
261 va_end(ap);
262}
263
264static void readline_flush_func(void *opaque)
265{
266 fflush(stdout);
267}
268
269static void readline_func(void *opaque, const char *str, void *readline_opaque)
270{
271 char **line = readline_opaque;
272 *line = g_strdup(str);
273}
274
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100275static void completion_match(const char *cmd, void *opaque)
276{
277 readline_add_completion(readline_state, cmd);
278}
279
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100280static void readline_completion_func(void *opaque, const char *str)
281{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100282 readline_set_completion_index(readline_state, strlen(str));
283 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100284}
285
286static char *fetchline_readline(void)
287{
288 char *line = NULL;
289
290 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
291 while (!line) {
292 int ch = getchar();
293 if (ch == EOF) {
294 break;
295 }
296 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200297 }
298 return line;
299}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200300
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100301#define MAXREADLINESZ 1024
302static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200303{
304 char *p, *line = g_malloc(MAXREADLINESZ);
305
306 if (!fgets(line, MAXREADLINESZ, stdin)) {
307 g_free(line);
308 return NULL;
309 }
310
311 p = line + strlen(line);
312 if (p != line && p[-1] == '\n') {
313 p[-1] = '\0';
314 }
315
316 return line;
317}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100318
319static char *fetchline(void)
320{
321 if (readline_state) {
322 return fetchline_readline();
323 } else {
324 return fetchline_fgets();
325 }
326}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200327
328static void prep_fetchline(void *opaque)
329{
330 int *fetchable = opaque;
331
332 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
333 *fetchable= 1;
334}
335
336static void command_loop(void)
337{
338 int i, done = 0, fetchable = 0, prompted = 0;
339 char *input;
340
341 for (i = 0; !done && i < ncmdline; i++) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500342 done = qemuio_command(qemuio_blk, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200343 }
344 if (cmdline) {
345 g_free(cmdline);
346 return;
347 }
348
349 while (!done) {
350 if (!prompted) {
351 printf("%s", get_prompt());
352 fflush(stdout);
353 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
354 prompted = 1;
355 }
356
357 main_loop_wait(false);
358
359 if (!fetchable) {
360 continue;
361 }
362
363 input = fetchline();
364 if (input == NULL) {
365 break;
366 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500367 done = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200368 g_free(input);
369
370 prompted = 0;
371 fetchable = 0;
372 }
373 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
374}
375
376static void add_user_command(char *optarg)
377{
Markus Armbruster5839e532014-08-19 10:31:08 +0200378 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200379 cmdline[ncmdline-1] = optarg;
380}
381
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100382static void reenable_tty_echo(void)
383{
384 qemu_set_tty_echo(STDIN_FILENO, true);
385}
386
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000387enum {
388 OPTION_OBJECT = 256,
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000389 OPTION_IMAGE_OPTS = 257,
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000390};
391
392static QemuOptsList qemu_object_opts = {
393 .name = "object",
394 .implied_opt_name = "qom-type",
395 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
396 .desc = {
397 { }
398 },
399};
400
401
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000402static QemuOptsList file_opts = {
403 .name = "file",
404 .implied_opt_name = "file",
405 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
406 .desc = {
407 /* no elements => accept any params */
408 { /* end of list */ }
409 },
410};
411
aliguorie3aff4f2009-04-05 19:14:04 +0000412int main(int argc, char **argv)
413{
Devin Nakamura43642b32011-07-11 11:22:16 -0400414 int readonly = 0;
Eric Blakee4e12bb2016-05-07 21:16:40 -0600415 const char *sopt = "hVc:d:f:rsnmkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400416 const struct option lopt[] = {
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000417 { "help", no_argument, NULL, 'h' },
418 { "version", no_argument, NULL, 'V' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000419 { "cmd", required_argument, NULL, 'c' },
420 { "format", required_argument, NULL, 'f' },
421 { "read-only", no_argument, NULL, 'r' },
422 { "snapshot", no_argument, NULL, 's' },
423 { "nocache", no_argument, NULL, 'n' },
424 { "misalign", no_argument, NULL, 'm' },
425 { "native-aio", no_argument, NULL, 'k' },
426 { "discard", required_argument, NULL, 'd' },
427 { "cache", required_argument, NULL, 't' },
428 { "trace", required_argument, NULL, 'T' },
429 { "object", required_argument, NULL, OPTION_OBJECT },
430 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
Devin Nakamura43642b32011-07-11 11:22:16 -0400431 { NULL, 0, NULL, 0 }
432 };
433 int c;
434 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100435 int flags = BDRV_O_UNMAP;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100436 bool writethrough = true;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300437 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500438 QDict *opts = NULL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000439 const char *format = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000440
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900441#ifdef CONFIG_POSIX
442 signal(SIGPIPE, SIG_IGN);
443#endif
444
Devin Nakamura43642b32011-07-11 11:22:16 -0400445 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800446 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000447
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100448 if (qcrypto_init(&local_error) < 0) {
449 error_reportf_err(local_error, "cannot initialize crypto: ");
450 exit(1);
451 }
452
Daniel P. Berrange064097d2016-02-10 18:41:01 +0000453 module_call_init(MODULE_INIT_QOM);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000454 qemu_add_opts(&qemu_object_opts);
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100455 bdrv_init();
456
Devin Nakamura43642b32011-07-11 11:22:16 -0400457 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
458 switch (c) {
459 case 's':
460 flags |= BDRV_O_SNAPSHOT;
461 break;
462 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100463 flags |= BDRV_O_NOCACHE;
464 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400465 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100466 case 'd':
467 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
468 error_report("Invalid discard option: %s", optarg);
469 exit(1);
470 }
471 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100472 case 'f':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000473 format = optarg;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100474 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400475 case 'c':
476 add_user_command(optarg);
477 break;
478 case 'r':
479 readonly = 1;
480 break;
481 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100482 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400483 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400484 case 'k':
485 flags |= BDRV_O_NATIVE_AIO;
486 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200487 case 't':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100488 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf592fa072012-04-18 12:07:39 +0200489 error_report("Invalid cache option: %s", optarg);
490 exit(1);
491 }
492 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000493 case 'T':
Paolo Bonzini41fc57e2016-01-07 16:55:24 +0300494 if (!trace_init_backends()) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000495 exit(1); /* error message will have been printed */
496 }
497 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400498 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200499 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400500 exit(0);
501 case 'h':
502 usage(progname);
503 exit(0);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000504 case OPTION_OBJECT: {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000505 QemuOpts *qopts;
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000506 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
507 optarg, true);
508 if (!qopts) {
509 exit(1);
510 }
511 } break;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000512 case OPTION_IMAGE_OPTS:
513 imageOpts = true;
514 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400515 default:
516 usage(progname);
517 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200518 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400519 }
aliguorie3aff4f2009-04-05 19:14:04 +0000520
Devin Nakamura43642b32011-07-11 11:22:16 -0400521 if ((argc - optind) > 1) {
522 usage(progname);
523 exit(1);
524 }
aliguorie3aff4f2009-04-05 19:14:04 +0000525
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000526 if (format && imageOpts) {
527 error_report("--image-opts and -f are mutually exclusive");
528 exit(1);
529 }
530
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300531 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100532 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300533 exit(1);
534 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800535
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000536 if (qemu_opts_foreach(&qemu_object_opts,
537 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200538 NULL, NULL)) {
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000539 exit(1);
540 }
541
Devin Nakamura43642b32011-07-11 11:22:16 -0400542 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200543 qemuio_add_command(&quit_cmd);
544 qemuio_add_command(&open_cmd);
545 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400546
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100547 if (isatty(STDIN_FILENO)) {
548 readline_state = readline_init(readline_printf_func,
549 readline_flush_func,
550 NULL,
551 readline_completion_func);
552 qemu_set_tty_echo(STDIN_FILENO, false);
553 atexit(reenable_tty_echo);
554 }
555
Devin Nakamura43642b32011-07-11 11:22:16 -0400556 /* open the device */
557 if (!readonly) {
558 flags |= BDRV_O_RDWR;
559 }
560
561 if ((argc - optind) == 1) {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000562 if (imageOpts) {
563 QemuOpts *qopts = NULL;
564 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
565 if (!qopts) {
566 exit(1);
567 }
568 opts = qemu_opts_to_qdict(qopts, NULL);
Kevin Wolfe151fc12016-03-15 12:52:30 +0100569 openfile(NULL, flags, writethrough, opts);
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000570 } else {
571 if (format) {
572 opts = qdict_new();
573 qdict_put(opts, "driver", qstring_from_str(format));
574 }
Kevin Wolfe151fc12016-03-15 12:52:30 +0100575 openfile(argv[optind], flags, writethrough, opts);
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000576 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400577 }
578 command_loop();
579
580 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000581 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400582 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000583 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400584
Markus Armbruster26f54e92014-10-07 13:59:04 +0200585 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100586 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400587 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000588}