Skip to contentSkip to conversation

qmp: report TRAY_STATE_CHANGED events

PATCH In review Paolo Bonzini 3 participants 5 messages Oct 25, 2011 – Nov 4, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qmp: report TRAY_STATE_CHANGED events

---
 QMP/qmp-events.txt |   20 ++++++++++++++++++++
 block.c            |    9 +++++++++
 monitor.c          |    3 +++
 monitor.h          |    1 +
 4 files changed, 33 insertions(+), 0 deletions(-)
QMP/qmp-events.txt +20 -0
index 0ce5d4e..242c7a5 100644
@@ -264,3 +264,23 @@ Example:
 
 Note: If action is "reset", "shutdown", or "pause" the WATCHDOG event is
 followed respectively by the RESET, SHUTDOWN, or STOP events.
+
+
+TRAY_STATE_CHANGED
+------------------
+
+Emitted when the state of a CD-ROM tray changes.
+
+Data:
+
+- "device": device name (json-string)
+- "state": state of the tray, one of the following (json-string)
+   "open": tray is open
+   "closed": tray is closed
+
+Example:
+
+{ "event": "TRAY_STATE_CHANGED",
+    "data": { "device": "ide0-cd0",
+              "state": "open" },
+    "timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
block.c +9 -0
index 9873b57..7b0aa62 100644
@@ -2968,10 +2968,19 @@ int bdrv_media_changed(BlockDriverState *bs)
 void bdrv_eject(BlockDriverState *bs, int eject_flag)
 {
     BlockDriver *drv = bs->drv;
+    QObject *data;
 
     if (drv && drv->bdrv_eject) {
         drv->bdrv_eject(bs, eject_flag);
     }
+
+    if (*bs->device_name) {
+        data = qobject_from_jsonf("{ 'device': %s, 'state': %s }",
+                                  bs->device_name,
+                                  eject_flag ? "open" : "closed");
+        monitor_protocol_event(QEVENT_TRAY_STATE_CHANGED, data);
+        qobject_decref(data);
+    }
 }
 
 /**
monitor.c +3 -0
index ffda0fe..516a789 100644
@@ -482,6 +482,9 @@ void monitor_protocol_event(MonitorEvent event, QObject *data)
         case QEVENT_SPICE_DISCONNECTED:
             event_name = "SPICE_DISCONNECTED";
             break;
+        case QEVENT_TRAY_STATE_CHANGED:
+            event_name = "TRAY_STATE_CHANGED";
+            break;
         default:
             abort();
             break;
monitor.h +1 -0
index 4f2d328..b8f4bf7 100644
@@ -35,6 +35,7 @@ typedef enum MonitorEvent {
     QEVENT_SPICE_CONNECTED,
     QEVENT_SPICE_INITIALIZED,
     QEVENT_SPICE_DISCONNECTED,
+    QEVENT_TRAY_STATE_CHANGED,
     QEVENT_MAX,
 } MonitorEvent;
 
Signature · 1 line
1.7.6.4
#1 lore raw
Quoted 19 lines · On Tue, 25 Oct 2011 18:26:58 +0200 Paolo Bonzini <[email protected]> wrote:
---
 QMP/qmp-events.txt |   20 ++++++++++++++++++++
 block.c            |    9 +++++++++
 monitor.c          |    3 +++
 monitor.h          |    1 +
 4 files changed, 33 insertions(+), 0 deletions(-)
QMP/qmp-events.txt +3 -0
index 0ce5d4e..242c7a5 100644
@@ -264,3 +264,23 @@ Example:
 
 Note: If action is "reset", "shutdown", or "pause" the WATCHDOG event is
 followed respectively by the RESET, SHUTDOWN, or STOP events.
+
+
+TRAY_STATE_CHANGED

QMP/qmp-events.txt Example: · line 279

Quoted 10 lines
QMP/qmp-events.txt +10 -0
+------------------
+
+Emitted when the state of a CD-ROM tray changes.
+
+Data:
+
+- "device": device name (json-string)
+- "state": state of the tray, one of the following (json-string)
+   "open": tray is open
+   "closed": tray is closed

Wouldn't be better to make this a bool?

CC'ing the block guys to get ACKs from them too.

Quoted 57 lines
QMP/qmp-events.txt +7 -0
+
+Example:
+
+{ "event": "TRAY_STATE_CHANGED",
+    "data": { "device": "ide0-cd0",
+              "state": "open" },
+    "timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
block.c +9 -0
index 9873b57..7b0aa62 100644
@@ -2968,10 +2968,19 @@ int bdrv_media_changed(BlockDriverState *bs)
 void bdrv_eject(BlockDriverState *bs, int eject_flag)
 {
     BlockDriver *drv = bs->drv;
+    QObject *data;
 
     if (drv && drv->bdrv_eject) {
         drv->bdrv_eject(bs, eject_flag);
     }
+
+    if (*bs->device_name) {
+        data = qobject_from_jsonf("{ 'device': %s, 'state': %s }",
+                                  bs->device_name,
+                                  eject_flag ? "open" : "closed");
+        monitor_protocol_event(QEVENT_TRAY_STATE_CHANGED, data);
+        qobject_decref(data);
+    }
 }
 
 /**
monitor.c +3 -0
index ffda0fe..516a789 100644
@@ -482,6 +482,9 @@ void monitor_protocol_event(MonitorEvent event, QObject *data)
         case QEVENT_SPICE_DISCONNECTED:
             event_name = "SPICE_DISCONNECTED";
             break;
+        case QEVENT_TRAY_STATE_CHANGED:
+            event_name = "TRAY_STATE_CHANGED";
+            break;
         default:
             abort();
             break;
monitor.h +1 -0
index 4f2d328..b8f4bf7 100644
@@ -35,6 +35,7 @@ typedef enum MonitorEvent {
     QEVENT_SPICE_CONNECTED,
     QEVENT_SPICE_INITIALIZED,
     QEVENT_SPICE_DISCONNECTED,
+    QEVENT_TRAY_STATE_CHANGED,
     QEVENT_MAX,
 } MonitorEvent;
 
#2 lore raw
In reply to Paolo Bonzini

It seems this patch can not work well.

there is my test process.

I startup a guest by the follow option: -drive file=/home/fsh/image/OCDC-natty-Test-Drive-20110823_010339.iso,if=ide,index=1,media=cdrom

after the guest startups, then I right click mouse in the UI of the guest, and select the Eject from the menu.
there comes an event in the qmp-monitor.
{"timestamp": {"seconds": 1320118137, "microseconds": 420150}, "event": "TRAY_STATE_CHANGED", "data": {"device": "ide0-cd1", "state": "open"}}

however, if I change the cdrom by this command:
{ "execute": "change","arguments": { "device": "ide0-cd1", "target": "/home/fsh/image/OCDC-natty-Test-Drive-20110823_010339.iso" } } there is no any event.

and { "execute": "eject", "arguments": { "device": "ide0-cd1" } }, there is also no any event.

only I right click mouse in the UI of the guest, qmp-monitor can receive an TRAY_STATE_CHANGED event.

Quoted 86 lines · Paolo Bonzini
---
  QMP/qmp-events.txt |   20 ++++++++++++++++++++
  block.c            |    9 +++++++++
  monitor.c          |    3 +++
  monitor.h          |    1 +
  4 files changed, 33 insertions(+), 0 deletions(-)
QMP/qmp-events.txt +20 -0
index 0ce5d4e..242c7a5 100644
@@ -264,3 +264,23 @@ Example:
 
  Note: If action is "reset", "shutdown", or "pause" the WATCHDOG event is
  followed respectively by the RESET, SHUTDOWN, or STOP events.
+
+
+TRAY_STATE_CHANGED
+------------------
+
+Emitted when the state of a CD-ROM tray changes.
+
+Data:
+
+- "device": device name (json-string)
+- "state": state of the tray, one of the following (json-string)
+   "open": tray is open
+   "closed": tray is closed
+
+Example:
+
+{ "event": "TRAY_STATE_CHANGED",
+    "data": { "device": "ide0-cd0",
+              "state": "open" },
+    "timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
block.c +9 -0
index 9873b57..7b0aa62 100644
@@ -2968,10 +2968,19 @@ int bdrv_media_changed(BlockDriverState *bs)
  void bdrv_eject(BlockDriverState *bs, int eject_flag)
  {
      BlockDriver *drv = bs->drv;
+    QObject *data;
 
      if (drv&&  drv->bdrv_eject) {
          drv->bdrv_eject(bs, eject_flag);
      }
+
+    if (*bs->device_name) {
+        data = qobject_from_jsonf("{ 'device': %s, 'state': %s }",
+                                  bs->device_name,
+                                  eject_flag ? "open" : "closed");
+        monitor_protocol_event(QEVENT_TRAY_STATE_CHANGED, data);
+        qobject_decref(data);
+    }
  }
 
  /**
monitor.c +3 -0
index ffda0fe..516a789 100644
@@ -482,6 +482,9 @@ void monitor_protocol_event(MonitorEvent event, QObject *data)
          case QEVENT_SPICE_DISCONNECTED:
              event_name = "SPICE_DISCONNECTED";
              break;
+        case QEVENT_TRAY_STATE_CHANGED:
+            event_name = "TRAY_STATE_CHANGED";
+            break;
          default:
              abort();
              break;
monitor.h +1 -0
index 4f2d328..b8f4bf7 100644
@@ -35,6 +35,7 @@ typedef enum MonitorEvent {
      QEVENT_SPICE_CONNECTED,
      QEVENT_SPICE_INITIALIZED,
      QEVENT_SPICE_DISCONNECTED,
+    QEVENT_TRAY_STATE_CHANGED,
      QEVENT_MAX,
  } MonitorEvent;
 
#3 lore raw
Quoted 6 lines · ShaoHe Feng

after the guest startups, then I right click mouse in the UI of the guest, and select the Eject from the menu.
there comes an event in the qmp-monitor.
{"timestamp": {"seconds": 1320118137, "microseconds": 420150}, "event":
"TRAY_STATE_CHANGED", "data": {"device": "ide0-cd1", "state": "open"}}

Quoted 6 lines

however, if I change the cdrom by this command:
{ "execute": "change","arguments": { "device": "ide0-cd1", "target":
"/home/fsh/image/OCDC-natty-Test-Drive-20110823_010339.iso" } } there is no any event.
and { "execute": "eject", "arguments": { "device": "ide0-cd1" } }, there is also no any event.

This was by design. The idea was that management can do the following to change a CD when the guest keeps the medium locked and reacts to eject requests (like very recent Linux does):

Scenario 1: non-forced media change

1. start looking at TRAY_STATE_CHANGED events
2. execute "eject" command
3. execute "query-block"
4. if disk is still shown as closed, check for guest reactions:
4.1. if no TRAY_STATE_CHANGED event has been reported since
step 1, wait until a TRAY_STATE_CHANGED event has arrived
4.2. if the TRAY_STATE_CHANGED event had state == closed, fail
5. execute "change" command

Scenario 2: forced media change

1. execute "eject -f" command (with the posted patches that always unlock the tray upon "eject -f")
2. execute "change" command
3. if it fails, restart

Paolo

#4 lore raw
Quoted 14 lines · On Fri, 04 Nov 2011 11:53:53 +0100 Paolo Bonzini <[email protected]> wrote:
Quoted 6 lines · ShaoHe Feng

after the guest startups, then I right click mouse in the UI of the guest, and select the Eject from the menu.
there comes an event in the qmp-monitor.
{"timestamp": {"seconds": 1320118137, "microseconds": 420150}, "event":
"TRAY_STATE_CHANGED", "data": {"device": "ide0-cd1", "state": "open"}}

Quoted 6 lines

however, if I change the cdrom by this command:
{ "execute": "change","arguments": { "device": "ide0-cd1", "target":
"/home/fsh/image/OCDC-natty-Test-Drive-20110823_010339.iso" } } there is no any event.
and { "execute": "eject", "arguments": { "device": "ide0-cd1" } }, there is also no any event.

Quoted 3 lines

This was by design. The idea was that management can do the following to change a CD when the guest keeps the medium locked and reacts to eject requests (like very recent Linux does):

Just a note: this didn't make 1.0... I replied to it but didn't get feedback:

http://lists.gnu.org/archive/html/qemu-devel/2011-10/msg03096.html

Quoted 21 lines

Scenario 1: non-forced media change

1. start looking at TRAY_STATE_CHANGED events
2. execute "eject" command
3. execute "query-block"
4. if disk is still shown as closed, check for guest reactions:
4.1. if no TRAY_STATE_CHANGED event has been reported since
step 1, wait until a TRAY_STATE_CHANGED event has arrived
4.2. if the TRAY_STATE_CHANGED event had state == closed, fail
5. execute "change" command

Scenario 2: forced media change

1. execute "eject -f" command (with the posted patches that always unlock the tray upon "eject -f")
2. execute "change" command
3. if it fails, restart

Paolo