From d14f5db85eceea19b46c4b6d379395557c851f48 Mon Sep 17 00:00:00 2001
From: Markus Armbruster <armbru@redhat.com>
Date: Mon, 21 Jun 2010 10:08:30 -0300
Subject: [PATCH 4/4] Make IDE drives defined with -device visible to cmos_init()

RH-Author: Markus Armbruster <armbru@redhat.com>
Message-id: <m339wgbsv5.fsf@blackfin.pond.sub.org>
Patchwork-id: 10027
O-Subject: [RHEL-6 KVM PATCH] Make IDE drives defined with -device visible to
	cmos_init()
Bugzilla: 597147
RH-Acked-by: Jes Sorensen <Jes.Sorensen@redhat.com>
RH-Acked-by: Gerd Hoffmann <kraxel@redhat.com>
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
RH-Acked-by: Juan Quintela <quintela@redhat.com>

A proper fix requires quite a bit of surgery.  This is just a quick,
temporary patch to make things work with libvirt.  It relies on the way
libvirt names drives, namely "drive-ide0-BUS-UNIT".  If you name your
drives differently, they won't get picked up, and your CMOS will be as
wrong as without the patch.  If you give them another bus or unit than
their name indicates, your CMOS will be differently wrong than without
the patch.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
Upstream: I wouldn't dare
BZ 597147
Testing: A quick smoke test, but no actual XP boot, need help!

 hw/pc.c |   17 +++++++++++++++++
 1 files changed, 17 insertions(+), 0 deletions(-)

For comparison, here's the diffstat of the proper fix I'm working on for
upstream:

 hw/ide.h      |   13 ++++---
 hw/ide/isa.c  |    8 ++--
 hw/ide/piix.c |    6 ++-
 hw/ide/qdev.c |    7 ++++
 hw/pc.c       |  100 +++++++++++++++++++++++++++++++++++++--------------------
 hw/pc.h       |    3 +-
 hw/pc_piix.c  |   16 ++++++---
 7 files changed, 101 insertions(+), 52 deletions(-)

Not something I'd want to put into B2 this late in the game.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/pc.c |   17 +++++++++++++++++
 1 files changed, 17 insertions(+), 0 deletions(-)

diff --git a/hw/pc.c b/hw/pc.c
index a77d3b6..d142282 100644
--- a/hw/pc.c
+++ b/hw/pc.c
@@ -262,6 +262,23 @@ static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
     int fd0, fd1, nb;
     int i;
 
+    /*
+     * hd_table[] has only IDE drives defined with -drive if=ide, not
+     * the ones defined with -device.  A proper fix requires quite a
+     * bit of surgery.  This is just a quick, temporary patch to make
+     * things work with libvirt.  It relies on the way libvirt names
+     * drives, namely "drive-ide0-BUS-UNIT".
+     */
+    for (i = 0; i < 4; i++) {
+        char id[32];
+
+        if (hd_table[i])
+            continue;
+        snprintf(id, sizeof(id), "drive-ide0-%d-%d",
+                 i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
+        hd_table[i] = drive_get_by_id(id);
+    }
+
     /* various important CMOS locations needed by PC/Bochs bios */
 
     /* memory size */
-- 
1.7.0.3

