2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
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.
10 #include <sys/types.h>
15 #include "qemu-common.h"
16 #include "block_int.h"
19 #define VERSION "0.0.1"
21 #define CMD_NOFILE_OK 0x01
24 static BlockDriverState
*bs
;
29 * Memory allocation helpers.
31 * Make sure memory is aligned by default, or purposefully misaligned if
32 * that is specified on the command line.
35 #define MISALIGN_OFFSET 16
36 static void *qemu_io_alloc(size_t len
, int pattern
)
41 len
+= MISALIGN_OFFSET
;
42 buf
= qemu_memalign(512, len
);
43 memset(buf
, pattern
, len
);
45 buf
+= MISALIGN_OFFSET
;
49 static void qemu_io_free(void *p
)
57 dump_buffer(const void *buffer
, int64_t offset
, int len
)
62 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
65 printf("%08llx: ", (unsigned long long)offset
+ i
);
66 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++)
69 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
80 print_report(const char *op
, struct timeval
*t
, int64_t offset
,
81 int count
, int total
, int cnt
, int Cflag
)
83 char s1
[64], s2
[64], ts
[64];
85 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
87 cvtstr((double)total
, s1
, sizeof(s1
));
88 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
89 printf("%s %d/%d bytes at offset %lld\n",
90 op
, total
, count
, (long long)offset
);
91 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
92 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
93 } else {/* bytes,ops,time,bytes/sec,ops/sec */
94 printf("%d,%d,%s,%.3f,%.3f\n",
96 tdiv((double)total
, *t
),
97 tdiv((double)cnt
, *t
));
102 * Parse multiple length statements for vectored I/O, and construct an I/O
103 * vector matching it.
106 create_iovec(QEMUIOVector
*qiov
, char **argv
, int nr_iov
, int pattern
)
108 size_t *sizes
= calloc(nr_iov
, sizeof(size_t));
113 for (i
= 0; i
< nr_iov
; i
++) {
119 printf("non-numeric length argument -- %s\n", arg
);
123 /* should be SIZE_T_MAX, but that doesn't exist */
124 if (len
> UINT_MAX
) {
125 printf("too large length argument -- %s\n", arg
);
130 printf("length argument %lld is not sector aligned\n",
139 qemu_iovec_init(qiov
, nr_iov
);
141 buf
= p
= qemu_io_alloc(count
, pattern
);
143 for (i
= 0; i
< nr_iov
; i
++) {
144 qemu_iovec_add(qiov
, p
, sizes
[i
]);
152 static int do_read(char *buf
, int64_t offset
, int count
, int *total
)
156 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
163 static int do_write(char *buf
, int64_t offset
, int count
, int *total
)
167 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
174 static int do_pread(char *buf
, int64_t offset
, int count
, int *total
)
176 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
182 static int do_pwrite(char *buf
, int64_t offset
, int count
, int *total
)
184 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
190 static int do_load_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
192 *total
= bdrv_load_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
198 static int do_save_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
200 *total
= bdrv_save_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
206 #define NOT_DONE 0x7fffffff
207 static void aio_rw_done(void *opaque
, int ret
)
209 *(int *)opaque
= ret
;
212 static int do_aio_readv(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
214 BlockDriverAIOCB
*acb
;
215 int async_ret
= NOT_DONE
;
217 acb
= bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
218 aio_rw_done
, &async_ret
);
222 while (async_ret
== NOT_DONE
)
226 return async_ret
< 0 ? async_ret
: 1;
229 static int do_aio_writev(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
231 BlockDriverAIOCB
*acb
;
232 int async_ret
= NOT_DONE
;
234 acb
= bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
235 aio_rw_done
, &async_ret
);
239 while (async_ret
== NOT_DONE
)
243 return async_ret
< 0 ? async_ret
: 1;
247 static const cmdinfo_t read_cmd
;
254 " reads a range of bytes from the given offset\n"
257 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
259 " Reads a segment of the currently open file, optionally dumping it to the\n"
260 " standard output stream (with -v option) for subsequent inspection.\n"
261 " -b, -- read from the VM state rather than the virtual disk\n"
262 " -C, -- report statistics in a machine parsable format\n"
263 " -l, -- length for pattern verification (only with -P)\n"
264 " -p, -- use bdrv_pread to read the file\n"
265 " -P, -- use a pattern to verify read data\n"
266 " -q, -- quite mode, do not show I/O statistics\n"
267 " -s, -- start offset for pattern verification (only with -P)\n"
268 " -v, -- dump buffer to standard output\n"
273 read_f(int argc
, char **argv
)
275 struct timeval t1
, t2
;
276 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
277 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
282 /* Some compilers get confused and warn if this is not initialized. */
284 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
286 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != EOF
) {
296 pattern_count
= cvtnum(optarg
);
297 if (pattern_count
< 0) {
298 printf("non-numeric length argument -- %s\n", optarg
);
307 pattern
= atoi(optarg
);
314 pattern_offset
= cvtnum(optarg
);
315 if (pattern_offset
< 0) {
316 printf("non-numeric length argument -- %s\n", optarg
);
324 return command_usage(&read_cmd
);
328 if (optind
!= argc
- 2)
329 return command_usage(&read_cmd
);
331 if (bflag
&& pflag
) {
332 printf("-b and -p cannot be specified at the same time\n");
336 offset
= cvtnum(argv
[optind
]);
338 printf("non-numeric length argument -- %s\n", argv
[optind
]);
343 count
= cvtnum(argv
[optind
]);
345 printf("non-numeric length argument -- %s\n", argv
[optind
]);
349 if (!Pflag
&& (lflag
|| sflag
)) {
350 return command_usage(&read_cmd
);
354 pattern_count
= count
- pattern_offset
;
357 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
358 printf("pattern verfication range exceeds end of read data\n");
363 if (offset
& 0x1ff) {
364 printf("offset %lld is not sector aligned\n",
369 printf("count %d is not sector aligned\n",
375 buf
= qemu_io_alloc(count
, 0xab);
377 gettimeofday(&t1
, NULL
);
379 cnt
= do_pread(buf
, offset
, count
, &total
);
381 cnt
= do_load_vmstate(buf
, offset
, count
, &total
);
383 cnt
= do_read(buf
, offset
, count
, &total
);
384 gettimeofday(&t2
, NULL
);
387 printf("read failed: %s\n", strerror(-cnt
));
392 void* cmp_buf
= malloc(pattern_count
);
393 memset(cmp_buf
, pattern
, pattern_count
);
394 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
395 printf("Pattern verification failed at offset %lld, "
397 (long long) offset
+ pattern_offset
, pattern_count
);
406 dump_buffer(buf
, offset
, count
);
408 /* Finally, report back -- -C gives a parsable format */
410 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
418 static const cmdinfo_t read_cmd
= {
424 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
425 .oneline
= "reads a number of bytes at a specified offset",
429 static const cmdinfo_t readv_cmd
;
436 " reads a range of bytes from the given offset into multiple buffers\n"
439 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
441 " Reads a segment of the currently open file, optionally dumping it to the\n"
442 " standard output stream (with -v option) for subsequent inspection.\n"
443 " Uses multiple iovec buffers if more than one byte range is specified.\n"
444 " -C, -- report statistics in a machine parsable format\n"
445 " -P, -- use a pattern to verify read data\n"
446 " -v, -- dump buffer to standard output\n"
447 " -q, -- quite mode, do not show I/O statistics\n"
452 readv_f(int argc
, char **argv
)
454 struct timeval t1
, t2
;
455 int Cflag
= 0, qflag
= 0, vflag
= 0;
465 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
472 pattern
= atoi(optarg
);
481 return command_usage(&readv_cmd
);
485 if (optind
> argc
- 2)
486 return command_usage(&readv_cmd
);
489 offset
= cvtnum(argv
[optind
]);
491 printf("non-numeric length argument -- %s\n", argv
[optind
]);
496 if (offset
& 0x1ff) {
497 printf("offset %lld is not sector aligned\n",
502 nr_iov
= argc
- optind
;
503 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, 0xab);
505 gettimeofday(&t1
, NULL
);
506 cnt
= do_aio_readv(&qiov
, offset
, &total
);
507 gettimeofday(&t2
, NULL
);
510 printf("readv failed: %s\n", strerror(-cnt
));
515 void* cmp_buf
= malloc(qiov
.size
);
516 memset(cmp_buf
, pattern
, qiov
.size
);
517 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
518 printf("Pattern verification failed at offset %lld, "
520 (long long) offset
, qiov
.size
);
529 dump_buffer(buf
, offset
, qiov
.size
);
531 /* Finally, report back -- -C gives a parsable format */
533 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
540 static const cmdinfo_t readv_cmd
= {
545 .args
= "[-Cqv] [-P pattern ] off len [len..]",
546 .oneline
= "reads a number of bytes at a specified offset",
550 static const cmdinfo_t write_cmd
;
557 " writes a range of bytes from the given offset\n"
560 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
562 " Writes into a segment of the currently open file, using a buffer\n"
563 " filled with a set pattern (0xcdcdcdcd).\n"
564 " -b, -- write to the VM state rather than the virtual disk\n"
565 " -p, -- use bdrv_pwrite to write the file\n"
566 " -P, -- use different pattern to fill file\n"
567 " -C, -- report statistics in a machine parsable format\n"
568 " -q, -- quite mode, do not show I/O statistics\n"
573 write_f(int argc
, char **argv
)
575 struct timeval t1
, t2
;
576 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0;
581 /* Some compilers get confused and warn if this is not initialized. */
585 while ((c
= getopt(argc
, argv
, "bCpP:q")) != EOF
) {
597 pattern
= atoi(optarg
);
603 return command_usage(&write_cmd
);
607 if (optind
!= argc
- 2)
608 return command_usage(&write_cmd
);
610 if (bflag
&& pflag
) {
611 printf("-b and -p cannot be specified at the same time\n");
615 offset
= cvtnum(argv
[optind
]);
617 printf("non-numeric length argument -- %s\n", argv
[optind
]);
622 count
= cvtnum(argv
[optind
]);
624 printf("non-numeric length argument -- %s\n", argv
[optind
]);
629 if (offset
& 0x1ff) {
630 printf("offset %lld is not sector aligned\n",
636 printf("count %d is not sector aligned\n",
642 buf
= qemu_io_alloc(count
, pattern
);
644 gettimeofday(&t1
, NULL
);
646 cnt
= do_pwrite(buf
, offset
, count
, &total
);
648 cnt
= do_save_vmstate(buf
, offset
, count
, &total
);
650 cnt
= do_write(buf
, offset
, count
, &total
);
651 gettimeofday(&t2
, NULL
);
654 printf("write failed: %s\n", strerror(-cnt
));
661 /* Finally, report back -- -C gives a parsable format */
663 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
671 static const cmdinfo_t write_cmd
= {
677 .args
= "[-abCpq] [-P pattern ] off len",
678 .oneline
= "writes a number of bytes at a specified offset",
682 static const cmdinfo_t writev_cmd
;
689 " writes a range of bytes from the given offset source from multiple buffers\n"
692 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
694 " Writes into a segment of the currently open file, using a buffer\n"
695 " filled with a set pattern (0xcdcdcdcd).\n"
696 " -P, -- use different pattern to fill file\n"
697 " -C, -- report statistics in a machine parsable format\n"
698 " -q, -- quite mode, do not show I/O statistics\n"
703 writev_f(int argc
, char **argv
)
705 struct timeval t1
, t2
;
706 int Cflag
= 0, qflag
= 0;
715 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
724 pattern
= atoi(optarg
);
727 return command_usage(&writev_cmd
);
731 if (optind
> argc
- 2)
732 return command_usage(&writev_cmd
);
734 offset
= cvtnum(argv
[optind
]);
736 printf("non-numeric length argument -- %s\n", argv
[optind
]);
741 if (offset
& 0x1ff) {
742 printf("offset %lld is not sector aligned\n",
747 nr_iov
= argc
- optind
;
748 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, pattern
);
750 gettimeofday(&t1
, NULL
);
751 cnt
= do_aio_writev(&qiov
, offset
, &total
);
752 gettimeofday(&t2
, NULL
);
755 printf("writev failed: %s\n", strerror(-cnt
));
762 /* Finally, report back -- -C gives a parsable format */
764 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
770 static const cmdinfo_t writev_cmd
= {
775 .args
= "[-Cq] [-P pattern ] off len [len..]",
776 .oneline
= "writes a number of bytes at a specified offset",
793 aio_write_done(void *opaque
, int ret
)
795 struct aio_ctx
*ctx
= opaque
;
798 gettimeofday(&t2
, NULL
);
802 printf("aio_write failed: %s\n", strerror(-ret
));
810 /* Finally, report back -- -C gives a parsable format */
811 t2
= tsub(t2
, ctx
->t1
);
812 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
813 ctx
->qiov
.size
, 1, ctx
->Cflag
);
815 qemu_io_free(ctx
->buf
);
819 static const cmdinfo_t aio_read_cmd
;
822 aio_read_done(void *opaque
, int ret
)
824 struct aio_ctx
*ctx
= opaque
;
827 gettimeofday(&t2
, NULL
);
830 printf("readv failed: %s\n", strerror(-ret
));
835 void *cmp_buf
= malloc(ctx
->qiov
.size
);
837 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
838 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
839 printf("Pattern verification failed at offset %lld, "
841 (long long) ctx
->offset
, ctx
->qiov
.size
);
851 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
854 /* Finally, report back -- -C gives a parsable format */
855 t2
= tsub(t2
, ctx
->t1
);
856 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
857 ctx
->qiov
.size
, 1, ctx
->Cflag
);
859 qemu_io_free(ctx
->buf
);
868 " asynchronously reads a range of bytes from the given offset\n"
871 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
873 " Reads a segment of the currently open file, optionally dumping it to the\n"
874 " standard output stream (with -v option) for subsequent inspection.\n"
875 " The read is performed asynchronously and should the aio_flush command \n"
876 " should be used to ensure all outstanding aio requests have been completed\n"
877 " -C, -- report statistics in a machine parsable format\n"
878 " -P, -- use a pattern to verify read data\n"
879 " -v, -- dump buffer to standard output\n"
880 " -q, -- quite mode, do not show I/O statistics\n"
885 aio_read_f(int argc
, char **argv
)
888 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
889 BlockDriverAIOCB
*acb
;
891 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
898 ctx
->pattern
= atoi(optarg
);
908 return command_usage(&aio_read_cmd
);
912 if (optind
> argc
- 2) {
914 return command_usage(&aio_read_cmd
);
917 ctx
->offset
= cvtnum(argv
[optind
]);
918 if (ctx
->offset
< 0) {
919 printf("non-numeric length argument -- %s\n", argv
[optind
]);
925 if (ctx
->offset
& 0x1ff) {
926 printf("offset %lld is not sector aligned\n",
927 (long long)ctx
->offset
);
932 nr_iov
= argc
- optind
;
933 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
935 gettimeofday(&ctx
->t1
, NULL
);
936 acb
= bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
937 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
947 static const cmdinfo_t aio_read_cmd
= {
952 .args
= "[-Cqv] [-P pattern ] off len [len..]",
953 .oneline
= "asynchronously reads a number of bytes",
954 .help
= aio_read_help
,
957 static const cmdinfo_t aio_write_cmd
;
964 " asynchronously writes a range of bytes from the given offset source \n"
965 " from multiple buffers\n"
968 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
970 " Writes into a segment of the currently open file, using a buffer\n"
971 " filled with a set pattern (0xcdcdcdcd).\n"
972 " The write is performed asynchronously and should the aio_flush command \n"
973 " should be used to ensure all outstanding aio requests have been completed\n"
974 " -P, -- use different pattern to fill file\n"
975 " -C, -- report statistics in a machine parsable format\n"
976 " -q, -- quite mode, do not show I/O statistics\n"
982 aio_write_f(int argc
, char **argv
)
986 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
987 BlockDriverAIOCB
*acb
;
989 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
998 pattern
= atoi(optarg
);
1002 return command_usage(&aio_write_cmd
);
1006 if (optind
> argc
- 2) {
1008 return command_usage(&aio_write_cmd
);
1011 ctx
->offset
= cvtnum(argv
[optind
]);
1012 if (ctx
->offset
< 0) {
1013 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1019 if (ctx
->offset
& 0x1ff) {
1020 printf("offset %lld is not sector aligned\n",
1021 (long long)ctx
->offset
);
1026 nr_iov
= argc
- optind
;
1027 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1029 gettimeofday(&ctx
->t1
, NULL
);
1030 acb
= bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1031 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1041 static const cmdinfo_t aio_write_cmd
= {
1042 .name
= "aio_write",
1043 .cfunc
= aio_write_f
,
1046 .args
= "[-Cq] [-P pattern ] off len [len..]",
1047 .oneline
= "asynchronously writes a number of bytes",
1048 .help
= aio_write_help
,
1052 aio_flush_f(int argc
, char **argv
)
1058 static const cmdinfo_t aio_flush_cmd
= {
1059 .name
= "aio_flush",
1060 .cfunc
= aio_flush_f
,
1061 .oneline
= "completes all outstanding aio requets"
1065 flush_f(int argc
, char **argv
)
1071 static const cmdinfo_t flush_cmd
= {
1075 .oneline
= "flush all in-core file state to disk",
1079 truncate_f(int argc
, char **argv
)
1084 offset
= cvtnum(argv
[1]);
1086 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1090 ret
= bdrv_truncate(bs
, offset
);
1092 printf("truncate: %s", strerror(ret
));
1099 static const cmdinfo_t truncate_cmd
= {
1102 .cfunc
= truncate_f
,
1106 .oneline
= "truncates the current file at the given offset",
1110 length_f(int argc
, char **argv
)
1115 size
= bdrv_getlength(bs
);
1117 printf("getlength: %s", strerror(size
));
1121 cvtstr(size
, s1
, sizeof(s1
));
1127 static const cmdinfo_t length_cmd
= {
1131 .oneline
= "gets the length of the current file",
1136 info_f(int argc
, char **argv
)
1138 BlockDriverInfo bdi
;
1139 char s1
[64], s2
[64];
1142 if (bs
->drv
&& bs
->drv
->format_name
)
1143 printf("format name: %s\n", bs
->drv
->format_name
);
1144 if (bs
->drv
&& bs
->drv
->protocol_name
)
1145 printf("format name: %s\n", bs
->drv
->protocol_name
);
1147 ret
= bdrv_get_info(bs
, &bdi
);
1151 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1152 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1154 printf("cluster size: %s\n", s1
);
1155 printf("vm state offset: %s\n", s2
);
1162 static const cmdinfo_t info_cmd
= {
1166 .oneline
= "prints information about the current file",
1170 alloc_f(int argc
, char **argv
)
1179 offset
= cvtnum(argv
[1]);
1180 if (offset
& 0x1ff) {
1181 printf("offset %lld is not sector aligned\n",
1187 nb_sectors
= cvtnum(argv
[2]);
1191 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
1193 cvtstr(offset
, s1
, sizeof(s1
));
1195 retstr
= ret
? "allocated" : "not allocated";
1196 if (nb_sectors
== 1)
1197 printf("sector %s at offset %s\n", retstr
, s1
);
1199 printf("%d/%d sectors %s at offset %s\n",
1200 num
, nb_sectors
, retstr
, s1
);
1204 static const cmdinfo_t alloc_cmd
= {
1210 .args
= "off [sectors]",
1211 .oneline
= "checks if a sector is present in the file",
1215 close_f(int argc
, char **argv
)
1222 static const cmdinfo_t close_cmd
= {
1226 .oneline
= "close the current open file",
1229 static int openfile(char *name
, int flags
, int growable
)
1232 fprintf(stderr
, "file open already, try 'help close'\n");
1236 bs
= bdrv_new("hda");
1241 flags
|= BDRV_O_FILE
;
1244 if (bdrv_open(bs
, name
, flags
) == -1) {
1245 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1261 " opens a new file in the requested mode\n"
1264 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1266 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1267 " -C, -- create new file if it doesn't exist\n"
1268 " -r, -- open file read-only\n"
1269 " -s, -- use snapshot file\n"
1270 " -n, -- disable host cache\n"
1271 " -g, -- allow file to grow (only applies to protocols)"
1275 static const cmdinfo_t open_cmd
;
1278 open_f(int argc
, char **argv
)
1285 while ((c
= getopt(argc
, argv
, "snCrg")) != EOF
) {
1288 flags
|= BDRV_O_SNAPSHOT
;
1291 flags
|= BDRV_O_NOCACHE
;
1294 flags
|= BDRV_O_CREAT
;
1303 return command_usage(&open_cmd
);
1308 flags
|= BDRV_O_RDONLY
;
1310 flags
|= BDRV_O_RDWR
;
1312 if (optind
!= argc
- 1)
1313 return command_usage(&open_cmd
);
1315 return openfile(argv
[optind
], flags
, growable
);
1318 static const cmdinfo_t open_cmd
= {
1324 .flags
= CMD_NOFILE_OK
,
1325 .args
= "[-Crsn] [path]",
1326 .oneline
= "open the file specified by path",
1334 /* only one device allowed so far */
1342 const cmdinfo_t
*ct
)
1344 if (ct
->flags
& CMD_FLAG_GLOBAL
)
1346 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1347 fprintf(stderr
, "no file open, try 'help open'\n");
1353 static void usage(const char *name
)
1356 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1357 "QEMU Disk exerciser\n"
1359 " -C, --create create new file if it doesn't exist\n"
1360 " -c, --cmd command to execute\n"
1361 " -r, --read-only export read-only\n"
1362 " -s, --snapshot use snapshot file\n"
1363 " -n, --nocache disable host cache\n"
1364 " -g, --growable allow file to grow (only applies to protocols)\n"
1365 " -m, --misalign misalign allocations for O_DIRECT\n"
1366 " -h, --help display this help and exit\n"
1367 " -V, --version output version information and exit\n"
1373 int main(int argc
, char **argv
)
1377 const char *sopt
= "hVc:Crsnmg";
1378 struct option lopt
[] = {
1379 { "help", 0, NULL
, 'h' },
1380 { "version", 0, NULL
, 'V' },
1381 { "offset", 1, NULL
, 'o' },
1382 { "cmd", 1, NULL
, 'c' },
1383 { "create", 0, NULL
, 'C' },
1384 { "read-only", 0, NULL
, 'r' },
1385 { "snapshot", 0, NULL
, 's' },
1386 { "nocache", 0, NULL
, 'n' },
1387 { "misalign", 0, NULL
, 'm' },
1388 { "growable", 0, NULL
, 'g' },
1389 { NULL
, 0, NULL
, 0 }
1395 progname
= basename(argv
[0]);
1397 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1400 flags
|= BDRV_O_SNAPSHOT
;
1403 flags
|= BDRV_O_NOCACHE
;
1406 add_user_command(optarg
);
1409 flags
|= BDRV_O_CREAT
;
1421 printf("%s version %s\n", progname
, VERSION
);
1432 if ((argc
- optind
) > 1) {
1439 /* initialize commands */
1442 add_command(&open_cmd
);
1443 add_command(&close_cmd
);
1444 add_command(&read_cmd
);
1445 add_command(&readv_cmd
);
1446 add_command(&write_cmd
);
1447 add_command(&writev_cmd
);
1448 add_command(&aio_read_cmd
);
1449 add_command(&aio_write_cmd
);
1450 add_command(&aio_flush_cmd
);
1451 add_command(&flush_cmd
);
1452 add_command(&truncate_cmd
);
1453 add_command(&length_cmd
);
1454 add_command(&info_cmd
);
1455 add_command(&alloc_cmd
);
1457 add_args_command(init_args_command
);
1458 add_check_command(init_check_command
);
1460 /* open the device */
1462 flags
|= BDRV_O_RDONLY
;
1464 flags
|= BDRV_O_RDWR
;
1466 if ((argc
- optind
) == 1)
1467 openfile(argv
[optind
], flags
, growable
);
1471 * Make sure all outstanding requests get flushed the program exits.