| 1 | QMP |
| 2 | === |
| 3 | |
| 4 | Package `qmp` enables interaction with QEMU instances via the QEMU Machine Protocol (QMP). |
| 5 | |
| 6 | ## Available Drivers |
| 7 | |
| 8 | ### Libvirt |
| 9 | |
| 10 | If your environment is managed by Libvirt, QMP interaction must be proxied through the Libvirt daemon. This can be be done through two available drivers: |
| 11 | |
| 12 | #### RPC |
| 13 | |
| 14 | The RPC driver provides a pure Go implementation of Libvirt's RPC protocol. |
| 15 | |
| 16 | ```go |
| 17 | //conn, err := net.DialTimeout("unix", "/var/run/libvirt/libvirt-sock", 2*time.Second) |
| 18 | conn, err := net.DialTimeout("tcp", "192.168.1.1:16509", 2*time.Second) |
| 19 | monitor := libvirtrpc.New("stage-lb-1", conn) |
| 20 | ``` |
| 21 | |
| 22 | #### virsh |
| 23 | |
| 24 | A connection to the monitor socket is provided by proxing requests through the `virsh` executable. |
| 25 | |
| 26 | ```go |
| 27 | monitor, err := qmp.NewLibvirtMonitor("qemu:///system", "stage-lb-1") |
| 28 | ``` |
| 29 | |
| 30 | ### Socket |
| 31 | |
| 32 | If your QEMU instances are not managed by libvirt, direct communication over its UNIX socket is available. |
| 33 | |
| 34 | ```go |
| 35 | monitor, err := qmp.NewSocketMonitor("unix", "/var/lib/qemu/example.monitor", 2*time.Second) |
| 36 | ``` |
| 37 | |
| 38 | ## Examples |
| 39 | |
| 40 | Using the above to establish a new `qmp.Monitor`, the following examples provide a brief overview of QMP usage. |
| 41 | |
| 42 | _error checking omitted for the sake of brevity._ |
| 43 | |
| 44 | ### Command Execution |
| 45 | ```go |
| 46 | type StatusResult struct { |
| 47 | ID string `json:"id"` |
| 48 | Return struct { |
| 49 | Running bool `json:"running"` |
| 50 | Singlestep bool `json:"singlestep"` |
| 51 | Status string `json:"status"` |
| 52 | } `json:"return"` |
| 53 | } |
| 54 | |
| 55 | monitor.Connect() |
| 56 | defer monitor.Disconnect() |
| 57 | |
| 58 | cmd := []byte(`{ "execute": "query-status" }`) |
| 59 | raw, _ := monitor.Run(cmd) |
| 60 | |
| 61 | var result StatusResult |
| 62 | json.Unmarshal(raw, &result) |
| 63 | |
| 64 | fmt.Println(result.Return.Status) |
| 65 | ``` |
| 66 | |
| 67 | ``` |
| 68 | running |
| 69 | ``` |
| 70 | |
| 71 | ### Event Monitor |
| 72 | |
| 73 | ```go |
| 74 | monitor.Connect() |
| 75 | defer monitor.Disconnect() |
| 76 | |
| 77 | stream, _ := monitor.Events() |
| 78 | for e := range stream { |
| 79 | log.Printf("EVENT: %s", e.Event) |
| 80 | } |
| 81 | |
| 82 | ``` |
| 83 | |
| 84 | ``` |
| 85 | $ virsh reboot example |
| 86 | Domain example is being rebooted |
| 87 | ``` |
| 88 | |
| 89 | ``` |
| 90 | EVENT: POWERDOWN |
| 91 | EVENT: SHUTDOWN |
| 92 | EVENT: STOP |
| 93 | EVENT: RESET |
| 94 | EVENT: RESUME |
| 95 | EVENT: RESET |
| 96 | ... |
| 97 | ``` |
| 98 | |
| 99 | ## More information |
| 100 | |
| 101 | * [QEMU QMP Wiki](http://wiki.qemu.org/QMP) |
| 102 | * [QEMU QMP Intro](http://git.qemu.org/?p=qemu.git;a=blob_plain;f=docs/qmp-intro.txt;hb=HEAD) |
| 103 | * [QEMU QMP Events](http://git.qemu.org/?p=qemu.git;a=blob_plain;f=docs/qmp-events.txt;hb=HEAD) |
| 104 | * [QEMU QMP Spec](http://git.qemu.org/?p=qemu.git;a=blob_plain;f=docs/qmp-spec.txt;hb=HEAD) |