From 7d8b25524ca326e86573aefbf3d1ba323f6a897f Mon Sep 17 00:00:00 2001 From: ccardenas Date: Sun, 9 Sep 2018 04:25:32 +0000 Subject: [PATCH] Add disk format tests. Thanks to Ori Bernstein. --- regress/usr.sbin/vmd/diskfmt/Makefile | 28 ++++ regress/usr.sbin/vmd/diskfmt/vioscribble.c | 165 +++++++++++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 regress/usr.sbin/vmd/diskfmt/Makefile create mode 100644 regress/usr.sbin/vmd/diskfmt/vioscribble.c diff --git a/regress/usr.sbin/vmd/diskfmt/Makefile b/regress/usr.sbin/vmd/diskfmt/Makefile new file mode 100644 index 00000000000..c2a5f42d5f6 --- /dev/null +++ b/regress/usr.sbin/vmd/diskfmt/Makefile @@ -0,0 +1,28 @@ +# $OpenBSD: Makefile,v 1.1 2018/09/09 04:25:32 ccardenas Exp $ + +# This regression test creates a raw disk image and a +# qcow disk image, and scribbles the same data to both +# of them. It verifies that they both have the same +# result. +# +# In order for this test to work, qemu must be installed +# in order to create the disk images. + +VMD_DIR=$(BSDSRCDIR)/usr.sbin/vmd/ + +PROG=vioscribble +SRCS=vioscribble.c $(VMD_DIR)/vioqcow2.c $(VMD_DIR)/vioraw.c +CFLAGS+=-I$(VMD_DIR) -pthread +LDFLAGS+=-pthread + +run-regress-vioscribble: scribble-images + +scribble-images: + rm -f scribble.raw scribble.qc2 + vmctl create scribble.raw -s 4G + qemu-img create -f qcow2 scribble.qc2 4G + + +.PHONY: ${REGRESS_TARGETS} scribble-images + +.include diff --git a/regress/usr.sbin/vmd/diskfmt/vioscribble.c b/regress/usr.sbin/vmd/diskfmt/vioscribble.c new file mode 100644 index 00000000000..14d720db652 --- /dev/null +++ b/regress/usr.sbin/vmd/diskfmt/vioscribble.c @@ -0,0 +1,165 @@ +/* $OpenBSD: vioscribble.c,v 1.1 2018/09/09 04:25:32 ccardenas Exp $ */ + +/* + * Copyright (c) 2018 Ori Bernstein + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * Quick hack of a program to try to test vioqcow2.c against + * vioraw.c. + * + * Compile with: + * + * cc -pthread -o scribble vioscribble.c vioqcow2.c vioraw.c + */ +#include /* PAGE_SIZE */ +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "pci.h" +#include "vmd.h" +#include "vmm.h" +#include "virtio.h" + +#define CLUSTERSZ 65536 + +int verbose; +struct virtio_backing qcowfile; +struct virtio_backing rawfile; + +/* We expect the scribble disks to be 4g in size */ +#define DISKSZ (4ull*1024ull*1024ull*1024ull) + +/* functions that io code depends on */ + +void +log_debug(const char *emsg, ...) +{ + if (verbose) { + va_list ap; + + va_start(ap, emsg); + vfprintf(stdout, emsg, ap); + fprintf(stdout, "\n"); + va_end(ap); + } +} + +void +log_warnx(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vfprintf(stdout, emsg, ap); + fprintf(stdout, "\n"); + va_end(ap); +} + +void +log_warn(const char *emsg, ...) +{ + va_list ap; + + va_start(ap, emsg); + vfprintf(stdout, emsg, ap); + fprintf(stdout, "\n"); + va_end(ap); +} + +static void +fill(size_t off, char *buf, size_t len) +{ + size_t i; + + /* use the top bits of off, since we can guess at where we went wrong. */ + for (i = 0; i < len; i++) + buf[i] = (off >> 8); +} + +int +main(int argc, char **argv) +{ + int qcfd, rawfd, i; + char buf[64*1024], cmp[64*1024]; + off_t len, off, qcsz, rawsz; + + verbose = !!getenv("VERBOSE"); + qcfd = open("scribble.qc2", O_RDWR); + rawfd = open("scribble.raw", O_RDWR); + if (qcfd == -1 || virtio_init_qcow2(&qcowfile, &qcsz, qcfd) == -1) + err(1, "unable to open qcow"); + if (rawfd == -1 || virtio_init_raw(&rawfile, &rawsz, rawfd) == -1) + err(1, "unable to open raw"); + + srandom_deterministic(123); + + /* scribble to both disks */ + printf("scribbling...\n"); + for (i = 0; i < 16; i++) { + off = (random() % DISKSZ); + len = random() % sizeof buf + 1; + fill(off, buf, sizeof buf); + if (qcowfile.pwrite(qcowfile.p, buf, len, off) == -1) + printf("iter %d: unable to write at %llx\n", i, off); + rawfile.pwrite(rawfile.p, buf, len, off); + + if (qcowfile.pread(qcowfile.p, buf, len, off) == -1) + printf("unable to read at %llx\n", off); + rawfile.pread(rawfile.p, cmp, len, off); + if (memcmp(buf, cmp, len) != 0) { + printf("iter %d: mismatch at 0x%llx (espected val: %d)\n", + i, off, (char)(off >> 8)); + break; + } + } + + /* validate that both disks match */ + printf("validating...\n"); + for (off = 0; off < DISKSZ; off += sizeof buf) { + if (qcowfile.pread(qcowfile.p, buf, sizeof buf, off) == -1) + printf("unable to read at %llx\n", off); + rawfile.pread(rawfile.p, cmp, sizeof buf, off); + if (memcmp(buf, cmp, sizeof buf) != 0) { + printf("mismatch at 0x%llx (espected val: %d)\n", + off, (char)(off >> 8)); + break; + } + } + return 0; +} -- 2.20.1