Skip to content

Commit 54c6636

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 8e9e795 commit 54c6636

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
@@ -777,7 +777,7 @@ func (ctx kvmContext) Start(domainName string) error {
777777
return logError("failed to start domain that is stopped %v", err)
778778
}
779779

780-
if status, err := getQemuStatus(qmpFile); err != nil || status != "running" {
780+
if status, err := getQemuStatus(qmpFile); err != nil || status != types.RUNNING {
781781
return logError("domain status is not running but %s after cont command returned %v", status, err)
782782
}
783783
return nil
@@ -811,33 +811,13 @@ func (ctx kvmContext) Info(domainName string) (int, types.SwState, error) {
811811
return effectiveDomainID, effectiveDomainState, err
812812
}
813813

814-
// if task us alive, we augment task status with finer grained details from qemu
815-
// lets parse the status according to https://github.com/qemu/qemu/blob/master/qapi/run-state.json#L8
816-
stateMap := map[string]types.SwState{
817-
"finish-migrate": types.PAUSED,
818-
"inmigrate": types.PAUSING,
819-
"paused": types.PAUSED,
820-
"postmigrate": types.PAUSED,
821-
"prelaunch": types.PAUSED,
822-
"restore-vm": types.PAUSED,
823-
"running": types.RUNNING,
824-
"save-vm": types.PAUSED,
825-
"shutdown": types.HALTING,
826-
"suspended": types.PAUSED,
827-
"watchdog": types.PAUSING,
828-
"colo": types.PAUSED,
829-
"preconfig": types.PAUSED,
830-
}
831-
res, err := getQemuStatus(getQmpExecutorSocket(domainName))
814+
_, err = getQemuStatus(getQmpExecutorSocket(domainName))
832815
if err != nil {
833-
return effectiveDomainID, types.BROKEN, logError("couldn't retrieve status for domain %s: %v", domainName, err)
816+
return effectiveDomainID, types.BROKEN,
817+
logError("couldn't retrieve status for domain %s: %v", domainName, err)
834818
}
835819

836-
if effectiveDomainState, matched := stateMap[res]; !matched {
837-
return effectiveDomainID, types.BROKEN, logError("domain %s reported to be in unexpected state %s", domainName, res)
838-
} else {
839-
return effectiveDomainID, effectiveDomainState, nil
840-
}
820+
return effectiveDomainID, effectiveDomainState, nil
841821
}
842822

843823
func (ctx kvmContext) Cleanup(domainName string) error {

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"
@@ -68,7 +69,25 @@ func execVNCPassword(socket string, password string) error {
6869
return err
6970
}
7071

71-
func getQemuStatus(socket string) (string, error) {
72+
func getQemuStatus(socket string) (types.SwState, error) {
73+
// lets parse the status according to
74+
// https://github.com/qemu/qemu/blob/master/qapi/run-state.json#L8
75+
qmpStatusMap := map[string]types.SwState{
76+
"finish-migrate": types.PAUSED,
77+
"inmigrate": types.PAUSING,
78+
"paused": types.PAUSED,
79+
"postmigrate": types.PAUSED,
80+
"prelaunch": types.PAUSED,
81+
"restore-vm": types.PAUSED,
82+
"running": types.RUNNING,
83+
"save-vm": types.PAUSED,
84+
"shutdown": types.HALTING,
85+
"suspended": types.PAUSED,
86+
"watchdog": types.PAUSING,
87+
"colo": types.PAUSED,
88+
"preconfig": types.PAUSED,
89+
}
90+
7291
if raw, err := execRawCmd(socket, `{ "execute": "query-status" }`); err == nil {
7392
var result struct {
7493
ID string `json:"id"`
@@ -81,14 +100,17 @@ func getQemuStatus(socket string) (string, error) {
81100
dec := json.NewDecoder(bytes.NewReader(raw))
82101
dec.DisallowUnknownFields()
83102
err = dec.Decode(&result)
103+
var matched bool
104+
var state types.SwState
84105
if err != nil {
85106
err = fmt.Errorf("%v; (JSON received: '%s')", err, raw)
86-
} else if result.Return.Status == "" {
87-
err = fmt.Errorf("'status' is empty; (JSON received: '%s')", raw)
107+
} else if state, matched = qmpStatusMap[result.Return.Status]; !matched {
108+
err = fmt.Errorf("unknown QMP status '%s' for QMP socket '%s'; (JSON response: '%s')",
109+
result.Return.Status, socket, raw)
88110
}
89-
return result.Return.Status, err
111+
return state, err
90112
} else {
91-
return "", err
113+
return types.UNKNOWN, err
92114
}
93115
}
94116

0 commit comments

Comments
 (0)