Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 26 Nov 2014 00:58:37 +0000 (UTC)
From:      Marcel Moolenaar <marcel@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-user@freebsd.org
Subject:   svn commit: r275095 - user/marcel/libvdsk/libvdsk
Message-ID:  <201411260058.sAQ0wbPU095009@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: marcel
Date: Wed Nov 26 00:58:36 2014
New Revision: 275095
URL: https://svnweb.freebsd.org/changeset/base/275095

Log:
  Add qcow.c, vhd.c and vmdk.c for adding support for QCOW, VHD and VMDK
  (resp.). These formats return nothing but errors, but help to test the
  probe logic.
  It's generally good to be able to detect a format, even if support for
  it is not present. It avoids treating the file as a raw disk.

Added:
  user/marcel/libvdsk/libvdsk/qcow.c   (contents, props changed)
  user/marcel/libvdsk/libvdsk/vhd.c   (contents, props changed)
  user/marcel/libvdsk/libvdsk/vmdk.c   (contents, props changed)
Modified:
  user/marcel/libvdsk/libvdsk/Makefile

Modified: user/marcel/libvdsk/libvdsk/Makefile
==============================================================================
--- user/marcel/libvdsk/libvdsk/Makefile	Wed Nov 26 00:48:07 2014	(r275094)
+++ user/marcel/libvdsk/libvdsk/Makefile	Wed Nov 26 00:58:36 2014	(r275095)
@@ -8,7 +8,10 @@ INCS=	vdsk.h
 
 # List of formats to support
 SRCS+=	\
-	raw.c
+	qcow.c \
+	raw.c \
+	vhd.c \
+	vmdk.c
 
 DEBUG_FLAGS=-O0 -gdwarf-2
 

Added: user/marcel/libvdsk/libvdsk/qcow.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/marcel/libvdsk/libvdsk/qcow.c	Wed Nov 26 00:58:36 2014	(r275095)
@@ -0,0 +1,148 @@
+/*-
+ * Copyright (c) 2014 Marcel Moolenaar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/disk.h>
+#include <sys/endian.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <vdsk.h>
+
+#include "vdsk_int.h"
+
+/* Flag bits in cluster offsets */
+#define	QCOW_CLSTR_COMPRESSED	(1ULL << 62)
+#define	QCOW_CLSTR_COPIED	(1ULL << 63)
+
+struct qcow_header {
+	uint32_t	magic;
+#define	QCOW_MAGIC		0x514649fb
+	uint32_t	version;
+#define	QCOW_VERSION_1		1
+#define	QCOW_VERSION_2		2
+	uint64_t	path_offset;
+	uint32_t	path_length;
+	uint32_t	clstr_log2sz;	/* v2 only */
+	uint64_t	disk_size;
+	union {
+		struct {
+			uint8_t		clstr_log2sz;
+			uint8_t		l2_log2sz;
+			uint16_t	_pad;
+			uint32_t	encryption;
+			uint64_t	l1_offset;
+		} v1;
+		struct {
+			uint32_t	encryption;
+			uint32_t	l1_entries;
+			uint64_t	l1_offset;
+			uint64_t	refcnt_offset;
+			uint32_t	refcnt_entries;
+			uint32_t	snapshot_count;
+			uint64_t	snapshot_offset;
+		} v2;
+	} u;
+};
+
+static int
+qcow_probe(struct vdsk *vdsk)
+{
+	struct qcow_header *hdr;
+
+	if (vdsk->sectorsize < 512 || vdsk->sectorsize > 4096)
+		return (ENOTBLK);
+
+	hdr = malloc(vdsk->sectorsize);
+	if (hdr == NULL)
+		return (errno);
+
+	if (read(vdsk->fd, hdr, vdsk->sectorsize) != vdsk->sectorsize)
+		return (errno);
+
+	if (be32dec(&hdr->magic) != QCOW_MAGIC)
+		return (ENXIO);
+
+	return (0);
+}
+
+static int
+qcow_open(struct vdsk *vdsk __unused)
+{
+
+	return (ENOSYS);
+}
+
+static int
+qcow_close(struct vdsk *vdsk __unused)
+{
+
+	return (ENOSYS);
+}
+
+static int
+qcow_read(struct vdsk *vdsk __unused, const struct iovec *iov __unused,
+    int iovcnt __unused, off_t offset __unused)
+{
+
+	return (ENOSYS);
+}
+
+static int
+qcow_write(struct vdsk *vdsk __unused, const struct iovec *iov __unused,
+    int iovcnt __unused, off_t offset __unused)
+{
+
+	return (ENOSYS);
+}
+
+static int
+qcow_flush(struct vdsk *vdsk __unused)
+{
+
+	return (ENOSYS);
+}
+
+static struct vdsk_format qcow_format = {
+	.name = "qcow",
+	.description = "QEMU Copy-On-Write, version 1",
+	.flags = VDSKFMT_HAS_HEADER,
+	.probe = qcow_probe,
+	.open = qcow_open,
+	.close = qcow_close,
+	.read = qcow_read,
+	.write = qcow_write,
+	.flush = qcow_flush,
+};
+FORMAT_DEFINE(qcow_format);
+

Added: user/marcel/libvdsk/libvdsk/vhd.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/marcel/libvdsk/libvdsk/vhd.c	Wed Nov 26 00:58:36 2014	(r275095)
@@ -0,0 +1,100 @@
+/*-
+ * Copyright (c) 2014 Marcel Moolenaar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/disk.h>
+#include <sys/endian.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <vdsk.h>
+
+#include "vdsk_int.h"
+
+static int
+vhd_probe(struct vdsk *vdsk __unused)
+{
+
+	return (ENOSYS);
+}
+
+static int
+vhd_open(struct vdsk *vdsk __unused)
+{
+
+	return (ENOSYS);
+}
+
+static int
+vhd_close(struct vdsk *vdsk __unused)
+{
+
+	return (ENOSYS);
+}
+
+static int
+vhd_read(struct vdsk *vdsk __unused, const struct iovec *iov __unused,
+    int iovcnt __unused, off_t offset __unused)
+{
+
+	return (ENOSYS);
+}
+
+static int
+vhd_write(struct vdsk *vdsk __unused, const struct iovec *iov __unused,
+    int iovcnt __unused, off_t offset __unused)
+{
+
+	return (ENOSYS);
+}
+
+static int
+vhd_flush(struct vdsk *vdsk __unused)
+{
+
+	return (ENOSYS);
+}
+
+static struct vdsk_format vhd_format = {
+	.name = "vhd",
+	.description = "Virtual Hard Disk",
+	.flags = VDSKFMT_HAS_HEADER,
+	.probe = vhd_probe,
+	.open = vhd_open,
+	.close = vhd_close,
+	.read = vhd_read,
+	.write = vhd_write,
+	.flush = vhd_flush,
+};
+FORMAT_DEFINE(vhd_format);
+

Added: user/marcel/libvdsk/libvdsk/vmdk.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/marcel/libvdsk/libvdsk/vmdk.c	Wed Nov 26 00:58:36 2014	(r275095)
@@ -0,0 +1,100 @@
+/*-
+ * Copyright (c) 2014 Marcel Moolenaar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/disk.h>
+#include <sys/endian.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <vdsk.h>
+
+#include "vdsk_int.h"
+
+static int
+vmdk_probe(struct vdsk *vdsk __unused)
+{
+
+	return (ENOSYS);
+}
+
+static int
+vmdk_open(struct vdsk *vdsk __unused)
+{
+
+	return (ENOSYS);
+}
+
+static int
+vmdk_close(struct vdsk *vdsk __unused)
+{
+
+	return (ENOSYS);
+}
+
+static int
+vmdk_read(struct vdsk *vdsk __unused, const struct iovec *iov __unused,
+    int iovcnt __unused, off_t offset __unused)
+{
+
+	return (ENOSYS);
+}
+
+static int
+vmdk_write(struct vdsk *vdsk __unused, const struct iovec *iov __unused,
+    int iovcnt __unused, off_t offset __unused)
+{
+
+	return (ENOSYS);
+}
+
+static int
+vmdk_flush(struct vdsk *vdsk __unused)
+{
+
+	return (ENOSYS);
+}
+
+static struct vdsk_format vmdk_format = {
+	.name = "vmdk",
+	.description = "Virtual Machine Disk",
+	.flags = VDSKFMT_HAS_HEADER,
+	.probe = vmdk_probe,
+	.open = vmdk_open,
+	.close = vmdk_close,
+	.read = vmdk_read,
+	.write = vmdk_write,
+	.flush = vmdk_flush,
+};
+FORMAT_DEFINE(vmdk_format);
+



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201411260058.sAQ0wbPU095009>