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.
11 #include <sys/types.h>
17 #include "qemu-common.h"
18 #include "block_int.h"
21 #define VERSION "0.0.1"
23 #define CMD_NOFILE_OK 0x01
26 static BlockDriverState
*bs
;
31 * Memory allocation helpers.
33 * Make sure memory is aligned by default, or purposefully misaligned if
34 * that is specified on the command line.
37 #define MISALIGN_OFFSET 16
38 static void *qemu_io_alloc(size_t len
, int pattern
)
43 len
+= MISALIGN_OFFSET
;
44 buf
= qemu_memalign(512, len
);
45 memset(buf
, pattern
, len
);
47 buf
+= MISALIGN_OFFSET
;
51 static void qemu_io_free(void *p
)
59 dump_buffer(const void *buffer
, int64_t offset
, int len
)
64 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
67 printf("%08llx: ", (unsigned long long)offset
+ i
);
68 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++)
71 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
82 print_report(const char *op
, struct timeval
*t
, int64_t offset
,
83 int count
, int total
, int cnt
, int Cflag
)
85 char s1
[64], s2
[64], ts
[64];
87 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
89 cvtstr((double)total
, s1
, sizeof(s1
));
90 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
91 printf("%s %d/%d bytes at offset %lld\n",
92 op
, total
, count
, (long long)offset
);
93 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
94 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
95 } else {/* bytes,ops,time,bytes/sec,ops/sec */
96 printf("%d,%d,%s,%.3f,%.3f\n",
98 tdiv((double)total
, *t
),
99 tdiv((double)cnt
, *t
));
104 * Parse multiple length statements for vectored I/O, and construct an I/O
105 * vector matching it.
108 create_iovec(QEMUIOVector
*qiov
, char **argv
, int nr_iov
, int pattern
)
110 size_t *sizes
= calloc(nr_iov
, sizeof(size_t));
115 for (i
= 0; i
< nr_iov
; i
++) {
121 printf("non-numeric length argument -- %s\n", arg
);
125 /* should be SIZE_T_MAX, but that doesn't exist */
126 if (len
> UINT_MAX
) {
127 printf("too large length argument -- %s\n", arg
);
132 printf("length argument %lld is not sector aligned\n",
141 qemu_iovec_init(qiov
, nr_iov
);
143 buf
= p
= qemu_io_alloc(count
, pattern
);
145 for (i
= 0; i
< nr_iov
; i
++) {
146 qemu_iovec_add(qiov
, p
, sizes
[i
]);
154 static int do_read(char *buf
, int64_t offset
, int count
, int *total
)
158 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
165 static int do_write(char *buf
, int64_t offset
, int count
, int *total
)
169 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
176 static int do_pread(char *buf
, int64_t offset
, int count
, int *total
)
178 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
184 static int do_pwrite(char *buf
, int64_t offset
, int count
, int *total
)
186 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
192 static int do_load_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
194 *total
= bdrv_load_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
200 static int do_save_vmstate(char *buf
, int64_t offset
, int count
, int *total
)
202 *total
= bdrv_save_vmstate(bs
, (uint8_t *)buf
, offset
, count
);
208 #define NOT_DONE 0x7fffffff
209 static void aio_rw_done(void *opaque
, int ret
)
211 *(int *)opaque
= ret
;
214 static int do_aio_readv(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
216 BlockDriverAIOCB
*acb
;
217 int async_ret
= NOT_DONE
;
219 acb
= bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
220 aio_rw_done
, &async_ret
);
224 while (async_ret
== NOT_DONE
)
228 return async_ret
< 0 ? async_ret
: 1;
231 static int do_aio_writev(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
233 BlockDriverAIOCB
*acb
;
234 int async_ret
= NOT_DONE
;
236 acb
= bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
237 aio_rw_done
, &async_ret
);
241 while (async_ret
== NOT_DONE
)
245 return async_ret
< 0 ? async_ret
: 1;
249 static const cmdinfo_t read_cmd
;
256 " reads a range of bytes from the given offset\n"
259 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
261 " Reads a segment of the currently open file, optionally dumping it to the\n"
262 " standard output stream (with -v option) for subsequent inspection.\n"
263 " -b, -- read from the VM state rather than the virtual disk\n"
264 " -C, -- report statistics in a machine parsable format\n"
265 " -l, -- length for pattern verification (only with -P)\n"
266 " -p, -- use bdrv_pread to read the file\n"
267 " -P, -- use a pattern to verify read data\n"
268 " -q, -- quite mode, do not show I/O statistics\n"
269 " -s, -- start offset for pattern verification (only with -P)\n"
270 " -v, -- dump buffer to standard output\n"
275 read_f(int argc
, char **argv
)
277 struct timeval t1
, t2
;
278 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
279 int Pflag
= 0, sflag
= 0, lflag
= 0, bflag
= 0;
284 /* Some compilers get confused and warn if this is not initialized. */
286 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
288 while ((c
= getopt(argc
, argv
, "bCl:pP:qs:v")) != EOF
) {
298 pattern_count
= cvtnum(optarg
);
299 if (pattern_count
< 0) {
300 printf("non-numeric length argument -- %s\n", optarg
);
309 pattern
= atoi(optarg
);
316 pattern_offset
= cvtnum(optarg
);
317 if (pattern_offset
< 0) {
318 printf("non-numeric length argument -- %s\n", optarg
);
326 return command_usage(&read_cmd
);
330 if (optind
!= argc
- 2)
331 return command_usage(&read_cmd
);
333 if (bflag
&& pflag
) {
334 printf("-b and -p cannot be specified at the same time\n");
338 offset
= cvtnum(argv
[optind
]);
340 printf("non-numeric length argument -- %s\n", argv
[optind
]);
345 count
= cvtnum(argv
[optind
]);
347 printf("non-numeric length argument -- %s\n", argv
[optind
]);
351 if (!Pflag
&& (lflag
|| sflag
)) {
352 return command_usage(&read_cmd
);
356 pattern_count
= count
- pattern_offset
;
359 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
360 printf("pattern verfication range exceeds end of read data\n");
365 if (offset
& 0x1ff) {
366 printf("offset %lld is not sector aligned\n",
371 printf("count %d is not sector aligned\n",
377 buf
= qemu_io_alloc(count
, 0xab);
379 gettimeofday(&t1
, NULL
);
381 cnt
= do_pread(buf
, offset
, count
, &total
);
383 cnt
= do_load_vmstate(buf
, offset
, count
, &total
);
385 cnt
= do_read(buf
, offset
, count
, &total
);
386 gettimeofday(&t2
, NULL
);
389 printf("read failed: %s\n", strerror(-cnt
));
394 void* cmp_buf
= malloc(pattern_count
);
395 memset(cmp_buf
, pattern
, pattern_count
);
396 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
397 printf("Pattern verification failed at offset %lld, "
399 (long long) offset
+ pattern_offset
, pattern_count
);
408 dump_buffer(buf
, offset
, count
);
410 /* Finally, report back -- -C gives a parsable format */
412 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
420 static const cmdinfo_t read_cmd
= {
426 .args
= "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
427 .oneline
= "reads a number of bytes at a specified offset",
431 static const cmdinfo_t readv_cmd
;
438 " reads a range of bytes from the given offset into multiple buffers\n"
441 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
443 " Reads a segment of the currently open file, optionally dumping it to the\n"
444 " standard output stream (with -v option) for subsequent inspection.\n"
445 " Uses multiple iovec buffers if more than one byte range is specified.\n"
446 " -C, -- report statistics in a machine parsable format\n"
447 " -P, -- use a pattern to verify read data\n"
448 " -v, -- dump buffer to standard output\n"
449 " -q, -- quite mode, do not show I/O statistics\n"
454 readv_f(int argc
, char **argv
)
456 struct timeval t1
, t2
;
457 int Cflag
= 0, qflag
= 0, vflag
= 0;
467 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
474 pattern
= atoi(optarg
);
483 return command_usage(&readv_cmd
);
487 if (optind
> argc
- 2)
488 return command_usage(&readv_cmd
);
491 offset
= cvtnum(argv
[optind
]);
493 printf("non-numeric length argument -- %s\n", argv
[optind
]);
498 if (offset
& 0x1ff) {
499 printf("offset %lld is not sector aligned\n",
504 nr_iov
= argc
- optind
;
505 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, 0xab);
507 gettimeofday(&t1
, NULL
);
508 cnt
= do_aio_readv(&qiov
, offset
, &total
);
509 gettimeofday(&t2
, NULL
);
512 printf("readv failed: %s\n", strerror(-cnt
));
517 void* cmp_buf
= malloc(qiov
.size
);
518 memset(cmp_buf
, pattern
, qiov
.size
);
519 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
520 printf("Pattern verification failed at offset %lld, "
522 (long long) offset
, qiov
.size
);
531 dump_buffer(buf
, offset
, qiov
.size
);
533 /* Finally, report back -- -C gives a parsable format */
535 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
542 static const cmdinfo_t readv_cmd
= {
547 .args
= "[-Cqv] [-P pattern ] off len [len..]",
548 .oneline
= "reads a number of bytes at a specified offset",
552 static const cmdinfo_t write_cmd
;
559 " writes a range of bytes from the given offset\n"
562 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
564 " Writes into a segment of the currently open file, using a buffer\n"
565 " filled with a set pattern (0xcdcdcdcd).\n"
566 " -b, -- write to the VM state rather than the virtual disk\n"
567 " -p, -- use bdrv_pwrite to write the file\n"
568 " -P, -- use different pattern to fill file\n"
569 " -C, -- report statistics in a machine parsable format\n"
570 " -q, -- quite mode, do not show I/O statistics\n"
575 write_f(int argc
, char **argv
)
577 struct timeval t1
, t2
;
578 int Cflag
= 0, pflag
= 0, qflag
= 0, bflag
= 0;
583 /* Some compilers get confused and warn if this is not initialized. */
587 while ((c
= getopt(argc
, argv
, "bCpP:q")) != EOF
) {
599 pattern
= atoi(optarg
);
605 return command_usage(&write_cmd
);
609 if (optind
!= argc
- 2)
610 return command_usage(&write_cmd
);
612 if (bflag
&& pflag
) {
613 printf("-b and -p cannot be specified at the same time\n");
617 offset
= cvtnum(argv
[optind
]);
619 printf("non-numeric length argument -- %s\n", argv
[optind
]);
624 count
= cvtnum(argv
[optind
]);
626 printf("non-numeric length argument -- %s\n", argv
[optind
]);
631 if (offset
& 0x1ff) {
632 printf("offset %lld is not sector aligned\n",
638 printf("count %d is not sector aligned\n",
644 buf
= qemu_io_alloc(count
, pattern
);
646 gettimeofday(&t1
, NULL
);
648 cnt
= do_pwrite(buf
, offset
, count
, &total
);
650 cnt
= do_save_vmstate(buf
, offset
, count
, &total
);
652 cnt
= do_write(buf
, offset
, count
, &total
);
653 gettimeofday(&t2
, NULL
);
656 printf("write failed: %s\n", strerror(-cnt
));
663 /* Finally, report back -- -C gives a parsable format */
665 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
673 static const cmdinfo_t write_cmd
= {
679 .args
= "[-abCpq] [-P pattern ] off len",
680 .oneline
= "writes a number of bytes at a specified offset",
684 static const cmdinfo_t writev_cmd
;
691 " writes a range of bytes from the given offset source from multiple buffers\n"
694 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
696 " Writes into a segment of the currently open file, using a buffer\n"
697 " filled with a set pattern (0xcdcdcdcd).\n"
698 " -P, -- use different pattern to fill file\n"
699 " -C, -- report statistics in a machine parsable format\n"
700 " -q, -- quite mode, do not show I/O statistics\n"
705 writev_f(int argc
, char **argv
)
707 struct timeval t1
, t2
;
708 int Cflag
= 0, qflag
= 0;
717 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
726 pattern
= atoi(optarg
);
729 return command_usage(&writev_cmd
);
733 if (optind
> argc
- 2)
734 return command_usage(&writev_cmd
);
736 offset
= cvtnum(argv
[optind
]);
738 printf("non-numeric length argument -- %s\n", argv
[optind
]);
743 if (offset
& 0x1ff) {
744 printf("offset %lld is not sector aligned\n",
749 nr_iov
= argc
- optind
;
750 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, pattern
);
752 gettimeofday(&t1
, NULL
);
753 cnt
= do_aio_writev(&qiov
, offset
, &total
);
754 gettimeofday(&t2
, NULL
);
757 printf("writev failed: %s\n", strerror(-cnt
));
764 /* Finally, report back -- -C gives a parsable format */
766 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
772 static const cmdinfo_t writev_cmd
= {
777 .args
= "[-Cq] [-P pattern ] off len [len..]",
778 .oneline
= "writes a number of bytes at a specified offset",
795 aio_write_done(void *opaque
, int ret
)
797 struct aio_ctx
*ctx
= opaque
;
800 gettimeofday(&t2
, NULL
);
804 printf("aio_write failed: %s\n", strerror(-ret
));
812 /* Finally, report back -- -C gives a parsable format */
813 t2
= tsub(t2
, ctx
->t1
);
814 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
815 ctx
->qiov
.size
, 1, ctx
->Cflag
);
817 qemu_io_free(ctx
->buf
);
821 static const cmdinfo_t aio_read_cmd
;
824 aio_read_done(void *opaque
, int ret
)
826 struct aio_ctx
*ctx
= opaque
;
829 gettimeofday(&t2
, NULL
);
832 printf("readv failed: %s\n", strerror(-ret
));
837 void *cmp_buf
= malloc(ctx
->qiov
.size
);
839 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
840 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
841 printf("Pattern verification failed at offset %lld, "
843 (long long) ctx
->offset
, ctx
->qiov
.size
);
853 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
856 /* Finally, report back -- -C gives a parsable format */
857 t2
= tsub(t2
, ctx
->t1
);
858 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
859 ctx
->qiov
.size
, 1, ctx
->Cflag
);
861 qemu_io_free(ctx
->buf
);
870 " asynchronously reads a range of bytes from the given offset\n"
873 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
875 " Reads a segment of the currently open file, optionally dumping it to the\n"
876 " standard output stream (with -v option) for subsequent inspection.\n"
877 " The read is performed asynchronously and should the aio_flush command \n"
878 " should be used to ensure all outstanding aio requests have been completed\n"
879 " -C, -- report statistics in a machine parsable format\n"
880 " -P, -- use a pattern to verify read data\n"
881 " -v, -- dump buffer to standard output\n"
882 " -q, -- quite mode, do not show I/O statistics\n"
887 aio_read_f(int argc
, char **argv
)
890 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
891 BlockDriverAIOCB
*acb
;
893 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
900 ctx
->pattern
= atoi(optarg
);
910 return command_usage(&aio_read_cmd
);
914 if (optind
> argc
- 2) {
916 return command_usage(&aio_read_cmd
);
919 ctx
->offset
= cvtnum(argv
[optind
]);
920 if (ctx
->offset
< 0) {
921 printf("non-numeric length argument -- %s\n", argv
[optind
]);
927 if (ctx
->offset
& 0x1ff) {
928 printf("offset %lld is not sector aligned\n",
929 (long long)ctx
->offset
);
934 nr_iov
= argc
- optind
;
935 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
937 gettimeofday(&ctx
->t1
, NULL
);
938 acb
= bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
939 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
949 static const cmdinfo_t aio_read_cmd
= {
954 .args
= "[-Cqv] [-P pattern ] off len [len..]",
955 .oneline
= "asynchronously reads a number of bytes",
956 .help
= aio_read_help
,
959 static const cmdinfo_t aio_write_cmd
;
966 " asynchronously writes a range of bytes from the given offset source \n"
967 " from multiple buffers\n"
970 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
972 " Writes into a segment of the currently open file, using a buffer\n"
973 " filled with a set pattern (0xcdcdcdcd).\n"
974 " The write is performed asynchronously and should the aio_flush command \n"
975 " should be used to ensure all outstanding aio requests have been completed\n"
976 " -P, -- use different pattern to fill file\n"
977 " -C, -- report statistics in a machine parsable format\n"
978 " -q, -- quite mode, do not show I/O statistics\n"
984 aio_write_f(int argc
, char **argv
)
988 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
989 BlockDriverAIOCB
*acb
;
991 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
1000 pattern
= atoi(optarg
);
1004 return command_usage(&aio_write_cmd
);
1008 if (optind
> argc
- 2) {
1010 return command_usage(&aio_write_cmd
);
1013 ctx
->offset
= cvtnum(argv
[optind
]);
1014 if (ctx
->offset
< 0) {
1015 printf("non-numeric length argument -- %s\n", argv
[optind
]);
1021 if (ctx
->offset
& 0x1ff) {
1022 printf("offset %lld is not sector aligned\n",
1023 (long long)ctx
->offset
);
1028 nr_iov
= argc
- optind
;
1029 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
1031 gettimeofday(&ctx
->t1
, NULL
);
1032 acb
= bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
1033 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
1043 static const cmdinfo_t aio_write_cmd
= {
1044 .name
= "aio_write",
1045 .cfunc
= aio_write_f
,
1048 .args
= "[-Cq] [-P pattern ] off len [len..]",
1049 .oneline
= "asynchronously writes a number of bytes",
1050 .help
= aio_write_help
,
1054 aio_flush_f(int argc
, char **argv
)
1060 static const cmdinfo_t aio_flush_cmd
= {
1061 .name
= "aio_flush",
1062 .cfunc
= aio_flush_f
,
1063 .oneline
= "completes all outstanding aio requets"
1067 flush_f(int argc
, char **argv
)
1073 static const cmdinfo_t flush_cmd
= {
1077 .oneline
= "flush all in-core file state to disk",
1081 truncate_f(int argc
, char **argv
)
1086 offset
= cvtnum(argv
[1]);
1088 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1092 ret
= bdrv_truncate(bs
, offset
);
1094 printf("truncate: %s", strerror(ret
));
1101 static const cmdinfo_t truncate_cmd
= {
1104 .cfunc
= truncate_f
,
1108 .oneline
= "truncates the current file at the given offset",
1112 length_f(int argc
, char **argv
)
1117 size
= bdrv_getlength(bs
);
1119 printf("getlength: %s", strerror(size
));
1123 cvtstr(size
, s1
, sizeof(s1
));
1129 static const cmdinfo_t length_cmd
= {
1133 .oneline
= "gets the length of the current file",
1138 info_f(int argc
, char **argv
)
1140 BlockDriverInfo bdi
;
1141 char s1
[64], s2
[64];
1144 if (bs
->drv
&& bs
->drv
->format_name
)
1145 printf("format name: %s\n", bs
->drv
->format_name
);
1146 if (bs
->drv
&& bs
->drv
->protocol_name
)
1147 printf("format name: %s\n", bs
->drv
->protocol_name
);
1149 ret
= bdrv_get_info(bs
, &bdi
);
1153 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1154 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1156 printf("cluster size: %s\n", s1
);
1157 printf("vm state offset: %s\n", s2
);
1164 static const cmdinfo_t info_cmd
= {
1168 .oneline
= "prints information about the current file",
1172 alloc_f(int argc
, char **argv
)
1181 offset
= cvtnum(argv
[1]);
1182 if (offset
& 0x1ff) {
1183 printf("offset %lld is not sector aligned\n",
1189 nb_sectors
= cvtnum(argv
[2]);
1193 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
1195 cvtstr(offset
, s1
, sizeof(s1
));
1197 retstr
= ret
? "allocated" : "not allocated";
1198 if (nb_sectors
== 1)
1199 printf("sector %s at offset %s\n", retstr
, s1
);
1201 printf("%d/%d sectors %s at offset %s\n",
1202 num
, nb_sectors
, retstr
, s1
);
1206 static const cmdinfo_t alloc_cmd
= {
1212 .args
= "off [sectors]",
1213 .oneline
= "checks if a sector is present in the file",
1217 close_f(int argc
, char **argv
)
1224 static const cmdinfo_t close_cmd
= {
1228 .oneline
= "close the current open file",
1231 static int openfile(char *name
, int flags
, int growable
)
1234 fprintf(stderr
, "file open already, try 'help close'\n");
1238 bs
= bdrv_new("hda");
1243 flags
|= BDRV_O_FILE
;
1246 if (bdrv_open(bs
, name
, flags
) == -1) {
1247 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1263 " opens a new file in the requested mode\n"
1266 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1268 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1269 " -C, -- create new file if it doesn't exist\n"
1270 " -r, -- open file read-only\n"
1271 " -s, -- use snapshot file\n"
1272 " -n, -- disable host cache\n"
1273 " -g, -- allow file to grow (only applies to protocols)"
1277 static const cmdinfo_t open_cmd
;
1280 open_f(int argc
, char **argv
)
1287 while ((c
= getopt(argc
, argv
, "snCrg")) != EOF
) {
1290 flags
|= BDRV_O_SNAPSHOT
;
1293 flags
|= BDRV_O_NOCACHE
;
1296 flags
|= BDRV_O_CREAT
;
1305 return command_usage(&open_cmd
);
1310 flags
|= BDRV_O_RDONLY
;
1312 flags
|= BDRV_O_RDWR
;
1314 if (optind
!= argc
- 1)
1315 return command_usage(&open_cmd
);
1317 return openfile(argv
[optind
], flags
, growable
);
1320 static const cmdinfo_t open_cmd
= {
1326 .flags
= CMD_NOFILE_OK
,
1327 .args
= "[-Crsn] [path]",
1328 .oneline
= "open the file specified by path",
1336 /* only one device allowed so far */
1344 const cmdinfo_t
*ct
)
1346 if (ct
->flags
& CMD_FLAG_GLOBAL
)
1348 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1349 fprintf(stderr
, "no file open, try 'help open'\n");
1355 static void usage(const char *name
)
1358 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1359 "QEMU Disk exerciser\n"
1361 " -C, --create create new file if it doesn't exist\n"
1362 " -c, --cmd command to execute\n"
1363 " -r, --read-only export read-only\n"
1364 " -s, --snapshot use snapshot file\n"
1365 " -n, --nocache disable host cache\n"
1366 " -g, --growable allow file to grow (only applies to protocols)\n"
1367 " -m, --misalign misalign allocations for O_DIRECT\n"
1368 " -h, --help display this help and exit\n"
1369 " -V, --version output version information and exit\n"
1375 int main(int argc
, char **argv
)
1379 const char *sopt
= "hVc:Crsnmg";
1380 struct option lopt
[] = {
1381 { "help", 0, NULL
, 'h' },
1382 { "version", 0, NULL
, 'V' },
1383 { "offset", 1, NULL
, 'o' },
1384 { "cmd", 1, NULL
, 'c' },
1385 { "create", 0, NULL
, 'C' },
1386 { "read-only", 0, NULL
, 'r' },
1387 { "snapshot", 0, NULL
, 's' },
1388 { "nocache", 0, NULL
, 'n' },
1389 { "misalign", 0, NULL
, 'm' },
1390 { "growable", 0, NULL
, 'g' },
1391 { NULL
, 0, NULL
, 0 }
1397 progname
= basename(argv
[0]);
1399 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1402 flags
|= BDRV_O_SNAPSHOT
;
1405 flags
|= BDRV_O_NOCACHE
;
1408 add_user_command(optarg
);
1411 flags
|= BDRV_O_CREAT
;
1423 printf("%s version %s\n", progname
, VERSION
);
1434 if ((argc
- optind
) > 1) {
1441 /* initialize commands */
1444 add_command(&open_cmd
);
1445 add_command(&close_cmd
);
1446 add_command(&read_cmd
);
1447 add_command(&readv_cmd
);
1448 add_command(&write_cmd
);
1449 add_command(&writev_cmd
);
1450 add_command(&aio_read_cmd
);
1451 add_command(&aio_write_cmd
);
1452 add_command(&aio_flush_cmd
);
1453 add_command(&flush_cmd
);
1454 add_command(&truncate_cmd
);
1455 add_command(&length_cmd
);
1456 add_command(&info_cmd
);
1457 add_command(&alloc_cmd
);
1459 add_args_command(init_args_command
);
1460 add_check_command(init_check_command
);
1462 /* open the device */
1464 flags
|= BDRV_O_RDONLY
;
1466 flags
|= BDRV_O_RDWR
;
1468 if ((argc
- optind
) == 1)
1469 openfile(argv
[optind
], flags
, growable
);
1473 * Make sure all outstanding requests get flushed the program exits.