qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 1/2] Add an in-memory block device


From: Anthony Liguori
Subject: [Qemu-devel] [PATCH 1/2] Add an in-memory block device
Date: Wed, 05 Dec 2007 16:05:18 -0600
User-agent: Thunderbird 2.0.0.6 (X11/20071022)

This is a generic in-memory block device.  It is needed by the next patch.

Regards,

Anthony Liguori
Index: qemu/Makefile
===================================================================
--- qemu.orig/Makefile  2007-12-05 15:39:09.000000000 -0600
+++ qemu/Makefile       2007-12-05 15:39:39.000000000 -0600
@@ -40,7 +40,7 @@
 BLOCK_OBJS=cutils.o
 BLOCK_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o
 BLOCK_OBJS+=block-dmg.o block-bochs.o block-vpc.o block-vvfat.o
-BLOCK_OBJS+=block-qcow2.o block-parallels.o
+BLOCK_OBJS+=block-qcow2.o block-parallels.o block-mem.o
 
 ######################################################################
 # libqemu_common.a: Target indepedent part of system emulation. The
Index: qemu/block-mem.c
===================================================================
--- /dev/null   1970-01-01 00:00:00.000000000 +0000
+++ qemu/block-mem.c    2007-12-05 15:54:02.000000000 -0600
@@ -0,0 +1,99 @@
+/*
+ * In-memory block driver
+ *
+ * Copyright IBM, Corp. 2007
+ *
+ * Authors:
+ *  Anthony Liguori   <address@hidden>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu-common.h"
+#include "block_int.h"
+
+typedef struct BDRVMemState {
+    void *mem;
+    size_t size;
+} BDRVMemState;
+
+static int mem_probe(const uint8_t *buf, int buf_size, const char *filename)
+{
+    return 0;
+}
+
+static int mem_open(BlockDriverState *bs, const char *filename, int flags)
+{
+    return -1;
+}
+
+int bdrv_mem_open(BlockDriverState *bs, size_t size)
+{
+    BDRVMemState *s;
+
+    bs->read_only = 0;
+    bs->is_temporary = 0;
+    bs->encrypted = 0;
+    pstrcpy(bs->filename, sizeof(bs->filename), "<mem>");
+    bs->drv = &bdrv_mem;
+    bs->total_sectors = ((size + 511) & ~511) / 512;
+
+    bs->opaque = qemu_mallocz(bdrv_mem.instance_size);
+    if (bs->opaque == NULL)
+           return -1;
+
+    s = bs->opaque;
+    s->mem = qemu_mallocz(size);
+    if (s->mem == NULL)
+       return -1;
+    s->size = size;
+
+    return 0;
+}
+
+static int mem_read(BlockDriverState *bs, int64_t sector_num,
+                   uint8_t *buf, int nb_sectors)
+{
+    BDRVMemState *s = bs->opaque;
+    size_t size;
+
+    sector_num = MIN(sector_num, bs->total_sectors - 1);
+    size = MIN(s->size - (sector_num * 512), nb_sectors * 512);
+
+    memcpy(buf, s->mem + (sector_num * 512), size);
+
+    return 0;
+}
+
+static int mem_write(BlockDriverState *bs, int64_t sector_num,
+                    const uint8_t *buf, int nb_sectors)
+{
+    BDRVMemState *s = bs->opaque;
+    size_t size;
+
+    sector_num = MIN(sector_num, bs->total_sectors - 1);
+    size = MIN(s->size - (sector_num * 512), nb_sectors * 512);
+
+    memcpy(s->mem + (sector_num * 512), buf, size);
+
+    return 0;
+}
+
+static void mem_close(BlockDriverState *bs)
+{
+    BDRVMemState *s = bs->opaque;
+
+    qemu_free(s->mem);
+}
+
+BlockDriver bdrv_mem = {
+    "mem",
+    sizeof(BDRVMemState),
+    mem_probe,
+    mem_open,
+    mem_read,
+    mem_write,
+    mem_close,
+};
Index: qemu/block.h
===================================================================
--- qemu.orig/block.h   2007-12-05 15:38:48.000000000 -0600
+++ qemu/block.h        2007-12-05 15:47:36.000000000 -0600
@@ -16,6 +16,7 @@
 extern BlockDriver bdrv_vvfat;
 extern BlockDriver bdrv_qcow2;
 extern BlockDriver bdrv_parallels;
+extern BlockDriver bdrv_mem;
 
 typedef struct BlockDriverInfo {
     /* in bytes, 0 if irrelevant */
@@ -155,4 +156,6 @@
                   const char *base_path,
                   const char *filename);
 
+int bdrv_mem_open(BlockDriverState *bs, size_t size);
+
 #endif

reply via email to

[Prev in Thread] Current Thread [Next in Thread]