Skip to content

Commit 9b53c40

Browse files
nanjjstgraber
authored andcommitted
incusd/instance/qmp: Add command ID to RunJSON
Signed-off-by: JUN JIE NAN <nanjunjie@gmail.com>
1 parent f084fbd commit 9b53c40

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

internal/server/instance/drivers/driver_qemu.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,8 +1221,17 @@ func (d *qemu) startupHook(monitor *qmp.Monitor, stage string) error {
12211221
}
12221222

12231223
for _, command := range commandList {
1224-
jsonCommand, _ := json.Marshal(command)
1225-
err = monitor.RunJSON(jsonCommand, nil, true)
1224+
id := monitor.IncreaseID()
1225+
command["id"] = id
1226+
1227+
var jsonCommand []byte
1228+
jsonCommand, err = json.Marshal(command)
1229+
if err != nil {
1230+
err = fmt.Errorf("Failed to marshal command at %s stage: %w", stage, err)
1231+
return err
1232+
}
1233+
1234+
err = monitor.RunJSON(jsonCommand, nil, true, id)
12261235
if err != nil {
12271236
err = fmt.Errorf("Failed to run QMP command %s at %s stage: %w", jsonCommand, stage, err)
12281237
return err

internal/server/instance/drivers/qmp/monitor.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (m *Monitor) ping() error {
193193
}
194194

195195
// RunJSON executes a JSON-formatted command.
196-
func (m *Monitor) RunJSON(request []byte, resp any, logCommand bool) error {
196+
func (m *Monitor) RunJSON(request []byte, resp any, logCommand bool, id uint32) error {
197197
// Check if disconnected
198198
if m.disconnected {
199199
return ErrMonitorDisconnect
@@ -215,7 +215,7 @@ func (m *Monitor) RunJSON(request []byte, resp any, logCommand bool) error {
215215
}
216216
}
217217

218-
out, err := m.qmp.run(request, qmpZeroKey)
218+
out, err := m.qmp.run(request, id)
219219
if err != nil {
220220
// Confirm the daemon didn't die.
221221
errPing := m.ping()
@@ -250,13 +250,18 @@ func (m *Monitor) RunJSON(request []byte, resp any, logCommand bool) error {
250250
return nil
251251
}
252252

253+
// IncreaseID returns on auto increment uint32 id.
254+
func (m *Monitor) IncreaseID() uint32 {
255+
return m.qmp.qmpIncreaseID()
256+
}
257+
253258
// run executes a command.
254259
func (m *Monitor) Run(cmd string, args any, resp any) error {
260+
id := m.IncreaseID()
261+
255262
// Construct the command.
256-
requestArgs := struct {
257-
Execute string `json:"execute"`
258-
Arguments any `json:"arguments,omitempty"`
259-
}{
263+
requestArgs := qmpCommand{
264+
ID: id,
260265
Execute: cmd,
261266
Arguments: args,
262267
}
@@ -267,7 +272,7 @@ func (m *Monitor) Run(cmd string, args any, resp any) error {
267272
}
268273

269274
logCommand := !slices.Contains(ExcludedCommands, cmd)
270-
return m.RunJSON(request, resp, logCommand)
275+
return m.RunJSON(request, resp, logCommand, id)
271276
}
272277

273278
// Connect creates or retrieves an existing QMP monitor for the path.

internal/server/scriptlet/qemu.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,21 @@ func QEMURun(l logger.Logger, instance *api.Instance, cmdArgs *[]string, conf *[
9292
return nil, err
9393
}
9494

95+
id := uint32(0)
96+
req, ok := value.(map[string]any)
97+
if ok {
98+
id = m.IncreaseID()
99+
req["id"] = id
100+
value = req
101+
}
102+
95103
request, err := json.Marshal(value)
96104
if err != nil {
97105
return nil, err
98106
}
99107

100108
var resp map[string]any
101-
err = m.RunJSON(request, &resp, true)
109+
err = m.RunJSON(request, &resp, true, id)
102110
if err != nil {
103111
return nil, err
104112
}

0 commit comments

Comments
 (0)