Skip to content

Commit

Permalink
Merge pull request #14950 from spowelljr/fixQEMUDelete
Browse files Browse the repository at this point in the history
Fix QEMU delete errors
  • Loading branch information
spowelljr committed Sep 13, 2022
2 parents 8a960cc + 31dd75b commit 98b27f9
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions pkg/drivers/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,15 +659,15 @@ func (d *Driver) RunQMPCommand(command string) (map[string]interface{}, error) {
// connect to monitor
conn, err := net.Dial("unix", d.monitorPath())
if err != nil {
return nil, err
return nil, errors.Wrap(err, "connect")
}
defer conn.Close()

// initial QMP response
var buf [1024]byte
nr, err := conn.Read(buf[:])
if err != nil {
return nil, err
return nil, errors.Wrap(err, "read initial resp")
}
type qmpInitialResponse struct {
QMP struct {
Expand All @@ -684,9 +684,8 @@ func (d *Driver) RunQMPCommand(command string) (map[string]interface{}, error) {
}

var initialResponse qmpInitialResponse
err = json.Unmarshal(buf[:nr], &initialResponse)
if err != nil {
return nil, err
if err := json.Unmarshal(buf[:nr], &initialResponse); err != nil {
return nil, errors.Wrap(err, "unmarshal initial resp")
}

// run 'qmp_capabilities' to switch to command mode
Expand All @@ -696,22 +695,21 @@ func (d *Driver) RunQMPCommand(command string) (map[string]interface{}, error) {
}
jsonCommand, err := json.Marshal(qmpCommand{Command: "qmp_capabilities"})
if err != nil {
return nil, err
return nil, errors.Wrap(err, "marshal qmp_capabilities")
}
if _, err := conn.Write(jsonCommand); err != nil {
return nil, err
return nil, errors.Wrap(err, "write qmp_capabilities")
}
nr, err = conn.Read(buf[:])
if err != nil {
return nil, err
return nil, errors.Wrap(err, "read qmp_capabilities resp")
}
type qmpResponse struct {
Return map[string]interface{} `json:"return"`
}
var response qmpResponse
err = json.Unmarshal(buf[:nr], &response)
if err != nil {
return nil, err
if err := json.Unmarshal(buf[:nr], &response); err != nil {
return nil, errors.Wrap(err, "unmarshal qmp_capabilities resp")
}
// expecting empty response
if len(response.Return) != 0 {
Expand All @@ -721,18 +719,21 @@ func (d *Driver) RunQMPCommand(command string) (map[string]interface{}, error) {
// { "execute": command }
jsonCommand, err = json.Marshal(qmpCommand{Command: command})
if err != nil {
return nil, err
return nil, errors.Wrap(err, "marshal command")
}
if _, err := conn.Write(jsonCommand); err != nil {
return nil, err
return nil, errors.Wrap(err, "write command")
}
nr, err = conn.Read(buf[:])
if err != nil {
return nil, err
return nil, errors.Wrap(err, "read command resp")
}
err = json.Unmarshal(buf[:nr], &response)
if err != nil {
return nil, err

// Sometimes QEMU returns two JSON objects with the first object being the command response
// and the second object being an event log (unimportant)
firstRespObj := strings.Split(string(buf[:nr]), "\n")[0]
if err := json.Unmarshal([]byte(firstRespObj), &response); err != nil {
return nil, errors.Wrap(err, "unmarshal command resp")
}
if strings.HasPrefix(command, "query-") {
return response.Return, nil
Expand Down

0 comments on commit 98b27f9

Please sign in to comment.