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

VM console log fixes #1255

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion internal/server/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -9346,7 +9346,7 @@ func (d *qemu) postCPUHotplug(monitor *qmp.Monitor) error {
// ConsoleLog returns all output sent to the instance's console's ring buffer since startup.
func (d *qemu) ConsoleLog() (string, error) {
// Setup a new operation.
op, err := operationlock.Create(d.Project().Name, d.Name(), d.op, operationlock.ActionConsoleRetrieve, false, false)
op, err := operationlock.CreateWaitGet(d.Project().Name, d.Name(), d.op, operationlock.ActionConsoleRetrieve, []operationlock.Action{operationlock.ActionRestart, operationlock.ActionRestore, operationlock.ActionMigrate}, false, true)
if err != nil {
return "", err
}
Expand All @@ -9361,6 +9361,13 @@ func (d *qemu) ConsoleLog() (string, error) {

logString, err := monitor.RingbufRead("console")
if err != nil {
// If a VM was started by an older version of Incus which was then upgraded, its
// console device won't be a ring buffer. We don't want to cause an error in this
// case, so just return an empty string.
if errors.Is(err, qmp.ErrNotARingbuf) {
return "", nil
}

return "", err
}

Expand Down
39 changes: 35 additions & 4 deletions internal/server/instance/drivers/qmp/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,37 @@ func (m *Monitor) CheckPCIDevice(deviceID string) (bool, error) {

// RingbufRead returns the complete contents of the specified ring buffer.
func (m *Monitor) RingbufRead(device string) (string, error) {
// Begin by ensuring the device specified is actually a ring buffer.
var queryResp struct {
Return []struct {
Label string `json:"label"`
Filename string `json:"filename"`
FrontendOpen bool `json:"frontend_open"`
} `json:"return"`
}

err := m.run("query-chardev", nil, &queryResp)
if err != nil {
return "", err
}

deviceFound := true
for _, qemuDevice := range queryResp.Return {
if qemuDevice.Label == device {
deviceFound = true
if qemuDevice.Filename != "ringbuf" {
// Can't call `ringbuf-read` on a non-ringbuf device.
return "", ErrNotARingbuf
}

break
}
}
if !deviceFound {
return "", fmt.Errorf("Specified qemu device %q doesn't exist", device)
}

// Now actually read from the ring buffer.
var args struct {
Device string `json:"device"`
Size int `json:"size"`
Expand All @@ -1160,23 +1191,23 @@ func (m *Monitor) RingbufRead(device string) (string, error) {
args.Device = device
args.Size = 10000

var resp struct {
var readResp struct {
Return string `json:"return"`
}

var sb strings.Builder

for {
err := m.run("ringbuf-read", args, &resp)
err := m.run("ringbuf-read", args, &readResp)
if err != nil {
return "", err
}

if len(resp.Return) == 0 {
if len(readResp.Return) == 0 {
break
}

sb.WriteString(resp.Return)
sb.WriteString(readResp.Return)
}

return sb.String(), nil
Expand Down
5 changes: 4 additions & 1 deletion internal/server/instance/drivers/qmp/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ import (
// ErrMonitorDisconnect is returned when interacting with a disconnected Monitor.
var ErrMonitorDisconnect = fmt.Errorf("Monitor is disconnected")

// ErrMonitorBadConsole is retuned when the requested console doesn't exist.
// ErrMonitorBadConsole is returned when the requested console doesn't exist.
var ErrMonitorBadConsole = fmt.Errorf("Requested console couldn't be found")

// ErrNotARingbuf is returned when the requested device isn't a ring buffer.
var ErrNotARingbuf = fmt.Errorf("Requested device isn't a ring buffer")
Loading