qemu/qemu-io.c
<<
>>
Prefs
   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 */
  10#include <sys/time.h>
  11#include <sys/types.h>
  12#include <stdarg.h>
  13#include <stdio.h>
  14#include <getopt.h>
  15#include <libgen.h>
  16
  17#include "qemu-io.h"
  18#include "qemu/main-loop.h"
  19#include "qemu/option.h"
  20#include "qemu/config-file.h"
  21#include "qemu/readline.h"
  22#include "sysemu/block-backend.h"
  23#include "block/block_int.h"
  24#include "trace/control.h"
  25
  26#define CMD_NOFILE_OK   0x01
  27
  28static char *progname;
  29
  30static BlockBackend *qemuio_blk;
  31
  32/* qemu-io commands passed using -c */
  33static int ncmdline;
  34static char **cmdline;
  35
  36static ReadLineState *readline_state;
  37
  38static int close_f(BlockBackend *blk, int argc, char **argv)
  39{
  40    blk_unref(qemuio_blk);
  41    qemuio_blk = NULL;
  42    return 0;
  43}
  44
  45static const cmdinfo_t close_cmd = {
  46    .name       = "close",
  47    .altname    = "c",
  48    .cfunc      = close_f,
  49    .oneline    = "close the current open file",
  50};
  51
  52static int openfile(char *name, int flags, QDict *opts)
  53{
  54    Error *local_err = NULL;
  55
  56    if (qemuio_blk) {
  57        fprintf(stderr, "file open already, try 'help close'\n");
  58        QDECREF(opts);
  59        return 1;
  60    }
  61
  62    qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
  63    if (!qemuio_blk) {
  64        fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
  65                name ? " device " : "", name ?: "",
  66                error_get_pretty(local_err));
  67        error_free(local_err);
  68        return 1;
  69    }
  70
  71    return 0;
  72}
  73
  74static void open_help(void)
  75{
  76    printf(
  77"\n"
  78" opens a new file in the requested mode\n"
  79"\n"
  80" Example:\n"
  81" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
  82"\n"
  83" Opens a file for subsequent use by all of the other qemu-io commands.\n"
  84" -r, -- open file read-only\n"
  85" -s, -- use snapshot file\n"
  86" -n, -- disable host cache\n"
  87" -o, -- options to be given to the block driver"
  88"\n");
  89}
  90
  91static int open_f(BlockBackend *blk, int argc, char **argv);
  92
  93static const cmdinfo_t open_cmd = {
  94    .name       = "open",
  95    .altname    = "o",
  96    .cfunc      = open_f,
  97    .argmin     = 1,
  98    .argmax     = -1,
  99    .flags      = CMD_NOFILE_OK,
 100    .args       = "[-Crsn] [-o options] [path]",
 101    .oneline    = "open the file specified by path",
 102    .help       = open_help,
 103};
 104
 105static QemuOptsList empty_opts = {
 106    .name = "drive",
 107    .merge_lists = true,
 108    .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
 109    .desc = {
 110        /* no elements => accept any params */
 111        { /* end of list */ }
 112    },
 113};
 114
 115static int open_f(BlockBackend *blk, int argc, char **argv)
 116{
 117    int flags = 0;
 118    int readonly = 0;
 119    int c;
 120    QemuOpts *qopts;
 121    QDict *opts;
 122
 123    while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
 124        switch (c) {
 125        case 's':
 126            flags |= BDRV_O_SNAPSHOT;
 127            break;
 128        case 'n':
 129            flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
 130            break;
 131        case 'r':
 132            readonly = 1;
 133            break;
 134        case 'o':
 135            if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
 136                printf("could not parse option list -- %s\n", optarg);
 137                qemu_opts_reset(&empty_opts);
 138                return 0;
 139            }
 140            break;
 141        default:
 142            qemu_opts_reset(&empty_opts);
 143            return qemuio_command_usage(&open_cmd);
 144        }
 145    }
 146
 147    if (!readonly) {
 148        flags |= BDRV_O_RDWR;
 149    }
 150
 151    qopts = qemu_opts_find(&empty_opts, NULL);
 152    opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
 153    qemu_opts_reset(&empty_opts);
 154
 155    if (optind == argc - 1) {
 156        return openfile(argv[optind], flags, opts);
 157    } else if (optind == argc) {
 158        return openfile(NULL, flags, opts);
 159    } else {
 160        QDECREF(opts);
 161        return qemuio_command_usage(&open_cmd);
 162    }
 163}
 164
 165static int quit_f(BlockBackend *blk, int argc, char **argv)
 166{
 167    return 1;
 168}
 169
 170static const cmdinfo_t quit_cmd = {
 171    .name       = "quit",
 172    .altname    = "q",
 173    .cfunc      = quit_f,
 174    .argmin     = -1,
 175    .argmax     = -1,
 176    .flags      = CMD_FLAG_GLOBAL,
 177    .oneline    = "exit the program",
 178};
 179
 180static void usage(const char *name)
 181{
 182    printf(
 183"Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
 184"QEMU Disk exerciser\n"
 185"\n"
 186"  -c, --cmd STRING     execute command with its arguments\n"
 187"                       from the given string\n"
 188"  -f, --format FMT     specifies the block driver to use\n"
 189"  -r, --read-only      export read-only\n"
 190"  -s, --snapshot       use snapshot file\n"
 191"  -n, --nocache        disable host cache\n"
 192"  -m, --misalign       misalign allocations for O_DIRECT\n"
 193"  -k, --native-aio     use kernel AIO implementation (on Linux only)\n"
 194"  -t, --cache=MODE     use the given cache mode for the image\n"
 195"  -T, --trace FILE     enable trace events listed in the given file\n"
 196"  -h, --help           display this help and exit\n"
 197"  -V, --version        output version information and exit\n"
 198"\n"
 199"See '%s -c help' for information on available commands."
 200"\n",
 201    name, name);
 202}
 203
 204static char *get_prompt(void)
 205{
 206    static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
 207
 208    if (!prompt[0]) {
 209        snprintf(prompt, sizeof(prompt), "%s> ", progname);
 210    }
 211
 212    return prompt;
 213}
 214
 215static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
 216                                                    const char *fmt, ...)
 217{
 218    va_list ap;
 219    va_start(ap, fmt);
 220    vprintf(fmt, ap);
 221    va_end(ap);
 222}
 223
 224static void readline_flush_func(void *opaque)
 225{
 226    fflush(stdout);
 227}
 228
 229static void readline_func(void *opaque, const char *str, void *readline_opaque)
 230{
 231    char **line = readline_opaque;
 232    *line = g_strdup(str);
 233}
 234
 235static void completion_match(const char *cmd, void *opaque)
 236{
 237    readline_add_completion(readline_state, cmd);
 238}
 239
 240static void readline_completion_func(void *opaque, const char *str)
 241{
 242    readline_set_completion_index(readline_state, strlen(str));
 243    qemuio_complete_command(str, completion_match, NULL);
 244}
 245
 246static char *fetchline_readline(void)
 247{
 248    char *line = NULL;
 249
 250    readline_start(readline_state, get_prompt(), 0, readline_func, &line);
 251    while (!line) {
 252        int ch = getchar();
 253        if (ch == EOF) {
 254            break;
 255        }
 256        readline_handle_byte(readline_state, ch);
 257    }
 258    return line;
 259}
 260
 261#define MAXREADLINESZ 1024
 262static char *fetchline_fgets(void)
 263{
 264    char *p, *line = g_malloc(MAXREADLINESZ);
 265
 266    if (!fgets(line, MAXREADLINESZ, stdin)) {
 267        g_free(line);
 268        return NULL;
 269    }
 270
 271    p = line + strlen(line);
 272    if (p != line && p[-1] == '\n') {
 273        p[-1] = '\0';
 274    }
 275
 276    return line;
 277}
 278
 279static char *fetchline(void)
 280{
 281    if (readline_state) {
 282        return fetchline_readline();
 283    } else {
 284        return fetchline_fgets();
 285    }
 286}
 287
 288static void prep_fetchline(void *opaque)
 289{
 290    int *fetchable = opaque;
 291
 292    qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
 293    *fetchable= 1;
 294}
 295
 296static void command_loop(void)
 297{
 298    int i, done = 0, fetchable = 0, prompted = 0;
 299    char *input;
 300
 301    for (i = 0; !done && i < ncmdline; i++) {
 302        done = qemuio_command(qemuio_blk, cmdline[i]);
 303    }
 304    if (cmdline) {
 305        g_free(cmdline);
 306        return;
 307    }
 308
 309    while (!done) {
 310        if (!prompted) {
 311            printf("%s", get_prompt());
 312            fflush(stdout);
 313            qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
 314            prompted = 1;
 315        }
 316
 317        main_loop_wait(false);
 318
 319        if (!fetchable) {
 320            continue;
 321        }
 322
 323        input = fetchline();
 324        if (input == NULL) {
 325            break;
 326        }
 327        done = qemuio_command(qemuio_blk, input);
 328        g_free(input);
 329
 330        prompted = 0;
 331        fetchable = 0;
 332    }
 333    qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
 334}
 335
 336static void add_user_command(char *optarg)
 337{
 338    cmdline = g_renew(char *, cmdline, ++ncmdline);
 339    cmdline[ncmdline-1] = optarg;
 340}
 341
 342static void reenable_tty_echo(void)
 343{
 344    qemu_set_tty_echo(STDIN_FILENO, true);
 345}
 346
 347int main(int argc, char **argv)
 348{
 349    int readonly = 0;
 350    const char *sopt = "hVc:d:f:rsnmgkt:T:";
 351    const struct option lopt[] = {
 352        { "help", 0, NULL, 'h' },
 353        { "version", 0, NULL, 'V' },
 354        { "offset", 1, NULL, 'o' },
 355        { "cmd", 1, NULL, 'c' },
 356        { "format", 1, NULL, 'f' },
 357        { "read-only", 0, NULL, 'r' },
 358        { "snapshot", 0, NULL, 's' },
 359        { "nocache", 0, NULL, 'n' },
 360        { "misalign", 0, NULL, 'm' },
 361        { "native-aio", 0, NULL, 'k' },
 362        { "discard", 1, NULL, 'd' },
 363        { "cache", 1, NULL, 't' },
 364        { "trace", 1, NULL, 'T' },
 365        { NULL, 0, NULL, 0 }
 366    };
 367    int c;
 368    int opt_index = 0;
 369    int flags = BDRV_O_UNMAP;
 370    Error *local_error = NULL;
 371    QDict *opts = NULL;
 372
 373#ifdef CONFIG_POSIX
 374    signal(SIGPIPE, SIG_IGN);
 375#endif
 376
 377    progname = basename(argv[0]);
 378    qemu_init_exec_dir(argv[0]);
 379
 380    bdrv_init();
 381
 382    while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
 383        switch (c) {
 384        case 's':
 385            flags |= BDRV_O_SNAPSHOT;
 386            break;
 387        case 'n':
 388            flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
 389            break;
 390        case 'd':
 391            if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
 392                error_report("Invalid discard option: %s", optarg);
 393                exit(1);
 394            }
 395            break;
 396        case 'f':
 397            if (!opts) {
 398                opts = qdict_new();
 399            }
 400            qdict_put(opts, "driver", qstring_from_str(optarg));
 401            break;
 402        case 'c':
 403            add_user_command(optarg);
 404            break;
 405        case 'r':
 406            readonly = 1;
 407            break;
 408        case 'm':
 409            qemuio_misalign = true;
 410            break;
 411        case 'k':
 412            flags |= BDRV_O_NATIVE_AIO;
 413            break;
 414        case 't':
 415            if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
 416                error_report("Invalid cache option: %s", optarg);
 417                exit(1);
 418            }
 419            break;
 420        case 'T':
 421            if (!trace_init_backends(optarg, NULL)) {
 422                exit(1); /* error message will have been printed */
 423            }
 424            break;
 425        case 'V':
 426            printf("%s version %s\n", progname, QEMU_VERSION);
 427            exit(0);
 428        case 'h':
 429            usage(progname);
 430            exit(0);
 431        default:
 432            usage(progname);
 433            exit(1);
 434        }
 435    }
 436
 437    if ((argc - optind) > 1) {
 438        usage(progname);
 439        exit(1);
 440    }
 441
 442    if (qemu_init_main_loop(&local_error)) {
 443        error_report_err(local_error);
 444        exit(1);
 445    }
 446
 447    /* initialize commands */
 448    qemuio_add_command(&quit_cmd);
 449    qemuio_add_command(&open_cmd);
 450    qemuio_add_command(&close_cmd);
 451
 452    if (isatty(STDIN_FILENO)) {
 453        readline_state = readline_init(readline_printf_func,
 454                                       readline_flush_func,
 455                                       NULL,
 456                                       readline_completion_func);
 457        qemu_set_tty_echo(STDIN_FILENO, false);
 458        atexit(reenable_tty_echo);
 459    }
 460
 461    /* open the device */
 462    if (!readonly) {
 463        flags |= BDRV_O_RDWR;
 464    }
 465
 466    if ((argc - optind) == 1) {
 467        openfile(argv[optind], flags, opts);
 468    }
 469    command_loop();
 470
 471    /*
 472     * Make sure all outstanding requests complete before the program exits.
 473     */
 474    bdrv_drain_all();
 475
 476    blk_unref(qemuio_blk);
 477    g_free(readline_state);
 478    return 0;
 479}
 480