source: trunk/packages/xen-3.1/xen-3.1/tools/ioemu/patches/xenstore-block-device-config @ 34

Last change on this file since 34 was 34, checked in by hartmans, 17 years ago

Add xen and xen-common

File size: 13.0 KB
Line 
1Index: ioemu/xenstore.c
2===================================================================
3--- ioemu.orig/xenstore.c       2007-05-03 15:17:52.000000000 +0100
4+++ ioemu/xenstore.c    2007-05-03 15:18:05.000000000 +0100
5@@ -9,8 +9,15 @@
6  */
7 
8 #include "vl.h"
9+#include "block_int.h"
10+#include <unistd.h>
11 
12 static struct xs_handle *xsh = NULL;
13+static char *media_filename[MAX_DISKS];
14+static QEMUTimer *insert_timer = NULL;
15+
16+#define UWAIT_MAX (30*1000000) /* thirty seconds */
17+#define UWAIT     (100000)     /* 1/10th second  */
18 
19 static int pasprintf(char **buf, const char *fmt, ...)
20 {
21@@ -28,9 +35,54 @@
22     return ret;
23 }
24 
25+static void insert_media(void *opaque)
26+{
27+    int i;
28+
29+    for (i = 0; i < MAX_DISKS; i++) {
30+        if (media_filename[i] && bs_table[i]) {
31+            do_change(bs_table[i]->device_name, media_filename[i]);
32+            free(media_filename[i]);
33+            media_filename[i] = NULL;
34+        }
35+    }
36+}
37+
38+void xenstore_check_new_media_present(int timeout)
39+{
40+
41+    if (insert_timer == NULL)
42+        insert_timer = qemu_new_timer(rt_clock, insert_media, NULL);
43+    qemu_mod_timer(insert_timer, qemu_get_clock(rt_clock) + timeout);
44+}
45+
46+static void waitForDevice(char *fn)
47+{
48+    struct stat sbuf;
49+    int status;
50+    int uwait = UWAIT_MAX;
51+
52+    do {
53+        status = stat(fn, &sbuf);
54+        if (!status) break;
55+        usleep(UWAIT);
56+        uwait -= UWAIT;
57+    } while (uwait > 0);
58+
59+    return;
60+}
61+
62 void xenstore_parse_domain_config(int domid)
63 {
64-    char *path;
65+    char **e = NULL;
66+    char *buf = NULL, *path;
67+    char *fpath = NULL, *bpath = NULL,
68+        *dev = NULL, *params = NULL, *type = NULL;
69+    int i;
70+    unsigned int len, num, hd_index;
71+
72+    for(i = 0; i < MAX_DISKS; i++)
73+        media_filename[i] = NULL;
74 
75     xsh = xs_daemon_open();
76     if (xsh == NULL) {
77@@ -44,8 +96,91 @@
78         goto out;
79     }
80 
81+    if (pasprintf(&buf, "%s/device/vbd", path) == -1)
82+        goto out;
83+
84+    e = xs_directory(xsh, XBT_NULL, buf, &num);
85+    if (e == NULL)
86+        goto out;
87+
88+    for (i = 0; i < num; i++) {
89+        /* read the backend path */
90+        if (pasprintf(&buf, "%s/device/vbd/%s/backend", path, e[i]) == -1)
91+            continue;
92+        free(bpath);
93+        bpath = xs_read(xsh, XBT_NULL, buf, &len);
94+        if (bpath == NULL)
95+            continue;
96+        /* read the name of the device */
97+        if (pasprintf(&buf, "%s/dev", bpath) == -1)
98+            continue;
99+        free(dev);
100+        dev = xs_read(xsh, XBT_NULL, buf, &len);
101+        if (dev == NULL)
102+            continue;
103+        if (strncmp(dev, "hd", 2) || strlen(dev) != 3)
104+            continue;
105+        hd_index = dev[2] - 'a';
106+        if (hd_index >= MAX_DISKS)
107+            continue;
108+        /* read the type of the device */
109+        if (pasprintf(&buf, "%s/device/vbd/%s/device-type", path, e[i]) == -1)
110+            continue;
111+        free(type);
112+        type = xs_read(xsh, XBT_NULL, buf, &len);
113+        if (pasprintf(&buf, "%s/params", bpath) == -1)
114+            continue;
115+        free(params);
116+        params = xs_read(xsh, XBT_NULL, buf, &len);
117+        if (params == NULL)
118+            continue;
119+        /*
120+         * check if device has a phantom vbd; the phantom is hooked
121+         * to the frontend device (for ease of cleanup), so lookup
122+         * the frontend device, and see if there is a phantom_vbd
123+         * if there is, we will use resolution as the filename
124+         */
125+        if (pasprintf(&buf, "%s/device/vbd/%s/phantom_vbd", path, e[i]) == -1)
126+            continue;
127+        free(fpath);
128+        fpath = xs_read(xsh, XBT_NULL, buf, &len);
129+        if (fpath) {
130+            if (pasprintf(&buf, "%s/dev", fpath) == -1)
131+                continue;
132+            free(params);
133+            params = xs_read(xsh, XBT_NULL, buf , &len);
134+            if (params) {
135+                /*
136+                 * wait for device, on timeout silently fail because we will
137+                 * fail to open below
138+                 */
139+                waitForDevice(params);
140+            }
141+        }
142+
143+        bs_table[hd_index] = bdrv_new(dev);
144+        /* check if it is a cdrom */
145+        if (type && !strcmp(type, "cdrom")) {
146+            bdrv_set_type_hint(bs_table[hd_index], BDRV_TYPE_CDROM);
147+            if (pasprintf(&buf, "%s/params", bpath) != -1)
148+                xs_watch(xsh, buf, dev);
149+        }
150+        /* open device now if media present */
151+        if (params[0]) {
152+            if (bdrv_open(bs_table[hd_index], params, 0 /* snapshot */) < 0)
153+                fprintf(stderr, "qemu: could not open hard disk image '%s'\n",
154+                        params);
155+        }
156+    }
157+
158  out:
159+    free(type);
160+    free(params);
161+    free(dev);
162+    free(bpath);
163+    free(buf);
164     free(path);
165+    free(e);
166     return;
167 }
168 
169@@ -58,14 +193,35 @@
170 
171 void xenstore_process_event(void *opaque)
172 {
173-    char **vec;
174-    unsigned int num;
175+    char **vec, *image = NULL;
176+    unsigned int len, num, hd_index;
177 
178     vec = xs_read_watch(xsh, &num);
179     if (!vec)
180         return;
181 
182+    if (strncmp(vec[XS_WATCH_TOKEN], "hd", 2) ||
183+        strlen(vec[XS_WATCH_TOKEN]) != 3)
184+        goto out;
185+    hd_index = vec[XS_WATCH_TOKEN][2] - 'a';
186+    image = xs_read(xsh, XBT_NULL, vec[XS_WATCH_PATH], &len);
187+    if (image == NULL || !strcmp(image, bs_table[hd_index]->filename))
188+        goto out;  /* gone or identical */
189+
190+    do_eject(0, vec[XS_WATCH_TOKEN]);
191+    bs_table[hd_index]->filename[0] = 0;
192+    if (media_filename[hd_index]) {
193+        free(media_filename[hd_index]);
194+        media_filename[hd_index] = NULL;
195+    }
196+
197+    if (image[0]) {
198+        media_filename[hd_index] = strdup(image);
199+        xenstore_check_new_media_present(5000);
200+    }
201+
202  out:
203+    free(image);
204     free(vec);
205 }
206 
207Index: ioemu/vl.c
208===================================================================
209--- ioemu.orig/vl.c     2007-05-03 15:17:52.000000000 +0100
210+++ ioemu/vl.c  2007-05-03 15:18:05.000000000 +0100
211@@ -5331,9 +5331,11 @@
212            "Standard options:\n"
213            "-M machine      select emulated machine (-M ? for list)\n"
214            "-fda/-fdb file  use 'file' as floppy disk 0/1 image\n"
215+#ifndef CONFIG_DM
216            "-hda/-hdb file  use 'file' as IDE hard disk 0/1 image\n"
217            "-hdc/-hdd file  use 'file' as IDE hard disk 2/3 image\n"
218            "-cdrom file     use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
219+#endif /* !CONFIG_DM */
220            "-boot [a|c|d]   boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
221           "-snapshot       write to temporary files instead of disk image files\n"
222 #ifdef TARGET_I386
223@@ -5460,11 +5462,13 @@
224     QEMU_OPTION_M,
225     QEMU_OPTION_fda,
226     QEMU_OPTION_fdb,
227+#ifndef CONFIG_DM
228     QEMU_OPTION_hda,
229     QEMU_OPTION_hdb,
230     QEMU_OPTION_hdc,
231     QEMU_OPTION_hdd,
232     QEMU_OPTION_cdrom,
233+#endif /* !CONFIG_DM */
234     QEMU_OPTION_boot,
235     QEMU_OPTION_snapshot,
236 #ifdef TARGET_I386
237@@ -5536,11 +5540,13 @@
238     { "M", HAS_ARG, QEMU_OPTION_M },
239     { "fda", HAS_ARG, QEMU_OPTION_fda },
240     { "fdb", HAS_ARG, QEMU_OPTION_fdb },
241+#ifndef CONFIG_DM
242     { "hda", HAS_ARG, QEMU_OPTION_hda },
243     { "hdb", HAS_ARG, QEMU_OPTION_hdb },
244     { "hdc", HAS_ARG, QEMU_OPTION_hdc },
245     { "hdd", HAS_ARG, QEMU_OPTION_hdd },
246     { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
247+#endif /* !CONFIG_DM */
248     { "boot", HAS_ARG, QEMU_OPTION_boot },
249     { "snapshot", 0, QEMU_OPTION_snapshot },
250 #ifdef TARGET_I386
251@@ -5882,10 +5888,16 @@
252 #ifdef CONFIG_GDBSTUB
253     int use_gdbstub, gdbstub_port;
254 #endif
255-    int i, cdrom_index;
256+    int i;
257+#ifndef CONFIG_DM
258+    int cdrom_index;
259+#endif /* !CONFIG_DM */
260     int snapshot, linux_boot;
261     const char *initrd_filename;
262-    const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
263+#ifndef CONFIG_DM
264+    const char *hd_filename[MAX_DISKS];
265+#endif /* !CONFIG_DM */
266+    const char *fd_filename[MAX_FD];
267     const char *kernel_filename, *kernel_cmdline;
268     DisplayState *ds = &display_state;
269     int cyls, heads, secs, translation;
270@@ -5946,8 +5958,10 @@
271     initrd_filename = NULL;
272     for(i = 0; i < MAX_FD; i++)
273         fd_filename[i] = NULL;
274+#ifndef CONFIG_DM
275     for(i = 0; i < MAX_DISKS; i++)
276         hd_filename[i] = NULL;
277+#endif /* !CONFIG_DM */
278     ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
279     vga_ram_size = VGA_RAM_SIZE;
280     bios_size = BIOS_SIZE;
281@@ -5961,11 +5975,13 @@
282     vncunused = 0;
283     kernel_filename = NULL;
284     kernel_cmdline = "";
285+#ifndef CONFIG_DM
286 #ifdef TARGET_PPC
287     cdrom_index = 1;
288 #else
289     cdrom_index = 2;
290 #endif
291+#endif /* !CONFIG_DM */
292     cyls = heads = secs = 0;
293     translation = BIOS_ATA_TRANSLATION_AUTO;
294     pstrcpy(monitor_device, sizeof(monitor_device), "null");
295@@ -6004,7 +6020,11 @@
296             break;
297         r = argv[optind];
298         if (r[0] != '-') {
299+#ifndef CONFIG_DM
300             hd_filename[0] = argv[optind++];
301+#else
302+            help();
303+#endif /* !CONFIG_DM */
304         } else {
305             const QEMUOption *popt;
306 
307@@ -6048,6 +6068,7 @@
308             case QEMU_OPTION_initrd:
309                 initrd_filename = optarg;
310                 break;
311+#ifndef CONFIG_DM
312             case QEMU_OPTION_hda:
313             case QEMU_OPTION_hdb:
314             case QEMU_OPTION_hdc:
315@@ -6060,6 +6081,7 @@
316                         cdrom_index = -1;
317                 }
318                 break;
319+#endif /* !CONFIG_DM */
320             case QEMU_OPTION_snapshot:
321                 snapshot = 1;
322                 break;
323@@ -6112,11 +6134,13 @@
324             case QEMU_OPTION_append:
325                 kernel_cmdline = optarg;
326                 break;
327+#ifndef CONFIG_DM
328             case QEMU_OPTION_cdrom:
329                 if (cdrom_index >= 0) {
330                     hd_filename[cdrom_index] = optarg;
331                 }
332                 break;
333+#endif /* !CONFIG_DM */
334             case QEMU_OPTION_boot:
335                 boot_device = optarg[0];
336                 if (boot_device != 'a' &&
337@@ -6372,6 +6396,7 @@
338     }
339 
340 #ifdef CONFIG_DM
341+    bdrv_init();
342     xenstore_parse_domain_config(domid);
343 #endif /* CONFIG_DM */
344 
345@@ -6381,6 +6406,7 @@
346 #endif
347     linux_boot = (kernel_filename != NULL);
348         
349+#ifndef CONFIG_DM
350     if (!linux_boot &&
351         hd_filename[0] == '\0' &&
352         (cdrom_index >= 0 && hd_filename[cdrom_index] == '\0') &&
353@@ -6394,6 +6420,7 @@
354         else
355             boot_device = 'd';
356     }
357+#endif /* !CONFIG_DM */
358 
359     setvbuf(stdout, NULL, _IOLBF, 0);
360     
361@@ -6514,6 +6541,7 @@
362 
363 #endif /* !CONFIG_DM */
364 
365+#ifndef CONFIG_DM
366     /* we always create the cdrom drive, even if no disk is there */
367     bdrv_init();
368     if (cdrom_index >= 0) {
369@@ -6540,6 +6568,7 @@
370             }
371         }
372     }
373+#endif /* !CONFIG_DM */
374 
375     /* we always create at least one floppy disk */
376     fd_table[0] = bdrv_new("fda");
377Index: ioemu/monitor.c
378===================================================================
379--- ioemu.orig/monitor.c        2007-05-03 15:17:52.000000000 +0100
380+++ ioemu/monitor.c     2007-05-03 15:18:05.000000000 +0100
381@@ -24,6 +24,7 @@
382 #include "vl.h"
383 #include "disas.h"
384 #include <dirent.h>
385+#include "block_int.h"
386 
387 //#define DEBUG
388 //#define DEBUG_COMPLETION
389@@ -330,7 +331,7 @@
390     return 0;
391 }
392 
393-static void do_eject(int force, const char *filename)
394+void do_eject(int force, const char *filename)
395 {
396     BlockDriverState *bs;
397 
398@@ -342,7 +343,7 @@
399     eject_device(bs, force);
400 }
401 
402-static void do_change(const char *device, const char *filename)
403+void do_change(const char *device, const char *filename)
404 {
405     BlockDriverState *bs;
406     int i;
407Index: ioemu/block.c
408===================================================================
409--- ioemu.orig/block.c  2007-05-03 15:17:52.000000000 +0100
410+++ ioemu/block.c       2007-05-03 15:18:05.000000000 +0100
411@@ -758,6 +758,7 @@
412 static void raw_close(BlockDriverState *bs)
413 {
414     BDRVRawState *s = bs->opaque;
415+    bs->total_sectors = 0;
416     close(s->fd);
417 }
418 
419Index: ioemu/vl.h
420===================================================================
421--- ioemu.orig/vl.h     2007-05-03 15:18:00.000000000 +0100
422+++ ioemu/vl.h  2007-05-03 15:18:05.000000000 +0100
423@@ -1192,6 +1192,8 @@
424 void term_print_help(void);
425 void monitor_readline(const char *prompt, int is_password,
426                       char *buf, int buf_size);
427+void do_eject(int force, const char *filename);
428+void do_change(const char *device, const char *filename);
429 
430 /* readline.c */
431 typedef void ReadLineFunc(void *opaque, const char *str);
432@@ -1206,6 +1208,9 @@
433 
434 /* xenstore.c */
435 void xenstore_parse_domain_config(int domid);
436+int xenstore_fd(void);
437+void xenstore_process_event(void *opaque);
438+void xenstore_check_new_media_present(int timeout);
439 
440 int xenstore_vm_write(int domid, char *key, char *val);
441 char *xenstore_vm_read(int domid, char *key, int *len);
442Index: ioemu/hw/ide.c
443===================================================================
444--- ioemu.orig/hw/ide.c 2007-05-03 15:17:52.000000000 +0100
445+++ ioemu/hw/ide.c      2007-05-03 15:18:05.000000000 +0100
446@@ -1199,6 +1199,7 @@
447         } else {
448             ide_atapi_cmd_error(s, SENSE_NOT_READY,
449                                 ASC_MEDIUM_NOT_PRESENT);
450+            xenstore_check_new_media_present(1000);
451         }
452         break;
453     case GPCMD_MODE_SENSE_10:
Note: See TracBrowser for help on using the repository browser.