Skip to content

Commit 0638598

Browse files
committed
pillar/kvm,qmp: return types.SwState from getQemuStatus(), not a string
Let getQemuStatus() be responsible for proper parsing of QEMU statuses, so the function can return a proper error if status is not expected. Signed-off-by: Roman Penyaev <[email protected]>
1 parent e5f011c commit 0638598

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

pkg/pillar/hypervisor/kvm.go

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ func (ctx KvmContext) Start(domainName string) error {
913913
return logError("failed to start domain that is stopped %v", err)
914914
}
915915

916-
if status, err := getQemuStatus(qmpFile); err != nil || status != "running" {
916+
if status, err := getQemuStatus(qmpFile); err != nil || status != types.RUNNING {
917917
return logError("domain status is not running but %s after cont command returned %v", status, err)
918918
}
919919
return nil
@@ -950,33 +950,13 @@ func (ctx KvmContext) Info(domainName string) (int, types.SwState, error) {
950950
return effectiveDomainID, effectiveDomainState, err
951951
}
952952

953-
// if task us alive, we augment task status with finer grained details from qemu
954-
// lets parse the status according to https://github.com/qemu/qemu/blob/master/qapi/run-state.json#L8
955-
stateMap := map[string]types.SwState{
956-
"finish-migrate": types.PAUSED,
957-
"inmigrate": types.PAUSING,
958-
"paused": types.PAUSED,
959-
"postmigrate": types.PAUSED,
960-
"prelaunch": types.PAUSED,
961-
"restore-vm": types.PAUSED,
962-
"running": types.RUNNING,
963-
"save-vm": types.PAUSED,
964-
"shutdown": types.HALTING,
965-
"suspended": types.PAUSED,
966-
"watchdog": types.PAUSING,
967-
"colo": types.PAUSED,
968-
"preconfig": types.PAUSED,
969-
}
970-
res, err := getQemuStatus(GetQmpExecutorSocket(domainName))
953+
_, err = getQemuStatus(GetQmpExecutorSocket(domainName))
971954
if err != nil {
972-
return effectiveDomainID, types.BROKEN, logError("couldn't retrieve status for domain %s: %v", domainName, err)
955+
return effectiveDomainID, types.BROKEN,
956+
logError("couldn't retrieve status for domain %s: %v", domainName, err)
973957
}
974958

975-
if effectiveDomainState, matched := stateMap[res]; !matched {
976-
return effectiveDomainID, types.BROKEN, logError("domain %s reported to be in unexpected state %s", domainName, res)
977-
} else {
978-
return effectiveDomainID, effectiveDomainState, nil
979-
}
959+
return effectiveDomainID, effectiveDomainState, nil
980960
}
981961

982962
// Cleanup cleans up a domain

pkg/pillar/hypervisor/qmp.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"github.com/digitalocean/go-qemu/qmp"
9+
"github.com/lf-edge/eve/pkg/pillar/types"
910
"github.com/sirupsen/logrus"
1011
"os"
1112
"time"
@@ -82,7 +83,25 @@ func QmpExecDeviceAdd(socket, id string, busnum, devnum uint16) error {
8283
return err
8384
}
8485

85-
func getQemuStatus(socket string) (string, error) {
86+
func getQemuStatus(socket string) (types.SwState, error) {
87+
// lets parse the status according to
88+
// https://github.com/qemu/qemu/blob/master/qapi/run-state.json#L8
89+
qmpStatusMap := map[string]types.SwState{
90+
"finish-migrate": types.PAUSED,
91+
"inmigrate": types.PAUSING,
92+
"paused": types.PAUSED,
93+
"postmigrate": types.PAUSED,
94+
"prelaunch": types.PAUSED,
95+
"restore-vm": types.PAUSED,
96+
"running": types.RUNNING,
97+
"save-vm": types.PAUSED,
98+
"shutdown": types.HALTING,
99+
"suspended": types.PAUSED,
100+
"watchdog": types.PAUSING,
101+
"colo": types.PAUSED,
102+
"preconfig": types.PAUSED,
103+
}
104+
86105
if raw, err := execRawCmd(socket, `{ "execute": "query-status" }`); err == nil {
87106
var result struct {
88107
ID string `json:"id"`
@@ -95,14 +114,17 @@ func getQemuStatus(socket string) (string, error) {
95114
dec := json.NewDecoder(bytes.NewReader(raw))
96115
dec.DisallowUnknownFields()
97116
err = dec.Decode(&result)
117+
var matched bool
118+
var state types.SwState
98119
if err != nil {
99120
err = fmt.Errorf("%v; (JSON received: '%s')", err, raw)
100-
} else if result.Return.Status == "" {
101-
err = fmt.Errorf("'status' is empty; (JSON received: '%s')", raw)
121+
} else if state, matched = qmpStatusMap[result.Return.Status]; !matched {
122+
err = fmt.Errorf("unknown QMP status '%s' for QMP socket '%s'; (JSON response: '%s')",
123+
result.Return.Status, socket, raw)
102124
}
103-
return result.Return.Status, err
125+
return state, err
104126
} else {
105-
return "", err
127+
return types.UNKNOWN, err
106128
}
107129
}
108130

0 commit comments

Comments
 (0)