mirror of
https://github.com/containers/podman.git
synced 2025-07-03 17:27:18 +08:00
Add support for pid ns
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com> Closes: #54 Approved by: umohnani8
This commit is contained in:

committed by
Atomic Bot

parent
2a3934f1da
commit
bd4e106de3
@ -6,6 +6,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/go-units"
|
"github.com/docker/go-units"
|
||||||
"github.com/opencontainers/selinux/go-selinux/label"
|
"github.com/opencontainers/selinux/go-selinux/label"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -61,6 +62,7 @@ type createResourceConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type createConfig struct {
|
type createConfig struct {
|
||||||
|
runtime *libpod.Runtime
|
||||||
args []string
|
args []string
|
||||||
capAdd []string // cap-add
|
capAdd []string // cap-add
|
||||||
capDrop []string // cap-drop
|
capDrop []string // cap-drop
|
||||||
@ -90,8 +92,8 @@ type createConfig struct {
|
|||||||
network string //network
|
network string //network
|
||||||
networkAlias []string //network-alias
|
networkAlias []string //network-alias
|
||||||
nsIPC string // ipc
|
nsIPC string // ipc
|
||||||
nsNet string //net
|
nsNET string //net
|
||||||
nsPID string //pid
|
pidMode container.PidMode //pid
|
||||||
nsUser string
|
nsUser string
|
||||||
pod string //pod
|
pod string //pod
|
||||||
privileged bool //privileged
|
privileged bool //privileged
|
||||||
@ -329,8 +331,13 @@ func parseCreateOpts(c *cli.Context, runtime *libpod.Runtime) (*createConfig, er
|
|||||||
if !c.Bool("detach") && !tty {
|
if !c.Bool("detach") && !tty {
|
||||||
tty = true
|
tty = true
|
||||||
}
|
}
|
||||||
|
pidMode := container.PidMode(c.String("pid"))
|
||||||
|
if !pidMode.Valid() {
|
||||||
|
return nil, errors.Errorf("--pid %q is not valid", c.String("pid"))
|
||||||
|
}
|
||||||
|
|
||||||
config := &createConfig{
|
config := &createConfig{
|
||||||
|
runtime: runtime,
|
||||||
capAdd: c.StringSlice("cap-add"),
|
capAdd: c.StringSlice("cap-add"),
|
||||||
capDrop: c.StringSlice("cap-drop"),
|
capDrop: c.StringSlice("cap-drop"),
|
||||||
cgroupParent: c.String("cgroup-parent"),
|
cgroupParent: c.String("cgroup-parent"),
|
||||||
@ -357,8 +364,8 @@ func parseCreateOpts(c *cli.Context, runtime *libpod.Runtime) (*createConfig, er
|
|||||||
network: c.String("network"),
|
network: c.String("network"),
|
||||||
networkAlias: c.StringSlice("network-alias"),
|
networkAlias: c.StringSlice("network-alias"),
|
||||||
nsIPC: c.String("ipc"),
|
nsIPC: c.String("ipc"),
|
||||||
nsNet: c.String("net"),
|
nsNET: c.String("net"),
|
||||||
nsPID: c.String("pid"),
|
pidMode: pidMode,
|
||||||
pod: c.String("pod"),
|
pod: c.String("pod"),
|
||||||
privileged: c.Bool("privileged"),
|
privileged: c.Bool("privileged"),
|
||||||
publish: c.StringSlice("publish"),
|
publish: c.StringSlice("publish"),
|
||||||
|
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -44,6 +45,28 @@ func blockAccessToKernelFilesystems(config *createConfig, g *generate.Generator)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addPidNS(config *createConfig, g *generate.Generator) error {
|
||||||
|
pidMode := config.pidMode
|
||||||
|
if pidMode.IsHost() {
|
||||||
|
return g.RemoveLinuxNamespace("pid")
|
||||||
|
}
|
||||||
|
if pidMode.IsContainer() {
|
||||||
|
ctr, err := config.runtime.LookupContainer(pidMode.Container())
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "container %q not found", pidMode.Container())
|
||||||
|
}
|
||||||
|
pid, err := ctr.PID()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "Failed to get pid of container %q", pidMode.Container())
|
||||||
|
}
|
||||||
|
pidNsPath := fmt.Sprintf("/proc/%d/ns/pid", pid)
|
||||||
|
if err := g.AddOrReplaceLinuxNamespace(libpod.PIDNamespace, pidNsPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func addRlimits(config *createConfig, g *generate.Generator) error {
|
func addRlimits(config *createConfig, g *generate.Generator) error {
|
||||||
var (
|
var (
|
||||||
ul *units.Ulimit
|
ul *units.Ulimit
|
||||||
@ -182,6 +205,10 @@ func createConfigToOCISpec(config *createConfig) (*spec.Spec, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := addPidNS(config, &g); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
configSpec := g.Spec()
|
configSpec := g.Spec()
|
||||||
|
|
||||||
if config.seccompProfilePath != "" && config.seccompProfilePath != "unconfined" {
|
if config.seccompProfilePath != "" && config.seccompProfilePath != "unconfined" {
|
||||||
|
28
test/kpod_run_ns.bats
Normal file
28
test/kpod_run_ns.bats
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#!/usr/bin/env bats
|
||||||
|
|
||||||
|
load helpers
|
||||||
|
|
||||||
|
function setup() {
|
||||||
|
copy_images
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "run pidns test" {
|
||||||
|
|
||||||
|
${KPOD_BINARY} ${KPOD_OPTIONS} pull ${ALPINE}
|
||||||
|
|
||||||
|
run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} run ${ALPINE} sh -c 'echo \$\$'"
|
||||||
|
echo $output
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
pid=$(echo $output | tr -d '\r')
|
||||||
|
[ $pid = "1" ]
|
||||||
|
|
||||||
|
run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} run --pid=host ${ALPINE} sh -c 'echo \$\$'"
|
||||||
|
echo $output
|
||||||
|
pid=$(echo $output | tr -d '\r')
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ $pid != "1" ]
|
||||||
|
|
||||||
|
run ${KPOD_BINARY} ${KPOD_OPTIONS} run --pid=badpid ${ALPINE} sh -c 'echo $$'
|
||||||
|
echo $output
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
}
|
Reference in New Issue
Block a user