Commit abdb293f authored by Chrysostomos Nanakos's avatar Chrysostomos Nanakos

Initial commit

Copy source code from Debian blktap-utils package.
parents
# ACLOCAL_AMFLAGS = -I m4
SUBDIRS = lvm
SUBDIRS += $(MAYBE_part)
SUBDIRS += vhd
SUBDIRS += control
SUBDIRS += drivers
SUBDIRS += include
if ENABLE_PART
MAYBE_part = part
endif
Blktap Userspace Tools + Library
================================
Andrew Warfield and Julian Chesterfield
16th June 2006
{firstname.lastname}@cl.cam.ac.uk
The blktap userspace toolkit provides a user-level disk I/O
interface. The blktap mechanism involves a kernel driver that acts
similarly to the existing Xen/Linux blkback driver, and a set of
associated user-level libraries. Using these tools, blktap allows
virtual block devices presented to VMs to be implemented in userspace
and to be backed by raw partitions, files, network, etc.
The key benefit of blktap is that it makes it easy and fast to write
arbitrary block backends, and that these user-level backends actually
perform very well. Specifically:
- Metadata disk formats such as Copy-on-Write, encrypted disks, sparse
formats and other compression features can be easily implemented.
- Accessing file-based images from userspace avoids problems related
to flushing dirty pages which are present in the Linux loopback
driver. (Specifically, doing a large number of writes to an
NFS-backed image don't result in the OOM killer going berserk.)
- Per-disk handler processes enable easier userspace policing of block
resources, and process-granularity QoS techniques (disk scheduling
and related tools) may be trivially applied to block devices.
- It's very easy to take advantage of userspace facilities such as
networking libraries, compression utilities, peer-to-peer
file-sharing systems and so on to build more complex block backends.
- Crashes are contained -- incremental development/debugging is very
fast.
How it works (in one paragraph):
Working in conjunction with the kernel blktap driver, all disk I/O
requests from VMs are passed to the userspace deamon (using a shared
memory interface) through a character device. Each active disk is
mapped to an individual device node, allowing per-disk processes to
implement individual block devices where desired. The userspace
drivers are implemented using asynchronous (Linux libaio),
O_DIRECT-based calls to preserve the unbuffered, batched and
asynchronous request dispatch achieved with the existing blkback
code. We provide a simple, asynchronous virtual disk interface that
makes it quite easy to add new disk implementations.
As of June 2006 the current supported disk formats are:
- Raw Images (both on partitions and in image files)
- File-backed Qcow disks
- Standalone sparse Qcow disks
- Fast shareable RAM disk between VMs (requires some form of cluster-based
filesystem support e.g. OCFS2 in the guest kernel)
- Some VMDK images - your mileage may vary
Raw and QCow images have asynchronous backends and so should perform
fairly well. VMDK is based directly on the qemu vmdk driver, which is
synchronous (a.k.a. slow).
Build and Installation Instructions
===================================
Make to configure the blktap backend driver in your dom0 kernel. It
will cooperate fine with the existing backend driver, so you can
experiment with tap disks without breaking existing VM configs.
To build the tools separately, "make && make install" in
tools/blktap.
Using the Tools
===============
Prepare the image for booting. For qcow files use the qcow utilities
installed earlier. e.g. qcow-create generates a blank standalone image
or a file-backed CoW image. img2qcow takes an existing image or
partition and creates a sparse, standalone qcow-based file.
The userspace disk agent is configured to start automatically via xend
(alternatively you can start it manually => 'blktapctrl')
Customise the VM config file to use the 'tap' handler, followed by the
driver type. e.g. for a raw image such as a file or partition:
disk = ['tap:aio:<FILENAME>,sda1,w']
e.g. for a qcow image:
disk = ['tap:qcow:<FILENAME>,sda1,w']
Mounting images in Dom0 using the blktap driver
===============================================
Tap (and blkback) disks are also mountable in Dom0 without requiring an
active VM to attach. You will need to build a xenlinux Dom0 kernel that
includes the blkfront driver (e.g. the default 'make world' or
'make kernels' build. Simply use the xm command-line tool to activate
the backend disks, and blkfront will generate a virtual block device that
can be accessed in the same way as a loop device or partition:
e.g. for a raw image file <FILENAME> that would normally be mounted using
the loopback driver (such as 'mount -o loop <FILENAME> /mnt/disk'), do the
following:
xm block-attach 0 tap:aio:<FILENAME> /dev/xvda1 w 0
mount /dev/xvda1 /mnt/disk <--- don't use loop driver
In this way, you can use any of the userspace device-type drivers built
with the blktap userspace toolkit to open and mount disks such as qcow
or vmdk images:
xm block-attach 0 tap:qcow:<FILENAME> /dev/xvda1 w 0
mount /dev/xvda1 /mnt/disk
2.0.90
\ No newline at end of file
This diff is collapsed.
m4_include([version.m4])
AC_INIT([blktap], BLKTAP_VERSION)
AM_INIT_AUTOMAKE([foreign])
# AM_SILENT_RULES([yes])
AC_PROG_CC
AC_PROG_LIBTOOL
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_CANONICAL_HOST
AC_SYS_LARGEFILE
AC_CHECK_HEADERS([uuid/uuid.h], [], [Need uuid-dev])
AC_CHECK_HEADERS([libaio.h], [], [Need libaio-dev])
AC_ARG_WITH([libiconv],
[AS_HELP_STRING([--with-libiconv],
[link to libiconv])],
[],
[with_libiconv=check])
AC_CHECK_LIB([aio], [main],
[:],
AC_MSG_ERROR([Need libaio-dev]))
AC_CHECK_LIB([uuid], [main],
[:],
AC_MSG_ERROR([Need uuid-dev]))
AS_IF([test x$with_libiconv != xno],
[AC_CHECK_LIB([iconv], [main],
[AC_SUBST([LIBICONV], ["-liconv"])],
[if test x$with_libiconv == xyes; then
AC_MSG_FAILURE([--with-libiconv given, but test failed])
fi])],
[AC_CHECK_LIB([], [iconv_open],
[:],
AC_MSG_ERROR([Missing iconv in libc]))])
AC_ARG_ENABLE([tests],
[AS_HELP_STRING([--enable-tests],
[build test programs])],
[],
[enable_tests=no])
AM_CONDITIONAL([ENABLE_PART],
[case "${host_os}" in
linux-*) true ;;
*) false ;;
esac])
AM_CONDITIONAL([ENABLE_VHDIO],
[test x$enable_shared = xyes &&
case "${host_os}" in
linux-gnu) true ;;
*) false ;;
esac])
AM_CONDITIONAL([ENABLE_TESTS],
[test x$enable_tests = xyes])
# AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([
Makefile
lvm/Makefile
part/Makefile
vhd/Makefile
vhd/lib/Makefile
vhd/lib/test/Makefile
control/Makefile
drivers/Makefile
include/Makefile
])
AC_OUTPUT
AM_CFLAGS = -Wall
AM_CFLAGS += -Werror
AM_CPPFLAGS = -D_GNU_SOURCE
AM_CPPFLAGS += -DTAPCTL
AM_CPPFLAGS += -I$(top_srcdir)/include
AM_CPPFLAGS += -DTAPDISK_EXEC='"tapdisk"'
AM_CPPFLAGS += -DTAPDISK_EXECDIR='"$(libexecdir)"'
AM_CPPFLAGS += -DTAPDISK_BUILDDIR='"$(top_builddir)/drivers"'
sbin_PROGRAMS = tap-ctl
tap_ctl_LDADD = libblktapctl.la
lib_LTLIBRARIES = libblktapctl.la
libblktapctl_la_SOURCES = tap-ctl-ipc.c
libblktapctl_la_SOURCES += tap-ctl-list.c
libblktapctl_la_SOURCES += tap-ctl-allocate.c
libblktapctl_la_SOURCES += tap-ctl-free.c
libblktapctl_la_SOURCES += tap-ctl-create.c
libblktapctl_la_SOURCES += tap-ctl-destroy.c
libblktapctl_la_SOURCES += tap-ctl-spawn.c
libblktapctl_la_SOURCES += tap-ctl-attach.c
libblktapctl_la_SOURCES += tap-ctl-detach.c
libblktapctl_la_SOURCES += tap-ctl-open.c
libblktapctl_la_SOURCES += tap-ctl-close.c
libblktapctl_la_SOURCES += tap-ctl-pause.c
libblktapctl_la_SOURCES += tap-ctl-unpause.c
libblktapctl_la_SOURCES += tap-ctl-major.c
libblktapctl_la_SOURCES += tap-ctl-check.c
libblktapctl_la_SOURCES += tap-ctl-stats.c
libblktapctl_la_LDFLAGS = -version-info 1:1:1
udev_rulesdir = $(sysconfdir)/udev/rules.d
dist_udev_rules_DATA = blktap.rules
SUBSYSTEM=="misc", KERNEL=="blktap-control", NAME="blktap/control"
SUBSYSTEM=="blktap2", KERNEL=="blktap[0-9]*", NAME="blktap/%k"
SUBSYSTEM=="block", KERNEL=="td[a-z]*", NAME="%k"
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <linux/major.h>
#include "tap-ctl.h"
#include "blktap2.h"
static int
tap_ctl_prepare_directory(const char *dir)
{
int err;
char *ptr, *name, *start;
err = access(dir, W_OK | R_OK);
if (!err)
return 0;
name = strdup(dir);
if (!name)
return ENOMEM;
start = name;
for (;;) {
ptr = strchr(start + 1, '/');
if (ptr)
*ptr = '\0';
err = mkdir(name, 0755);
if (err && errno != EEXIST) {
PERROR("mkdir %s", name);
err = errno;
break;
}
if (!ptr)
break;
else {
*ptr = '/';
start = ptr + 1;
}
}
free(name);
return err;
}
static int
tap_ctl_make_device(const char *devname, const int major,
const int minor, const int perm)
{
int err;
char *copy, *dir;
copy = strdup(devname);
if (!copy)
return ENOMEM;
dir = dirname(copy);
err = tap_ctl_prepare_directory(dir);
free(copy);
if (err)
return err;
if (!access(devname, F_OK))
if (unlink(devname)) {
PERROR("unlink %s", devname);
return errno;
}
err = mknod(devname, perm, makedev(major, minor));
if (err) {
PERROR("mknod %s", devname);
return errno;
}
return 0;
}
static int
tap_ctl_check_environment(void)
{
FILE *f;
int err, minor;
char name[256];
err = tap_ctl_prepare_directory(BLKTAP2_CONTROL_DIR);
if (err)
return err;
if (!access(BLKTAP2_CONTROL_DEVICE, R_OK | W_OK))
return 0;
memset(name, 0, sizeof(name));
f = fopen("/proc/misc", "r");
if (!f) {
EPRINTF("failed to open /proc/misc: %d\n", errno);
return errno;
}
while (fscanf(f, "%d %256s", &minor, name) == 2)
if (!strcmp(name, BLKTAP2_CONTROL_NAME)) {
err = tap_ctl_make_device(BLKTAP2_CONTROL_DEVICE,
MISC_MAJOR,
minor, S_IFCHR | 0600);
goto out;
}
err = ENOSYS;
EPRINTF("didn't find %s in /proc/misc\n", BLKTAP2_CONTROL_NAME);
out:
fclose(f);
return err;
}
static int
tap_ctl_allocate_device(int *minor, char **devname)
{
char *name;
int fd, err;
struct blktap2_handle handle;
*minor = -1;
if (!devname)
return EINVAL;
fd = open(BLKTAP2_CONTROL_DEVICE, O_RDONLY);
if (fd == -1) {
EPRINTF("failed to open control device: %d\n", errno);
return errno;
}
err = ioctl(fd, BLKTAP2_IOCTL_ALLOC_TAP, &handle);
close(fd);
if (err == -1) {
EPRINTF("failed to allocate new device: %d\n", errno);
return errno;
}
err = asprintf(&name, "%s%d", BLKTAP2_RING_DEVICE, handle.minor);
if (err == -1) {
err = ENOMEM;
goto fail;
}
err = tap_ctl_make_device(name, handle.ring,
handle.minor, S_IFCHR | 0600);
free(name);
if (err) {
EPRINTF("creating ring device for %d failed: %d\n",
handle.minor, err);
goto fail;
}
if (*devname)
name = *devname;
else {
err = asprintf(&name, "%s%d",
BLKTAP2_IO_DEVICE, handle.minor);
if (err == -1) {
err = ENOMEM;
goto fail;
}
*devname = name;
}
err = tap_ctl_make_device(name, handle.device,
handle.minor, S_IFBLK | 0600);
if (err) {
EPRINTF("creating IO device for %d failed: %d\n",
handle.minor, err);
goto fail;
}
DBG("new interface: ring: %u, device: %u, minor: %u\n",
handle.ring, handle.device, handle.minor);
*minor = handle.minor;
return 0;
fail:
tap_ctl_free(handle.minor);
return err;
}
int
tap_ctl_allocate(int *minor, char **devname)
{
int err;
*minor = -1;
err = tap_ctl_check_environment();
if (err)
return err;
err = tap_ctl_allocate_device(minor, devname);
if (err)
return err;
return 0;
}
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "tap-ctl.h"
int
tap_ctl_attach(const int id, const int minor)
{
int err;
tapdisk_message_t message;
memset(&message, 0, sizeof(message));
message.type = TAPDISK_MESSAGE_ATTACH;
message.cookie = minor;
err = tap_ctl_connect_send_and_receive(id, &message, NULL);
if (err)
return err;
if (message.type == TAPDISK_MESSAGE_ATTACH_RSP) {
err = message.u.response.error;
if (err)
EPRINTF("attach failed: %d\n", err);
} else {
EPRINTF("got unexpected result '%s' from %d\n",
tapdisk_message_name(message.type), id);
err = EINVAL;
}
return err;
}
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include "tap-ctl.h"
#include "blktap2.h"
int
tap_ctl_check_blktap(const char **msg)
{
FILE *f;
int err = 0, minor;
char name[32];
memset(name, 0, sizeof(name));
f = fopen("/proc/misc", "r");
if (!f) {
*msg = "failed to open /proc/misc";
return -errno;
}
while (fscanf(f, "%d %32s", &minor, name) == 2) {
if (!strcmp(name, BLKTAP2_CONTROL_NAME))
goto out;
}
err = -ENOSYS;
*msg = "blktap kernel module not installed";
out:
fclose(f);
return err;
}
int
tap_ctl_check(const char **msg)
{
int err;
err = tap_ctl_check_blktap(msg);
if (err)
goto out;
err = 0;
*msg = "ok";
out:
return err;
}
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include "tap-ctl.h"
int
tap_ctl_close(const int id, const int minor, const int force,
struct timeval *timeout)
{