Skip to contentSkip to conversation

don't exit if cdrom media fails to open

PATCH In review David Ahern 3 participants 4 messages Jun 9, 2009 – Jun 10, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 don't exit if cdrom media fails to open

This patch allows a VM to continue even if the cdrom image is not accessible at VM boot time. It allows a boot progression similar to real hardware (e.g., try cd, then disk).

For example,

qemu-system-x86_64 -cdrom /dev/dvd -hda disk.img -boot dc

If there is no media in the tray, qemu currently exits with the message:
qemu: could not open disk image /dev/dvd

With this patch, the message is still displayed, but qemu continues. It first tries to boot from the cdrom and then falls back to the disk.

---

vl.c +1 -0
index fcf8532..bd3709c 100644
@@ -152,6 +152,7 @@ int main(int argc, char **argv)
 #include "qemu-char.h"
 #include "cache-utils.h"
 #include "block.h"
+#include "block_int.h"
 #include "dma.h"
 #include "audio/audio.h"
 #include "migration.h"
@@ -2551,7 +2552,8 @@ int drive_init(struct drive_opt *arg, int

snapshot, void *opaque)

     if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
         fprintf(stderr, "qemu: could not open disk image %s\n",
                         file);
-        return -1;
+        if (bdrv->type != BDRV_TYPE_CDROM)
+            return -1;
     }
     if (bdrv_key_required(bdrv))
         autostart = 0;
#1 lore raw

Hi David,

Quoted 31 lines · David Ahern

This patch allows a VM to continue even if the cdrom image is not accessible at VM boot time. It allows a boot progression similar to real hardware (e.g., try cd, then disk).

For example,

qemu-system-x86_64 -cdrom /dev/dvd -hda disk.img -boot dc

If there is no media in the tray, qemu currently exits with the message:
qemu: could not open disk image /dev/dvd

With this patch, the message is still displayed, but qemu continues. It first tries to boot from the cdrom and then falls back to the disk.

---

vl.c +1 -0
index fcf8532..bd3709c 100644
@@ -152,6 +152,7 @@ int main(int argc, char **argv)
 #include "qemu-char.h"
 #include "cache-utils.h"
 #include "block.h"
+#include "block_int.h"
 #include "dma.h"
 #include "audio/audio.h"
 #include "migration.h"

vl.c int drive_init(struct drive_opt *arg, int · line 2557

Quoted 7 lines
vl.c +2 -1
@@ -2551,7 +2552,8 @@ int drive_init(struct drive_opt *arg, int
 snapshot, void *opaque)
     if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
         fprintf(stderr, "qemu: could not open disk image %s\n",
                         file);
-        return -1;
+        if (bdrv->type != BDRV_TYPE_CDROM)
+            return -1;

I think Cole's patch here:

https://bugzilla.redhat.com/show_bug.cgi?id=473154#c15

fixes the same problem? Certainly, it seems to me that any code requiring the path to be e.g. /dev/cd is doomed and should be fixed as Cole suggests?

Cheers,
Mark.

(Patch below for convenience)

qemu/block-raw-posix.c +8 -3
@@ -942,16 +942,21 @@ static int hdev_open(BlockDriverState *b
 
     s->type = FTYPE_FILE;
 #if defined(__linux__)
-    if (strstart(filename, "/dev/cd", NULL)) {
+    if (strstart(filename, "/dev/cd", NULL) ||
+        bs->type == BDRV_TYPE_CDROM) {
         /* open will not fail even if no CD is inserted */
         open_flags |= O_NONBLOCK;
         s->type = FTYPE_CD;
-    } else if (strstart(filename, "/dev/fd", NULL)) {
+    }
+    if (strstart(filename, "/dev/fd", NULL) ||
+        bs->type == BDRV_TYPE_FLOPPY) {
         s->type = FTYPE_FD;
         s->fd_open_flags = open_flags;
         /* open will not fail even if no floppy is inserted */
         open_flags |= O_NONBLOCK;
-    } else if (strstart(filename, "/dev/sg", NULL)) {
+    }
+    if (s->type = FTYPE_FILE &&
+        strstart(filename, "/dev/sg", NULL)) {
         bs->sg = 1;
     }
 #endif
#2 lore raw
Quoted 44 lines · Mark McLoughlin

Hi David,

Quoted 31 lines · David Ahern

This patch allows a VM to continue even if the cdrom image is not accessible at VM boot time. It allows a boot progression similar to real hardware (e.g., try cd, then disk).

For example,

qemu-system-x86_64 -cdrom /dev/dvd -hda disk.img -boot dc

If there is no media in the tray, qemu currently exits with the message:
qemu: could not open disk image /dev/dvd

With this patch, the message is still displayed, but qemu continues. It first tries to boot from the cdrom and then falls back to the disk.

---

vl.c +1 -0
index fcf8532..bd3709c 100644
@@ -152,6 +152,7 @@ int main(int argc, char **argv)
 #include "qemu-char.h"
 #include "cache-utils.h"
 #include "block.h"
+#include "block_int.h"
 #include "dma.h"
 #include "audio/audio.h"
 #include "migration.h"

vl.c int drive_init(struct drive_opt *arg, int · line 2557

Quoted 7 lines
vl.c +2 -1
@@ -2551,7 +2552,8 @@ int drive_init(struct drive_opt *arg, int
 snapshot, void *opaque)
     if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
         fprintf(stderr, "qemu: could not open disk image %s\n",
                         file);
-        return -1;
+        if (bdrv->type != BDRV_TYPE_CDROM)
+            return -1;

I think Cole's patch here:

Quoted 5 lines

https://bugzilla.redhat.com/show_bug.cgi?id=473154#c15

fixes the same problem? Certainly, it seems to me that any code requiring the path to be e.g. /dev/cd is doomed and should be fixed as Cole suggests?

Cole's patch works as well. I don't have a preference for how it gets done, just that I can start a guest without checking whether there is media in the tray and adjusting the qemu invocation.

thanks,

david

Quoted 36 lines

Cheers,
Mark.

(Patch below for convenience)

qemu/block-raw-posix.c +8 -3
@@ -942,16 +942,21 @@ static int hdev_open(BlockDriverState *b
 
     s->type = FTYPE_FILE;
 #if defined(__linux__)
-    if (strstart(filename, "/dev/cd", NULL)) {
+    if (strstart(filename, "/dev/cd", NULL) ||
+        bs->type == BDRV_TYPE_CDROM) {
         /* open will not fail even if no CD is inserted */
         open_flags |= O_NONBLOCK;
         s->type = FTYPE_CD;
-    } else if (strstart(filename, "/dev/fd", NULL)) {
+    }
+    if (strstart(filename, "/dev/fd", NULL) ||
+        bs->type == BDRV_TYPE_FLOPPY) {
         s->type = FTYPE_FD;
         s->fd_open_flags = open_flags;
         /* open will not fail even if no floppy is inserted */
         open_flags |= O_NONBLOCK;
-    } else if (strstart(filename, "/dev/sg", NULL)) {
+    }
+    if (s->type = FTYPE_FILE &&
+        strstart(filename, "/dev/sg", NULL)) {
         bs->sg = 1;
     }
 #endif
#3 lore raw
Quoted 47 lines · David Ahern
Quoted 45 lines · Mark McLoughlin

Hi David,

Quoted 31 lines · David Ahern

This patch allows a VM to continue even if the cdrom image is not accessible at VM boot time. It allows a boot progression similar to real hardware (e.g., try cd, then disk).

For example,

qemu-system-x86_64 -cdrom /dev/dvd -hda disk.img -boot dc

If there is no media in the tray, qemu currently exits with the message:
qemu: could not open disk image /dev/dvd

With this patch, the message is still displayed, but qemu continues. It first tries to boot from the cdrom and then falls back to the disk.

---

vl.c +1 -0
index fcf8532..bd3709c 100644
@@ -152,6 +152,7 @@ int main(int argc, char **argv)
 #include "qemu-char.h"
 #include "cache-utils.h"
 #include "block.h"
+#include "block_int.h"
 #include "dma.h"
 #include "audio/audio.h"
 #include "migration.h"

vl.c int drive_init(struct drive_opt *arg, int · line 2557

Quoted 7 lines
vl.c +2 -1
@@ -2551,7 +2552,8 @@ int drive_init(struct drive_opt *arg, int
 snapshot, void *opaque)
     if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
         fprintf(stderr, "qemu: could not open disk image %s\n",
                         file);
-        return -1;
+        if (bdrv->type != BDRV_TYPE_CDROM)
+            return -1;

I think Cole's patch here:

https://bugzilla.redhat.com/show_bug.cgi?id=473154#c15

Quoted 8 lines
Quoted 3 lines

fixes the same problem? Certainly, it seems to me that any code requiring the path to be e.g. /dev/cd is doomed and should be fixed as Cole suggests?

Cole's patch works as well. I don't have a preference for how it gets done, just that I can start a guest without checking whether there is media in the tray and adjusting the qemu invocation.

There is an extra benefit to my patch: /dev/dvd will be treated as a host cdrom device, so ejecting media in the guest will be mapped to a physical eject on the host.

I'll touch up my patch and send with a Signed-off-by.

Thanks,
Cole