Skip to content

Commit

Permalink
Merge pull request kata-containers#429 from bergwolf/qmp
Browse files Browse the repository at this point in the history
qemu: clean up qmp channel
  • Loading branch information
Sebastien Boeuf authored Jun 20, 2018
2 parents d02babd + 8f329db commit fca7eb8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 73 deletions.
71 changes: 9 additions & 62 deletions virtcontainers/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package virtcontainers

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -49,7 +48,6 @@ type qemu struct {
config HypervisorConfig

qmpMonitorCh qmpChannel
qmpControlCh qmpChannel

qemuConfig govmmQemu.Config

Expand All @@ -62,6 +60,8 @@ type qemu struct {

const qmpCapErrMsg = "Failed to negoatiate QMP capabilities"

const qmpSocket = "qmp.sock"

const defaultConsole = "console.sock"

// agnostic list of kernel parameters
Expand Down Expand Up @@ -223,50 +223,8 @@ func (q *qemu) memoryTopology(sandboxConfig SandboxConfig) (govmmQemu.Memory, er
return q.arch.memoryTopology(memMb, hostMemMb), nil
}

func (q *qemu) qmpSocketPath(socketName string) (string, error) {
if socketName == "" {
return "", errors.New("need socket name")
}

parentDirPath := filepath.Join(runStoragePath, q.sandbox.id)

dir, err := utils.BuildSocketPath(parentDirPath)
if err != nil {
return "", err
}

name := fmt.Sprintf("%s-%s", socketName, q.state.UUID)

path, err := utils.BuildSocketPath(dir, name)
if err == nil {
return path, nil
}

// The socket path is too long so truncate up to a minimum length.

// The minimum path length we're prepared to use (based on current
// values)
const minNameLen = 12

dirLen := len(dir)

// '-1' is for the addition of a path separator
availableNameLen := utils.MaxSocketPathLen - dirLen - 1

if availableNameLen < minNameLen {
return "", fmt.Errorf("QMP socket name cannot be shortened: %v", name)
}

new := name[:availableNameLen]

q.Logger().WithFields(logrus.Fields{
"original-name": name,
"new-name": new,
}).Warnf("shortening QMP socket name")

name = new

return utils.BuildSocketPath(dir, name)
func (q *qemu) qmpSocketPath(sandboxID string) (string, error) {
return utils.BuildSocketPath(runStoragePath, sandboxID, qmpSocket)
}

func (q *qemu) getQemuMachine(sandboxConfig SandboxConfig) (govmmQemu.Machine, error) {
Expand Down Expand Up @@ -354,7 +312,7 @@ func (q *qemu) createSandbox(sandboxConfig SandboxConfig) error {
return fmt.Errorf("UUID should not be empty")
}

monitorSockPath, err := q.qmpSocketPath(monitorSocket)
monitorSockPath, err := q.qmpSocketPath(sandboxConfig.ID)
if err != nil {
return err
}
Expand All @@ -364,29 +322,18 @@ func (q *qemu) createSandbox(sandboxConfig SandboxConfig) error {
path: monitorSockPath,
}

controlSockPath, err := q.qmpSocketPath(controlSocket)
err = os.MkdirAll(filepath.Dir(monitorSockPath), dirMode)
if err != nil {
return err
}

q.qmpControlCh = qmpChannel{
ctx: context.Background(),
path: controlSockPath,
}

qmpSockets := []govmmQemu.QMPSocket{
{
Type: "unix",
Name: q.qmpMonitorCh.path,
Server: true,
NoWait: true,
},
{
Type: "unix",
Name: q.qmpControlCh.path,
Server: true,
NoWait: true,
},
}

// Add bridges before any other devices. This way we make sure that
Expand Down Expand Up @@ -531,7 +478,7 @@ func (q *qemu) stopSandbox() error {
disconnectCh := make(chan struct{})

q.Logger().Info("Stopping Sandbox")
qmp, _, err := govmmQemu.QMPStart(q.qmpControlCh.ctx, q.qmpControlCh.path, cfg, disconnectCh)
qmp, _, err := govmmQemu.QMPStart(q.qmpMonitorCh.ctx, q.qmpMonitorCh.path, cfg, disconnectCh)
if err != nil {
q.Logger().WithError(err).Error("Failed to connect to QEMU instance")
return err
Expand All @@ -558,7 +505,7 @@ func (q *qemu) togglePauseSandbox(pause bool) error {
// Auto-closed by QMPStart().
disconnectCh := make(chan struct{})

qmp, _, err := govmmQemu.QMPStart(q.qmpControlCh.ctx, q.qmpControlCh.path, cfg, disconnectCh)
qmp, _, err := govmmQemu.QMPStart(q.qmpMonitorCh.ctx, q.qmpMonitorCh.path, cfg, disconnectCh)
if err != nil {
q.Logger().WithError(err).Error("Failed to connect to QEMU instance")
return err
Expand Down Expand Up @@ -591,7 +538,7 @@ func (q *qemu) qmpSetup() (*govmmQemu.QMP, error) {
// Auto-closed by QMPStart().
disconnectCh := make(chan struct{})

qmp, _, err := govmmQemu.QMPStart(q.qmpControlCh.ctx, q.qmpControlCh.path, cfg, disconnectCh)
qmp, _, err := govmmQemu.QMPStart(q.qmpMonitorCh.ctx, q.qmpMonitorCh.path, cfg, disconnectCh)
if err != nil {
q.Logger().WithError(err).Error("Failed to connect to QEMU instance")
return nil, err
Expand Down
11 changes: 0 additions & 11 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ import (
deviceManager "github.com/kata-containers/runtime/virtcontainers/device/manager"
)

// controlSocket is the sandbox control socket.
// It is an hypervisor resource, and for example qemu's control
// socket is the QMP one.
const controlSocket = "ctl"

// monitorSocket is the sandbox monitoring socket.
// It is an hypervisor resource, and is a qmp socket in the qemu case.
// This is a socket that any monitoring entity will listen to in order
// to understand if the VM is still alive or not.
const monitorSocket = "mon"

// vmStartTimeout represents the time in seconds a sandbox can wait before
// to consider the VM starting operation failed.
const vmStartTimeout = 10
Expand Down

0 comments on commit fca7eb8

Please sign in to comment.