qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] using partition images
@ 2006-05-08  3:53 Jim C. Brown
  2006-05-08  4:49 ` Jim C. Brown
  0 siblings, 1 reply; 11+ messages in thread
From: Jim C. Brown @ 2006-05-08  3:53 UTC (permalink / raw)
  To: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 715 bytes --]

This is a preliminary patch that adds support for using a single partition
image as a hard disk image.

Syntax is: qemu -hdX part:file

E.g. qemu -hda part:/dev/hda1

qemu will see a hard disk with a single partition on it. Reading appears to work
ok, I haven't tested writing yet.

Writing to the MBR is allowed, but this is a bad idea as changes to the MBR
will be lost the moment qemu shuts down.

Known Issues:

booting is not supported - this will require passing a separate bootsector.
system id of the partition is always W95 FAT32 (LBA).
having multiple partition images in a single hard disk is not supported.

-- 
Infinite complexity begets infinite beauty.
Infinite precision begets infinite perfection.

[-- Attachment #2: patch --]
[-- Type: text/plain, Size: 8677 bytes --]

--- vl.h	Sun May  7 23:24:35 2006
+++ vl.h	Sun May  7 23:24:47 2006
@@ -477,6 +477,7 @@
 extern BlockDriver bdrv_bochs;
 extern BlockDriver bdrv_vpc;
 extern BlockDriver bdrv_vvfat;
+extern BlockDriver bdrv_part_raw;
 
 void bdrv_init(void);
 BlockDriver *bdrv_find_format(const char *format_name);
--- Makefile.target	Sun May  7 23:25:52 2006
+++ Makefile.target	Sun May  7 23:26:04 2006
@@ -273,7 +273,7 @@
 
 # must use static linking to avoid leaving stuff in virtual address space
 VL_OBJS=vl.o osdep.o block.o readline.o monitor.o pci.o console.o loader.o
-VL_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o block-dmg.o block-bochs.o block-vpc.o block-vvfat.o
+VL_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o block-dmg.o block-bochs.o block-vpc.o block-vvfat.o block-part-raw.o
 ifdef CONFIG_WIN32
 VL_OBJS+=tap-win32.o
 endif
--- block.c	Sun May  7 23:22:40 2006
+++ block.c	Sun May  7 23:22:25 2006
@@ -794,4 +794,5 @@
     bdrv_register(&bdrv_bochs);
     bdrv_register(&bdrv_vpc);
     bdrv_register(&bdrv_vvfat);
+    bdrv_register(&bdrv_part_raw);
 }
--- Makefile	Sun May  7 23:38:56 2006
+++ Makefile	Sun May  7 23:16:20 2006
@@ -22,7 +22,7 @@
 	$(MAKE) -C $$d $@ || exit 1 ; \
         done
 
-qemu-img$(EXESUF): qemu-img.c block.c block-cow.c block-qcow.c aes.c block-vmdk.c block-cloop.c block-dmg.c block-bochs.c block-vpc.c block-vvfat.c
+qemu-img$(EXESUF): qemu-img.c block.c block-cow.c block-qcow.c aes.c block-vmdk.c block-cloop.c block-dmg.c block-bochs.c block-vpc.c block-vvfat.c block-part-raw.c
 	$(CC) -DQEMU_TOOL $(CFLAGS) $(LDFLAGS) $(DEFINES) -o $@ $^ -lz $(LIBS)
 
 dyngen$(EXESUF): dyngen.c
--- /dev/null	Wed Apr 19 17:19:14 2006
+++ block-part-raw.c	Sun May  7 23:21:52 2006
@@ -0,0 +1,249 @@
+/*
+ * Block driver to use partition images instead of whole hard disk images
+ * 
+ * Copyright (c) 2006 Jim Brown
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include "vl.h"
+#include "block_int.h"
+
+#ifdef __sun__
+#include <sys/dkio.h>
+#endif
+
+typedef struct BDRVPartRawState {
+    char mbr_data[63*512];
+    int fd;
+} BDRVPartRawState;
+
+static int part_raw_probe(const uint8_t *buf, int buf_size, const char *filename)
+{
+    if (strstart(filename, "part:", NULL))
+        return 100;
+    return 0;
+}
+
+static int part_raw_open(BlockDriverState *bs, const char *filename)
+{
+    BDRVPartRawState *s = bs->opaque;
+    int fd;
+    int64_t size;
+#ifdef _BSD
+    struct stat sb;
+#endif
+#ifdef __sun__
+    struct dk_minfo minfo;
+    int rv;
+#endif
+    int head, cylinder, sector;
+
+    fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
+    if (fd < 0) {
+        fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
+        if (fd < 0)
+            return -1;
+        bs->read_only = 1;
+    }
+#ifdef _BSD
+    if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
+#ifdef DIOCGMEDIASIZE
+	if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
+#endif
+#ifdef CONFIG_COCOA
+        size = LONG_LONG_MAX;
+#else
+        size = lseek(fd, 0LL, SEEK_END);
+#endif
+    } else
+#endif
+#ifdef __sun__
+    /*
+     * use the DKIOCGMEDIAINFO ioctl to read the size.
+     */
+    rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
+    if ( rv != -1 ) {
+        size = minfo.dki_lbsize * minfo.dki_capacity;
+    } else /* there are reports that lseek on some devices
+              fails, but irc discussion said that contingency
+              on contingency was overkill */
+#endif
+    {
+        size = lseek(fd, 0, SEEK_END);
+    }
+    bs->total_sectors = (size / 512) + 63;
+    s->fd = fd;
+
+    /* set up c/h/s */
+    size = size+(63*512);
+    cylinder = size/(63*16);
+    /* FIXME */
+    cylinder = cylinder + 1; /* add a cylinder just in case partition extends beyond the edge of the last cylinder/head/track */
+    head = 16;
+    sector = 63;
+    /* some bit twiddling here */
+    sector = (((cylinder >> 8) & 3) << 6) + sector;
+
+    /* set up fake MBR */
+    memset(s->mbr_data, 0, 63*512);
+    /* first partition is bootable */
+    s->mbr_data[446] = 0x80;
+    /* start head */
+    s->mbr_data[447] = 0x01;
+    /* start sector - only first 6 bits */
+    s->mbr_data[448] = 0x01;
+    /* start cylinder - this byte plus 2 bits from mbr_data[447] */
+    s->mbr_data[449] = 0x00;
+    /* system ID */
+    s->mbr_data[450] = 0x0C; /* say we're win98 fat32 */
+    /* ending head */
+    s->mbr_data[451] = head;
+    /* ending sector */
+    s->mbr_data[452] = sector;
+    /* ending cylinder */
+    s->mbr_data[453] = cylinder;
+    /* absolute start sector - 4 bytes/DWORD */
+    s->mbr_data[454] = 0x3F; // 3F = 63
+    /* absolute total number of sectors - 4 bytes/DWORD */
+    *((uint32_t*)(s->mbr_data+458)) = cpu_to_le32(bs->total_sectors - 63);
+    /* leave the other partitions blank - we only support the first one */
+
+    /* set the MBR sector signature */
+    s->mbr_data[510] = 0x55;
+    s->mbr_data[511] = 0xAA;
+
+    return 0;
+}
+
+static int part_raw_read(BlockDriverState *bs, int64_t sector_num, 
+                    uint8_t *buf, int nb_sectors)
+{
+    BDRVPartRawState *s = bs->opaque;
+    int ret,split;
+
+    if (sector_num >= 63)
+    {
+    
+    lseek(s->fd, (sector_num - 63) * 512, SEEK_SET);
+    ret = read(s->fd, buf, nb_sectors * 512);
+    if (ret != nb_sectors * 512) 
+        return -1;
+    return 0;
+
+    }
+    else
+    {
+
+    if ((nb_sectors + sector_num) > 63)
+    {
+    	/* ah hell - we have to do both the fake part and the real part */
+
+	split = nb_sectors + sector_num - 63;
+	ret = part_raw_read(bs, 63, &buf[(nb_sectors-split)*512], split * 512);
+	if (ret != split * 512)
+	    return -1;
+
+	/* this will always return 0 */
+	ret = part_raw_read(bs, sector_num, buf, (nb_sectors - split) * 512);
+	return 0;
+    }
+    else
+    {
+    	memcpy(buf, &(s->mbr_data[sector_num*512]), nb_sectors*512);
+	return 0;
+    }
+
+    }
+}
+
+static int part_raw_write(BlockDriverState *bs, int64_t sector_num, 
+                     const uint8_t *buf, int nb_sectors)
+{
+    BDRVPartRawState *s = bs->opaque;
+    int ret, split;
+
+    if (sector_num >= 63)
+    {
+    
+    lseek(s->fd, (sector_num - 63) * 512, SEEK_SET);
+    ret = write(s->fd, buf, nb_sectors * 512);
+    if (ret != nb_sectors * 512) 
+        return -1;
+    return 0;
+
+    }
+    else
+    {
+
+    if ((nb_sectors + sector_num) > 63)
+    {
+    	/* ah hell - we have to do both the fake part and the real part */
+
+	split = nb_sectors + sector_num - 63;
+	ret = part_raw_write(bs, 63, &buf[(nb_sectors-split)*512], split * 512);
+	if (ret != split * 512)
+	    return -1;
+
+	/* this will always return 0 */
+	ret = part_raw_write(bs, sector_num, buf, (nb_sectors - split) * 512);
+	return 0;
+    }
+    else
+    {
+    	memcpy(&(s->mbr_data[sector_num*512]), buf, nb_sectors*512);
+	return 0;
+    }
+
+    }
+}
+
+static void part_raw_close(BlockDriverState *bs)
+{
+    BDRVPartRawState *s = bs->opaque;
+    close(s->fd);
+}
+
+static int part_raw_create(const char *filename, int64_t total_size,
+                      const char *backing_file, int flags)
+{
+    int fd;
+
+    if (flags || backing_file)
+        return -ENOTSUP;
+
+    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 
+              0644);
+    if (fd < 0)
+        return -EIO;
+    ftruncate(fd, total_size * 512);
+    close(fd);
+    return 0;
+}
+
+BlockDriver bdrv_part_raw = {
+    "part_raw",
+    sizeof(BDRVPartRawState),
+    part_raw_probe,
+    part_raw_open,
+    part_raw_read,
+    part_raw_write,
+    part_raw_close,
+    part_raw_create,
+};
+

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Qemu-devel] using partition images
  2006-05-08  3:53 [Qemu-devel] using partition images Jim C. Brown
@ 2006-05-08  4:49 ` Jim C. Brown
  2006-05-08 10:20   ` Fabrice Bellard
  0 siblings, 1 reply; 11+ messages in thread
From: Jim C. Brown @ 2006-05-08  4:49 UTC (permalink / raw)
  To: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 615 bytes --]

On Sun, May 07, 2006 at 11:53:46PM -0400, Jim C. Brown wrote:
> Known Issues:
> 
> booting is not supported - this will require passing a separate bootsector.

I stand corrected. New patch that adds support for booting partition images.
Apply this on top of the first one.

Also attached is the required bootsector.h (this contains the actual code of
the bootsector).

Bootsector code for the MBR was taken from http://www.cpqlinux.com/mbr.html.

Also attached is a tarball of the sources I used to create bootsector.h

-- 
Infinite complexity begets infinite beauty.
Infinite precision begets infinite perfection.

[-- Attachment #2: block.patch --]
[-- Type: text/plain, Size: 891 bytes --]

--- ../qemu.cvs/block-part-raw.c	Sun May  7 23:54:03 2006
+++ block-part-raw.c	Mon May  8 00:40:32 2006
@@ -23,6 +23,7 @@
  */
 #include "vl.h"
 #include "block_int.h"
+#include "bootsector.h"
 
 #ifdef __sun__
 #include <sys/dkio.h>
@@ -40,7 +41,7 @@
     return 0;
 }
 
-static int part_raw_open(BlockDriverState *bs, const char *filename)
+static int part_raw_open(BlockDriverState *bs, const char *nfilename)
 {
     BDRVPartRawState *s = bs->opaque;
     int fd;
@@ -53,6 +54,7 @@
     int rv;
 #endif
     int head, cylinder, sector;
+    const char * filename = &(nfilename[5]);
 
     fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
     if (fd < 0) {
@@ -102,6 +104,7 @@
 
     /* set up fake MBR */
     memset(s->mbr_data, 0, 63*512);
+    memcpy(s->mbr_data, mbr_boot_sector, 512);
     /* first partition is bootable */
     s->mbr_data[446] = 0x80;
     /* start head */

[-- Attachment #3: bootsector.h --]
[-- Type: text/plain, Size: 2781 bytes --]

const char mbr_boot_sector[512] = {
0xfa,
0x33,
0xc0,
0x8e,
0xd0,
0xbc,
0x0,
0x7c,
0x8b,
0xf4,
0x50,
0x7,
0x50,
0x1f,
0xfb,
0xfc,
0xbf,
0x0,
0x6,
0xb9,
0x0,
0x1,
0xf2,
0xa5,
0xea,
0x1d,
0x6,
0x0,
0x0,
0xbe,
0xbe,
0x7,
0xb3,
0x4,
0x80,
0x3c,
0x80,
0x74,
0xe,
0x80,
0x3c,
0x0,
0x75,
0x1c,
0x83,
0xc6,
0x10,
0xfe,
0xcb,
0x75,
0xef,
0xcd,
0x18,
0x8b,
0x14,
0x8b,
0x4c,
0x2,
0x8b,
0xee,
0x83,
0xc6,
0x10,
0xfe,
0xcb,
0x74,
0x1a,
0x80,
0x3c,
0x0,
0x74,
0xf4,
0xbe,
0x8b,
0x6,
0xac,
0x3c,
0x0,
0x74,
0xb,
0x56,
0xbb,
0x7,
0x0,
0xb4,
0xe,
0xcd,
0x10,
0x5e,
0xeb,
0xf0,
0xeb,
0xfe,
0xbf,
0x5,
0x0,
0xbb,
0x0,
0x7c,
0xb8,
0x1,
0x2,
0x57,
0xcd,
0x13,
0x5f,
0x73,
0xc,
0x33,
0xc0,
0xcd,
0x13,
0x4f,
0x75,
0xed,
0xbe,
0xa3,
0x6,
0xeb,
0xd3,
0xbe,
0xc2,
0x6,
0xbf,
0xfe,
0x7d,
0x81,
0x3d,
0x55,
0xaa,
0x75,
0xc7,
0x8b,
0xf5,
0xea,
0x0,
0x7c,
0x0,
0x0,
0x49,
0x6e,
0x76,
0x61,
0x6c,
0x69,
0x64,
0x20,
0x70,
0x61,
0x72,
0x74,
0x69,
0x74,
0x69,
0x6f,
0x6e,
0x20,
0x74,
0x61,
0x62,
0x6c,
0x65,
0x0,
0x45,
0x72,
0x72,
0x6f,
0x72,
0x20,
0x6c,
0x6f,
0x61,
0x64,
0x69,
0x6e,
0x67,
0x20,
0x6f,
0x70,
0x65,
0x72,
0x61,
0x74,
0x69,
0x6e,
0x67,
0x20,
0x73,
0x79,
0x73,
0x74,
0x65,
0x6d,
0x0,
0x4d,
0x69,
0x73,
0x73,
0x69,
0x6e,
0x67,
0x20,
0x6f,
0x70,
0x65,
0x72,
0x61,
0x74,
0x69,
0x6e,
0x67,
0x20,
0x73,
0x79,
0x73,
0x74,
0x65,
0x6d,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0};

[-- Attachment #4: bootsector.tgz --]
[-- Type: application/x-tar-gz, Size: 3534 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Qemu-devel] using partition images
  2006-05-08  4:49 ` Jim C. Brown
@ 2006-05-08 10:20   ` Fabrice Bellard
  2006-05-08 12:26     ` Jim C. Brown
  0 siblings, 1 reply; 11+ messages in thread
From: Fabrice Bellard @ 2006-05-08 10:20 UTC (permalink / raw)
  To: qemu-devel

A few ideas:

- Use an external file 'bootsect.bin' as it is done for linux_boot.bin.
- Provide the source code of the boot sector.
- Instead of copying the raw block driver, use the block driver recursively.

Fabrice.

Jim C. Brown wrote:
> On Sun, May 07, 2006 at 11:53:46PM -0400, Jim C. Brown wrote:
> 
>>Known Issues:
>>
>>booting is not supported - this will require passing a separate bootsector.
> 
> 
> I stand corrected. New patch that adds support for booting partition images.
> Apply this on top of the first one.
> 
> Also attached is the required bootsector.h (this contains the actual code of
> the bootsector).
> 
> Bootsector code for the MBR was taken from http://www.cpqlinux.com/mbr.html.
> 
> Also attached is a tarball of the sources I used to create bootsector.h

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Qemu-devel] using partition images
  2006-05-08 10:20   ` Fabrice Bellard
@ 2006-05-08 12:26     ` Jim C. Brown
  2006-05-08 12:32       ` Johannes Schindelin
                         ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Jim C. Brown @ 2006-05-08 12:26 UTC (permalink / raw)
  To: Fabrice Bellard; +Cc: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 1137 bytes --]

On Mon, May 08, 2006 at 12:20:28PM +0200, Fabrice Bellard wrote:
> A few ideas:
> 
> - Use an external file 'bootsect.bin' as it is done for linux_boot.bin.

Done. I've decided to use the name bootmbr.bin because I think that name
describes its function more accurately.

> - Provide the source code of the boot sector.

After realizing that the original bootsector was actually a dump from an MSDOS
disk (and thus probably closed source) I decided to use BOOTNORM.ASM

BOOTNORM.ASM is from the FreeDISK FDISK (available http://www.23cc.com/free-fdisk and http://ffdisk.webaps.de/fdisk121.zip) and is GPL.

> - Instead of copying the raw block driver, use the block driver recursively.

I'll work on this tonight. I've been thinking about doing this, since it would
allow one to use any qemu-supported disk image format as a partition image.

I can't think of any disk format that's heavily used in qemu that is normally
used for partition images except for raw. OTOH it might be interesting to have
qcow partition images.

> 
> Fabrice.
> 

-- 
Infinite complexity begets infinite beauty.
Infinite precision begets infinite perfection.

[-- Attachment #2: block-part-raw.c --]
[-- Type: text/plain, Size: 7063 bytes --]

/*
 * Block driver to use partition images instead of whole hard disk images
 * 
 * Copyright (c) 2007 Jim Brown
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "vl.h"
#include "block_int.h"

#ifdef __sun__
#include <sys/dkio.h>
#endif

typedef struct BDRVPartRawState {
    char mbr_data[63*512];
    int fd;
} BDRVPartRawState;

static int part_raw_probe(const uint8_t *buf, int buf_size, const char *filename)
{
    if (strstart(filename, "part:", NULL))
        return 100;
    return 0;
}

static int part_raw_open(BlockDriverState *bs, const char *nfilename)
{
    BDRVPartRawState *s = bs->opaque;
    int fd, boot_fd;
    int64_t size;
#ifdef _BSD
    struct stat sb;
#endif
#ifdef __sun__
    struct dk_minfo minfo;
    int rv;
#endif
    int head, cylinder, sector;
    const char * filename = &(nfilename[5]);

    fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
    if (fd < 0) {
        fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
        if (fd < 0)
            return -1;
        bs->read_only = 1;
    }
#ifdef _BSD
    if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
#ifdef DIOCGMEDIASIZE
	if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
#endif
#ifdef CONFIG_COCOA
        size = LONG_LONG_MAX;
#else
        size = lseek(fd, 0LL, SEEK_END);
#endif
    } else
#endif
#ifdef __sun__
    /*
     * use the DKIOCGMEDIAINFO ioctl to read the size.
     */
    rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
    if ( rv != -1 ) {
        size = minfo.dki_lbsize * minfo.dki_capacity;
    } else /* there are reports that lseek on some devices
              fails, but irc discussion said that contingency
              on contingency was overkill */
#endif
    {
        size = lseek(fd, 0, SEEK_END);
    }
    bs->total_sectors = (size / 512) + 63;
    s->fd = fd;

    /* set up c/h/s */
    size = size+(63*512);
    cylinder = size/(63*16);
    /* FIXME */
    cylinder = cylinder + 1; /* add a cylinder just in case partition extends beyond the edge of the last cylinder/head/track */
    head = 16;
    sector = 63;
    /* some bit twiddling here */
    sector = (((cylinder >> 8) & 3) << 6) + sector;

    /* set up fake MBR */
    memset(s->mbr_data, 0, 63*512);
    boot_fd = open("bootmbr.bin", O_RDONLY);
    if (boot_fd == -1)
    {
    printf("Warning: failed to open bootsector.bin - MBR will not be bootbale\n");
    }
    else
    {
    	if (read(boot_fd, s->mbr_data, 512) == -1)
	{
    printf("Warning: failed to read bootsector.bin - MBR will not be bootbale\n");
	}
    	close(boot_fd);
    }
    /* first partition is bootable */
    s->mbr_data[446] = 0x80;
    /* start head */
    s->mbr_data[447] = 0x01;
    /* start sector - only first 6 bits */
    s->mbr_data[448] = 0x01;
    /* start cylinder - this byte plus 2 bits from mbr_data[447] */
    s->mbr_data[449] = 0x00;
    /* system ID */
    s->mbr_data[450] = 0x0C; /* say we're win98 fat32 */
    /* ending head */
    s->mbr_data[451] = head;
    /* ending sector */
    s->mbr_data[452] = sector;
    /* ending cylinder */
    s->mbr_data[453] = cylinder;
    /* absolute start sector - 4 bytes/DWORD */
    s->mbr_data[454] = 0x3F; // 3F = 63
    /* absolute total number of sectors - 4 bytes/DWORD */
    *((uint32_t*)(s->mbr_data+458)) = cpu_to_le32(bs->total_sectors - 63);
    /* leave the other partitions blank - we only support the first one */

    /* set the MBR sector signature */
    s->mbr_data[510] = 0x55;
    s->mbr_data[511] = 0xAA;

    return 0;
}

static int part_raw_read(BlockDriverState *bs, int64_t sector_num, 
                    uint8_t *buf, int nb_sectors)
{
    BDRVPartRawState *s = bs->opaque;
    int ret,split;

    if (sector_num >= 63)
    {
    
    lseek(s->fd, (sector_num - 63) * 512, SEEK_SET);
    ret = read(s->fd, buf, nb_sectors * 512);
    if (ret != nb_sectors * 512) 
        return -1;
    return 0;

    }
    else
    {

    if ((nb_sectors + sector_num) > 63)
    {
    	/* ah hell - we have to do both the fake part and the real part */

	split = nb_sectors + sector_num - 63;
	ret = part_raw_read(bs, 63, &buf[(nb_sectors-split)*512], split * 512);
	if (ret != split * 512)
	    return -1;

	/* this will always return 0 */
	ret = part_raw_read(bs, sector_num, buf, (nb_sectors - split) * 512);
	return 0;
    }
    else
    {
    	memcpy(buf, &(s->mbr_data[sector_num*512]), nb_sectors*512);
	return 0;
    }

    }
}

static int part_raw_write(BlockDriverState *bs, int64_t sector_num, 
                     const uint8_t *buf, int nb_sectors)
{
    BDRVPartRawState *s = bs->opaque;
    int ret, split;

    if (sector_num >= 63)
    {
    
    lseek(s->fd, (sector_num - 63) * 512, SEEK_SET);
    ret = write(s->fd, buf, nb_sectors * 512);
    if (ret != nb_sectors * 512) 
        return -1;
    return 0;

    }
    else
    {

    if ((nb_sectors + sector_num) > 63)
    {
    	/* ah hell - we have to do both the fake part and the real part */

	split = nb_sectors + sector_num - 63;
	ret = part_raw_write(bs, 63, &buf[(nb_sectors-split)*512], split * 512);
	if (ret != split * 512)
	    return -1;

	/* this will always return 0 */
	ret = part_raw_write(bs, sector_num, buf, (nb_sectors - split) * 512);
	return 0;
    }
    else
    {
    	memcpy(&(s->mbr_data[sector_num*512]), buf, nb_sectors*512);
	return 0;
    }

    }
}

static void part_raw_close(BlockDriverState *bs)
{
    BDRVPartRawState *s = bs->opaque;
    close(s->fd);
}

static int part_raw_create(const char *filename, int64_t total_size,
                      const char *backing_file, int flags)
{
    int fd;

    if (flags || backing_file)
        return -ENOTSUP;

    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 
              0644);
    if (fd < 0)
        return -EIO;
    ftruncate(fd, total_size * 512);
    close(fd);
    return 0;
}

BlockDriver bdrv_part_raw = {
    "part_raw",
    sizeof(BDRVPartRawState),
    part_raw_probe,
    part_raw_open,
    part_raw_read,
    part_raw_write,
    part_raw_close,
    part_raw_create,
};


[-- Attachment #3: bootmbr.bin --]
[-- Type: application/octet-stream, Size: 512 bytes --]

[-- Attachment #4: BOOTNORM.ASM --]
[-- Type: text/plain, Size: 5809 bytes --]

;

; normal DOS boot sector

;





segment _DATA           class=DATA align=2



                 

  global  _bootnormal_code

_bootnormal_code:



;-----------------------------------------------------------------------

;   ENTRY (copied from freedos bootsector)

;

; IN: DL = boot drive

;OUT: DL = boot drive

;

;-----------------------------------------------------------------------



real_start:     cli

                cld

                xor     ax, ax

                mov     ss, ax          ; initialize stack

                mov     ds, ax

                mov     bp, 0x7c00

                lea     sp, [bp-0x20]

                sti

                

                mov     ax, 0x1FE0

                mov     es, ax

                mov     si, bp

                mov     di, bp

                mov     cx, 0x0100

                rep     movsw



                jmp     word 0x1FE0:0x7c00+ cont-real_start



cont:           mov     ds, ax

                mov     ss, ax

                xor     ax,ax

                mov     es,ax



;               call    print

;               db      "FreeDOS MBR...",0





										 ; search for active partition

                lea di, [bp+0x1be] ; start of partition table

test_next_for_active:				

                test byte [di],0x80

                jne  active_partition_found

                add  di,0x10                    ; next table

                cmp  di, 07c00h+0x1fe; scanned beyond end of table ??

                jb  test_next_for_active



;*****************************************************************				

                call print

                db 'no active partition found',0

				

WAIT_FOR_REBOOT:

                jmp $





;*****************************************************************

trouble_reading_drive:

                call print

                db 'read error while reading drive',0

                jmp WAIT_FOR_REBOOT



;*****************************************************************



invalid_partition_code:

                call print

                db 'partition signature != 55AA',0

			

                jmp WAIT_FOR_REBOOT



								

;*****************************************************************



active_partition_found:

;				call print

;				db 'loading active partition',0



                call read_boot_sector                    

				

                jc  trouble_reading_drive

			

                cmp word [es:0x7c00+0x1fe],0xaa55

                jne invalid_partition_code

			

;               call print

;               db '.jump DOS..',0

			

                jmp word 0x0:0x7c00             ; and jump to boot sector code





;*****************************

; read_boot_sector				

;

; IN: DI--> partition info

;OUT:CARRY

;*****************************



read_boot_sector:

                ;  /* check for LBA support */

		mov bx,0x55aa		

		mov ah,0x41

		int 0x13



		jc  StandardBios    ;  if (regs.b.x != 0xaa55 || (regs.flags & 0x01))

		cmp bx,0xaa55       ;    goto StandardBios;

		jne StandardBios



                              ;  /* if DAP cannot be used, don't use LBA */

                              ;  if ((regs.c.x & 1) == 0)

                              ;    goto StandardBios;

 		test cl,1

 		jz StandardBios

 

        jmp short LBABios







;struct _bios_LBA_address_packet            /* Used to access a hard disk via LBA */

;{

;  unsigned char packet_size;    /* size of this packet...set to 16  */

;  unsigned char reserved_1;     /* set to 0...unused                */

;  unsigned char number_of_blocks;       /* 0 < number_of_blocks < 128       */

;  unsigned char reserved_2;     /* set to 0...unused                */

;  UBYTE far *buffer_address;    /* addr of transfer buffer          */

;  unsigned long block_address;  /* LBA address                      */

;  unsigned long block_address_high;     /* high bytes of LBA addr...unused  */

;};



_bios_LBA_address_packet:

	db 0x10

	db 0

	db 4				; read four sectors - why not

	db 0

	dw 0x7c00			; fixed boot address for DOS sector

	dw 0x0000	

_bios_LBA_low  dw 0

_bios_LBA_high dw 0

	dw 0,0





LBABios:

						; copy start address of partition to DAP

	mov ax,[di+8]

	mov [0x7c00+ (_bios_LBA_low-real_start)],ax

	mov ax,[di+8+2]

	mov [0x7c00+ (_bios_LBA_high-real_start)],ax



    mov ax,0x4200		;  regs.a.x = LBA_READ;

    mov si,0x7c00+ (_bios_LBA_address_packet-real_start); regs.si = FP_OFF(&dap);



	int 0x13

	ret



;*****************************************************************

; read disk, using standard BIOS

;

StandardBios:

;	call print

;	db 'standard BIOS',0  





    mov ax,0x0204			;  regs.a.x = 0x0201;

    mov bx,0x7c00			;  regs.b.x = FP_OFF(buffer);

	mov cx,[di+2]			;      regs.c.x =

          					; ((chs.Cylinder & 0xff) << 8) + ((chs.Cylinder & 0x300) >> 2) +

          					; chs.Sector;

          					; that was easy ;-)

    mov dh,[di+1]			;  regs.d.b.h = chs.Head;

    						;  regs.es = FP_SEG(buffer);

	int 0x13

	ret	

	





;****** PRINT

; prints text after call to this function.



print_1char:        

                xor   bx, bx                   ; video page 0

                mov   ah, 0x0E                 ; else print it

                int   0x10                     ; via TTY mode

print:          pop   si                       ; this is the first character

print1:         lodsb                          ; get token

                push  si                       ; stack up potential return address

                cmp   al, 0                    ; end of string?

                jne   print_1char              ; until done

                ret                            ; and jump to it







		times	0x1fe-$+$$ db 0

		db 0x55,0xaa

		


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Qemu-devel] using partition images
  2006-05-08 12:26     ` Jim C. Brown
@ 2006-05-08 12:32       ` Johannes Schindelin
  2006-05-08 13:11       ` Paul Brook
  2006-05-08 14:49       ` Jim C. Brown
  2 siblings, 0 replies; 11+ messages in thread
From: Johannes Schindelin @ 2006-05-08 12:32 UTC (permalink / raw)
  To: qemu-devel

Hi,

On Mon, 8 May 2006, Jim C. Brown wrote:

> I can't think of any disk format that's heavily used in qemu that is 
> normally used for partition images except for raw. OTOH it might be 
> interesting to have qcow partition images.

Well, you might argue it is not heavily used, but there is a disk mode in 
VVFAT, which emulates a floppy disk (an attempt to force the guest system 
to synchronize often). It contains it's own MBR emulation, of course, but 
there is no reason to keep it, once you have your scheme running.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Qemu-devel] using partition images
  2006-05-08 12:26     ` Jim C. Brown
  2006-05-08 12:32       ` Johannes Schindelin
@ 2006-05-08 13:11       ` Paul Brook
  2006-05-08 14:19         ` Jim C. Brown
  2006-05-08 14:49       ` Jim C. Brown
  2 siblings, 1 reply; 11+ messages in thread
From: Paul Brook @ 2006-05-08 13:11 UTC (permalink / raw)
  To: qemu-devel

> > - Instead of copying the raw block driver, use the block driver
> > recursively.
>
> I'll work on this tonight. I've been thinking about doing this, since it
> would allow one to use any qemu-supported disk image format as a partition
> image.
>
> I can't think of any disk format that's heavily used in qemu that is
> normally used for partition images except for raw. OTOH it might be
> interesting to have qcow partition images.

If done properly this should also allow use of vmware split image files.

Paul

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Qemu-devel] using partition images
  2006-05-08 13:11       ` Paul Brook
@ 2006-05-08 14:19         ` Jim C. Brown
  2006-05-08 14:28           ` Paul Brook
  0 siblings, 1 reply; 11+ messages in thread
From: Jim C. Brown @ 2006-05-08 14:19 UTC (permalink / raw)
  To: Paul Brook; +Cc: qemu-devel

On Mon, May 08, 2006 at 02:11:36PM +0100, Paul Brook wrote:
> > I'll work on this tonight. I've been thinking about doing this, since it
> > would allow one to use any qemu-supported disk image format as a partition
> > image.
> >
> > I can't think of any disk format that's heavily used in qemu that is
> > normally used for partition images except for raw. OTOH it might be
> > interesting to have qcow partition images.
> 
> If done properly this should also allow use of vmware split image files.
> 

It'd probably be easier to fix the vmdk driver to handle these natively.

If split vmdks are just a series of partition images plus an image of an
MBR/partition table then it may be possible to hack this up via a partition
driver that supported harddisk sharing (using multiple partition images as
part of the same hard disk).

> Paul
> 

-- 
Infinite complexity begets infinite beauty.
Infinite precision begets infinite perfection.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Qemu-devel] using partition images
  2006-05-08 14:19         ` Jim C. Brown
@ 2006-05-08 14:28           ` Paul Brook
  2006-05-08 14:59             ` Jim C. Brown
  0 siblings, 1 reply; 11+ messages in thread
From: Paul Brook @ 2006-05-08 14:28 UTC (permalink / raw)
  To: Jim C. Brown; +Cc: qemu-devel

On Monday 08 May 2006 15:19, Jim C. Brown wrote:
> On Mon, May 08, 2006 at 02:11:36PM +0100, Paul Brook wrote:
> > > I'll work on this tonight. I've been thinking about doing this, since
> > > it would allow one to use any qemu-supported disk image format as a
> > > partition image.
> > >
> > > I can't think of any disk format that's heavily used in qemu that is
> > > normally used for partition images except for raw. OTOH it might be
> > > interesting to have qcow partition images.
> >
> > If done properly this should also allow use of vmware split image files.
>
> It'd probably be easier to fix the vmdk driver to handle these natively.
>
> If split vmdks are just a series of partition images plus an image of an
> MBR/partition table then it may be possible to hack this up via a partition
> driver that supported harddisk sharing (using multiple partition images as
> part of the same hard disk).

I think you should be aiming for a generic composite device block driver.
Then write a fake MBR block device (or whatever you want to call it).

To use a single partition you create a composite device consisting of the fake 
mbr and the raw partition.

A vmware split image file is just a composite of several raw images with a 
funny config file.

Paul

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Qemu-devel] using partition images
  2006-05-08 12:26     ` Jim C. Brown
  2006-05-08 12:32       ` Johannes Schindelin
  2006-05-08 13:11       ` Paul Brook
@ 2006-05-08 14:49       ` Jim C. Brown
  2 siblings, 0 replies; 11+ messages in thread
From: Jim C. Brown @ 2006-05-08 14:49 UTC (permalink / raw)
  To: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 774 bytes --]

On Mon, May 08, 2006 at 08:26:20AM -0400, Jim C. Brown wrote:
> > - Instead of copying the raw block driver, use the block driver recursively.
> 
> I'll work on this tonight. I've been thinking about doing this, since it would
> allow one to use any qemu-supported disk image format as a partition image.
> 
> > 
> > Fabrice.
> > 
> 

New patch here. Apply directly to qemu cvs (i.e. don't apply any of the older
patches).

I've only tested raw partition images and the vvfat driver (in floppy disk mode
of course) with this, but it should work with any format.

("-hda part:fat:floppy:/dir" is what I used)

Now to hack up a fake bootsector so we can boot vvfat partitions. ;)

-- 
Infinite complexity begets infinite beauty.
Infinite precision begets infinite perfection.

[-- Attachment #2: patch --]
[-- Type: text/plain, Size: 7996 bytes --]

--- vl.h	Sun May  7 23:24:35 2006
+++ vl.h	Sun May  7 23:24:47 2006
@@ -477,6 +477,7 @@
 extern BlockDriver bdrv_bochs;
 extern BlockDriver bdrv_vpc;
 extern BlockDriver bdrv_vvfat;
+extern BlockDriver bdrv_part;
 
 void bdrv_init(void);
 BlockDriver *bdrv_find_format(const char *format_name);
--- Makefile.target	Sun May  7 23:25:52 2006
+++ Makefile.target	Sun May  7 23:26:04 2006
@@ -273,7 +273,7 @@
 
 # must use static linking to avoid leaving stuff in virtual address space
 VL_OBJS=vl.o osdep.o block.o readline.o monitor.o pci.o console.o loader.o
-VL_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o block-dmg.o block-bochs.o block-vpc.o block-vvfat.o
+VL_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o block-dmg.o block-bochs.o block-vpc.o block-vvfat.o block-part.o
 ifdef CONFIG_WIN32
 VL_OBJS+=tap-win32.o
 endif
--- block.c	Sun May  7 23:22:40 2006
+++ block.c	Sun May  7 23:22:25 2006
@@ -794,4 +794,5 @@
     bdrv_register(&bdrv_bochs);
     bdrv_register(&bdrv_vpc);
     bdrv_register(&bdrv_vvfat);
+    bdrv_register(&bdrv_part);
 }
--- Makefile	Sun May  7 23:38:56 2006
+++ Makefile	Sun May  7 23:16:20 2006
@@ -22,7 +22,7 @@
 	$(MAKE) -C $$d $@ || exit 1 ; \
         done
 
-qemu-img$(EXESUF): qemu-img.c block.c block-cow.c block-qcow.c aes.c block-vmdk.c block-cloop.c block-dmg.c block-bochs.c block-vpc.c block-vvfat.c
+qemu-img$(EXESUF): qemu-img.c block.c block-cow.c block-qcow.c aes.c block-vmdk.c block-cloop.c block-dmg.c block-bochs.c block-vpc.c block-vvfat.c block-part.c
 	$(CC) -DQEMU_TOOL $(CFLAGS) $(LDFLAGS) $(DEFINES) -o $@ $^ -lz $(LIBS)
 
 dyngen$(EXESUF): dyngen.c
--- /dev/null	Wed Apr 19 17:19:14 2006
+++ block-part.c	Mon May  8 10:33:52 2006
@@ -0,0 +1,223 @@
+/*
+ * Block driver to use partition images instead of whole hard disk images
+ * 
+ * Copyright (c) 2006 Jim Brown
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include "vl.h"
+#include "block_int.h"
+
+#ifdef __sun__
+#include <sys/dkio.h>
+#endif
+
+typedef struct BDRVPartState {
+    char mbr_data[63*512];
+    BlockDriverState * slave_bs;
+} BDRVPartState;
+
+static int part_probe(const uint8_t *buf, int buf_size, const char *filename)
+{
+    if (strstart(filename, "part:", NULL))
+        return 100;
+    return 0;
+}
+
+static int part_open(BlockDriverState *bs, const char *nfilename)
+{
+    BDRVPartState *s = bs->opaque;
+    BlockDriverState * slave_bs;
+    int boot_fd;
+    int64_t size;
+    int head, cylinder, sector;
+    const char * filename = &(nfilename[5]);
+
+    slave_bs = qemu_mallocz(sizeof(BlockDriverState));
+    if (bdrv_open2(slave_bs, filename, 0, NULL) != 0)
+    {
+	qemu_free(slave_bs);
+    	return -1;
+    }
+
+    s->slave_bs = slave_bs;
+    bs->read_only = slave_bs->read_only;
+    bs->total_sectors = slave_bs->total_sectors + 63;
+
+    /* set up c/h/s */
+    size = bs->total_sectors*512;
+    cylinder = size/(63*16);
+    /* FIXME */
+    cylinder = cylinder + 1; /* add a cylinder just in case partition extends beyond the edge of the last cylinder/head/track */
+    head = 16;
+    sector = 63;
+    /* some bit twiddling here */
+    sector = (((cylinder >> 8) & 3) << 6) + sector;
+
+    /* set up fake MBR */
+    memset(s->mbr_data, 0, 63*512);
+    boot_fd = open("bootmbr.bin", O_RDONLY);
+    if (boot_fd == -1)
+    {
+    printf("Warning: failed to open bootsector.bin - MBR will not be bootable\n");
+    }
+    else
+    {
+    	if (read(boot_fd, s->mbr_data, 512) == -1)
+	{
+    printf("Warning: failed to read bootsector.bin - MBR will not be bootbale\n");
+	}
+    	close(boot_fd);
+    }
+    /* first partition is bootable */
+    s->mbr_data[446] = 0x80;
+    /* start head */
+    s->mbr_data[447] = 0x01;
+    /* start sector - only first 6 bits */
+    s->mbr_data[448] = 0x01;
+    /* start cylinder - this byte plus 2 bits from mbr_data[447] */
+    s->mbr_data[449] = 0x00;
+    /* system ID */
+    s->mbr_data[450] = 0x0C; /* say we're win98 fat32 */
+    /* ending head */
+    s->mbr_data[451] = head;
+    /* ending sector */
+    s->mbr_data[452] = sector;
+    /* ending cylinder */
+    s->mbr_data[453] = cylinder;
+    /* absolute start sector - 4 bytes/DWORD */
+    s->mbr_data[454] = 0x3F; // 3F = 63
+    /* absolute total number of sectors - 4 bytes/DWORD */
+    *((uint32_t*)(s->mbr_data+458)) = cpu_to_le32(bs->total_sectors - 63);
+    /* leave the other partitions blank - we only support the first one */
+
+    /* set the MBR sector signature */
+    s->mbr_data[510] = 0x55;
+    s->mbr_data[511] = 0xAA;
+
+    return 0;
+}
+
+static int part_read(BlockDriverState *bs, int64_t sector_num, 
+                    uint8_t *buf, int nb_sectors)
+{
+    BDRVPartState *s = bs->opaque;
+    int ret,split;
+
+    if (sector_num >= 63)
+    {
+    
+    ret = bdrv_read(s->slave_bs, sector_num-63, buf, nb_sectors);
+    if (ret != 0) 
+        return -1;
+    return 0;
+
+    }
+    else
+    {
+
+    if ((nb_sectors + sector_num) > 63)
+    {
+    	/* ah hell - we have to do both the fake part and the real part */
+
+	split = nb_sectors + sector_num - 63;
+	ret = part_read(bs, 63, &buf[(nb_sectors-split)*512], split * 512);
+	//if (ret != split * 512)
+	if (ret != 0)
+	    return -1;
+
+	/* this will always return 0 */
+	ret = part_read(bs, sector_num, buf, (nb_sectors - split) * 512);
+	return 0;
+    }
+    else
+    {
+    	memcpy(buf, &(s->mbr_data[sector_num*512]), nb_sectors*512);
+	return 0;
+    }
+
+    }
+}
+
+static int part_write(BlockDriverState *bs, int64_t sector_num, 
+                     const uint8_t *buf, int nb_sectors)
+{
+    BDRVPartState *s = bs->opaque;
+    int ret, split;
+
+    if (sector_num >= 63)
+    {
+    
+    ret = bdrv_write(s->slave_bs, sector_num-63, buf, nb_sectors);
+    if (ret != 0) 
+        return -1;
+    return 0;
+
+    }
+    else
+    {
+
+    if ((nb_sectors + sector_num) > 63)
+    {
+    	/* ah hell - we have to do both the fake part and the real part */
+
+	split = nb_sectors + sector_num - 63;
+	ret = part_write(bs, 63, &buf[(nb_sectors-split)*512], split * 512);
+	if (ret != split * 512)
+	    return -1;
+
+	/* this will always return 0 */
+	ret = part_write(bs, sector_num, buf, (nb_sectors - split) * 512);
+	return 0;
+    }
+    else
+    {
+    	memcpy(&(s->mbr_data[sector_num*512]), buf, nb_sectors*512);
+	return 0;
+    }
+
+    }
+}
+
+static void part_close(BlockDriverState *bs)
+{
+    BDRVPartState *s = bs->opaque;
+    bdrv_close(s->slave_bs);
+    qemu_free(s->slave_bs);
+    s->slave_bs = NULL;
+}
+
+static int part_create(const char *filename, int64_t total_size,
+                      const char *backing_file, int flags)
+{
+    /* what would be the point... just make a raw or qcow */
+    return -ENOTSUP;
+}
+
+BlockDriver bdrv_part = {
+    "part",
+    sizeof(BDRVPartState),
+    part_probe,
+    part_open,
+    part_read,
+    part_write,
+    part_close,
+    part_create,
+};
+

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Qemu-devel] using partition images
  2006-05-08 14:28           ` Paul Brook
@ 2006-05-08 14:59             ` Jim C. Brown
  2006-05-08 15:09               ` Paul Brook
  0 siblings, 1 reply; 11+ messages in thread
From: Jim C. Brown @ 2006-05-08 14:59 UTC (permalink / raw)
  To: Paul Brook; +Cc: qemu-devel

On Mon, May 08, 2006 at 03:28:31PM +0100, Paul Brook wrote:
> > If split vmdks are just a series of partition images plus an image of an
> > MBR/partition table then it may be possible to hack this up via a partition
> > driver that supported harddisk sharing (using multiple partition images as
> > part of the same hard disk).
> 
> I think you should be aiming for a generic composite device block driver.
> Then write a fake MBR block device (or whatever you want to call it).
> 
> To use a single partition you create a composite device consisting of the fake 
> mbr and the raw partition.

I'm not sure if it's worth it to make the fake MBR its own block device.

Since we're going to need configurable options anyways, it might be possible
to specify how the MBR should be handled. E.g.

-hda multipart:mbr:fake:part1:/usr/images/hda1.img:part1sysid:0xc:part2:fat:floppy:/usr/images/tools:part2sysid:0x6

would handle the current faking of the mbr using bootmbr.bin and autogenerated
partition table, while

-hda multipart:mbr:file:/usr/images/vmdk-mbr.vmdk:part1:/usr/images/hda1.vmdk:part2:/usr/images/hda2.vmdk

would allow for having the mbr as a separate file. And even

-hda multipart:mbr:part1:part1:/usr/images/hda1.vmdk:part2:/usr/images/hda2.vmdk

which would specify that hda1.vmdk has the mbr and partition table prepended to it.

The syntax is getting kinda ugly though. Anthony suggested that we use mini-config
files with all the options and just pass those to -hda (e.g. -hda multipart:hda.config)

> 
> A vmware split image file is just a composite of several raw images with a 
> funny config file.

It'd still be nice if the vmware driver had support for using those config files
instead of requiring a user to type this out by hand.

I guess qemu-img could have an option to convert those config files into qemu
multipart/composite harddisk image configs.

> 
> Paul
> 

-- 
Infinite complexity begets infinite beauty.
Infinite precision begets infinite perfection.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Qemu-devel] using partition images
  2006-05-08 14:59             ` Jim C. Brown
@ 2006-05-08 15:09               ` Paul Brook
  0 siblings, 0 replies; 11+ messages in thread
From: Paul Brook @ 2006-05-08 15:09 UTC (permalink / raw)
  To: qemu-devel

> The syntax is getting kinda ugly though. Anthony suggested that we use
> mini-config files with all the options and just pass those to -hda (e.g.
> -hda multipart:hda.config)
>
> > A vmware split image file is just a composite of several raw images with
> > a funny config file.
>
> It'd still be nice if the vmware driver had support for using those config
> files instead of requiring a user to type this out by hand.

If you implement the config files mentioned above then supporting and 
autodetecting both qemu config files and vmware config files (assuming we 
want/need a new config file syntax) should be fairly straightforward. No need 
to modify the vmdk driver.

Paul

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2006-05-08 15:09 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-05-08  3:53 [Qemu-devel] using partition images Jim C. Brown
2006-05-08  4:49 ` Jim C. Brown
2006-05-08 10:20   ` Fabrice Bellard
2006-05-08 12:26     ` Jim C. Brown
2006-05-08 12:32       ` Johannes Schindelin
2006-05-08 13:11       ` Paul Brook
2006-05-08 14:19         ` Jim C. Brown
2006-05-08 14:28           ` Paul Brook
2006-05-08 14:59             ` Jim C. Brown
2006-05-08 15:09               ` Paul Brook
2006-05-08 14:49       ` Jim C. Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).