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

Improve VM shutdown (from Incus) #13875

Merged
merged 3 commits into from
Sep 7, 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
11 changes: 9 additions & 2 deletions lxd/instance/drivers/driver_qemu_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"io"
"strconv"
"strings"
"syscall"

"golang.org/x/sys/unix"

Expand All @@ -13,6 +15,9 @@ import (
"github.com/canonical/lxd/shared/logger"
)

// ErrExecDisconnected is returned when the guest disconnects the exec session.
var ErrExecDisconnected = fmt.Errorf("Disconnected")

// Cmd represents a running command for an Qemu VM.
type qemuCmd struct {
attachedChildPid int
Expand Down Expand Up @@ -77,8 +82,10 @@ func (c *qemuCmd) Wait() (int, error) {
// Error of type EOF indicates the session ended unexpectedly,
// so we inform the client of the disconnection with a more
// descriptive message.
if errors.Is(err, io.EOF) {
return exitStatus, fmt.Errorf("Disconnected")
// The error can be different depending on why the VM disconnected
// so we handle these cases similarly.
if errors.Is(err, io.EOF) || strings.Contains(err.Error(), io.ErrUnexpectedEOF.Error()) || strings.Contains(err.Error(), syscall.ECONNRESET.Error()) {
return exitStatus, ErrExecDisconnected
}

return exitStatus, err
Expand Down
8 changes: 8 additions & 0 deletions lxd/instance_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/canonical/lxd/lxd/cluster"
"github.com/canonical/lxd/lxd/db/operationtype"
"github.com/canonical/lxd/lxd/instance"
"github.com/canonical/lxd/lxd/instance/drivers"
"github.com/canonical/lxd/lxd/instance/instancetype"
"github.com/canonical/lxd/lxd/operations"
"github.com/canonical/lxd/lxd/request"
Expand Down Expand Up @@ -289,7 +290,14 @@ func (s *execWs) Do(op *operations.Operation) error {
_ = pty.Close()
}

// Make VM disconnections (shutdown/reboot) match containers.
if cmdErr == drivers.ErrExecDisconnected {
cmdResult = 129
Copy link
Member

@mihalicyn mihalicyn Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exit code is 128+SIGHUP (=1) = 129. Looks good to me.

see also, https://tldp.org/LDP/abs/html/exitcodes.html

cmdErr = nil
}

metadata := shared.Jmap{"return": cmdResult}

err = op.ExtendMetadata(metadata)
if err != nil {
return err
Expand Down
Loading