Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix QEMU delete errors #14950

Merged
merged 3 commits into from
Sep 13, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 response")
}
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 response")
}

// 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 response")
spowelljr marked this conversation as resolved.
Show resolved Hide resolved
}
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 response")
}
// 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 response")
}
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 response")
}
if strings.HasPrefix(command, "query-") {
return response.Return, nil
Expand Down