Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtcontainers: add method to get hypervisor PID
Browse files Browse the repository at this point in the history
hypervisor PID can be used to move the whole process and its
threads into a new cgroup.

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Feb 14, 2019
1 parent 136b188 commit a1c8590
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions virtcontainers/fc.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,3 +702,7 @@ func (fc *firecracker) getThreadIDs() (*threadIDs, error) {
func (fc *firecracker) cleanup() error {
return nil
}

func (fc *firecracker) pid() int {
return fc.info.PID
}
1 change: 1 addition & 0 deletions virtcontainers/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,4 +609,5 @@ type hypervisor interface {
hypervisorConfig() HypervisorConfig
getThreadIDs() (*threadIDs, error)
cleanup() error
pid() int
}
5 changes: 5 additions & 0 deletions virtcontainers/mock_hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

type mockHypervisor struct {
mockPid int
}

func (m *mockHypervisor) capabilities() types.Capabilities {
Expand Down Expand Up @@ -100,3 +101,7 @@ func (m *mockHypervisor) getThreadIDs() (*threadIDs, error) {
func (m *mockHypervisor) cleanup() error {
return nil
}

func (m *mockHypervisor) pid() int {
return m.mockPid
}
24 changes: 24 additions & 0 deletions virtcontainers/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package virtcontainers
import (
"context"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -500,6 +501,8 @@ func (q *qemu) createSandbox(ctx context.Context, id string, hypervisorConfig *H
return err
}

pidFile := q.pidFile()

qemuConfig := govmmQemu.Config{
Name: fmt.Sprintf("sandbox-%s", q.id),
UUID: q.state.UUID,
Expand All @@ -518,6 +521,7 @@ func (q *qemu) createSandbox(ctx context.Context, id string, hypervisorConfig *H
VGA: "none",
GlobalParam: "kvm-pit.lost_tick_policy=discard",
Bios: firmwarePath,
PidFile: pidFile,
}

if ioThread != nil {
Expand Down Expand Up @@ -1567,3 +1571,23 @@ func (q *qemu) cleanup() error {

return nil
}

func (q *qemu) pidFile() string {
return filepath.Join(store.RunVMStoragePath, q.id, "pid")
}

func (q *qemu) pid() int {
data, err := ioutil.ReadFile(q.pidFile())
if err != nil {
q.Logger().WithError(err).Error("Could not read qemu pid file")
return 0
}

pid, err := strconv.Atoi(strings.Trim(string(data), "\n\t "))
if err != nil {
q.Logger().WithError(err).Error("Could not convert string to int")
return 0
}

return pid
}

0 comments on commit a1c8590

Please sign in to comment.