Fix the pid-file option for runc run/exec/create command
Signed-off-by: Wang Long <long.wanglong@huawei.com>
This commit is contained in:
parent
bc462c96bf
commit
8676c75442
|
@ -54,6 +54,9 @@ command(s) that get executed on start, edit the args parameter of the spec. See
|
||||||
cli.ShowCommandHelp(context, "create")
|
cli.ShowCommandHelp(context, "create")
|
||||||
return fmt.Errorf("runc: \"create\" requires exactly one argument")
|
return fmt.Errorf("runc: \"create\" requires exactly one argument")
|
||||||
}
|
}
|
||||||
|
if err := revisePidFile(context); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
spec, err := setupSpec(context)
|
spec, err := setupSpec(context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
3
exec.go
3
exec.go
|
@ -89,6 +89,9 @@ following will output a list of processes running in the container:
|
||||||
if os.Geteuid() != 0 {
|
if os.Geteuid() != 0 {
|
||||||
return fmt.Errorf("runc should be run as root")
|
return fmt.Errorf("runc should be run as root")
|
||||||
}
|
}
|
||||||
|
if err := revisePidFile(context); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
status, err := execProcess(context)
|
status, err := execProcess(context)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
os.Exit(status)
|
os.Exit(status)
|
||||||
|
|
3
run.go
3
run.go
|
@ -59,6 +59,9 @@ command(s) that get executed on start, edit the args parameter of the spec. See
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(context *cli.Context) error {
|
Action: func(context *cli.Context) error {
|
||||||
|
if err := revisePidFile(context); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
spec, err := setupSpec(context)
|
spec, err := setupSpec(context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -59,3 +59,29 @@ function teardown() {
|
||||||
|
|
||||||
testcontainer test_busybox running
|
testcontainer test_busybox running
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "runc create --pid-file with new CWD" {
|
||||||
|
# create pid_file directory as the CWD
|
||||||
|
run mkdir pid_file
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
run cd pid_file
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
|
||||||
|
runc create --pid-file pid.txt -b $BUSYBOX_BUNDLE --console /dev/pts/ptmx test_busybox
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
|
||||||
|
testcontainer test_busybox created
|
||||||
|
|
||||||
|
# check pid.txt was generated
|
||||||
|
[ -e pid.txt ]
|
||||||
|
|
||||||
|
run cat pid.txt
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[[ ${lines[0]} == $(__runc state test_busybox | jq '.pid') ]]
|
||||||
|
|
||||||
|
# start the command
|
||||||
|
runc start test_busybox
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
|
||||||
|
testcontainer test_busybox running
|
||||||
|
}
|
||||||
|
|
|
@ -45,6 +45,33 @@ function teardown() {
|
||||||
[[ ${lines[0]} != $(__runc state test_busybox | jq '.pid') ]]
|
[[ ${lines[0]} != $(__runc state test_busybox | jq '.pid') ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "runc exec --pid-file with new CWD" {
|
||||||
|
# create pid_file directory as the CWD
|
||||||
|
run mkdir pid_file
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
run cd pid_file
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
|
||||||
|
# run busybox detached
|
||||||
|
runc run -d -b $BUSYBOX_BUNDLE --console /dev/pts/ptmx test_busybox
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
|
||||||
|
wait_for_container 15 1 test_busybox
|
||||||
|
|
||||||
|
runc exec --pid-file pid.txt test_busybox echo Hello from exec
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
echo text echoed = "'""${output}""'"
|
||||||
|
[[ "${output}" == *"Hello from exec"* ]]
|
||||||
|
|
||||||
|
# check pid.txt was generated
|
||||||
|
[ -e pid.txt ]
|
||||||
|
|
||||||
|
run cat pid.txt
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[[ ${lines[0]} =~ [0-9]+ ]]
|
||||||
|
[[ ${lines[0]} != $(__runc state test_busybox | jq '.pid') ]]
|
||||||
|
}
|
||||||
|
|
||||||
@test "runc exec ls -la" {
|
@test "runc exec ls -la" {
|
||||||
# run busybox detached
|
# run busybox detached
|
||||||
runc run -d --console /dev/pts/ptmx test_busybox
|
runc run -d --console /dev/pts/ptmx test_busybox
|
||||||
|
|
|
@ -55,3 +55,27 @@ function teardown() {
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
[[ ${lines[0]} == $(__runc state test_busybox | jq '.pid') ]]
|
[[ ${lines[0]} == $(__runc state test_busybox | jq '.pid') ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "runc run detached --pid-file with new CWD" {
|
||||||
|
# create pid_file directory as the CWD
|
||||||
|
run mkdir pid_file
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
run cd pid_file
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
|
||||||
|
# run busybox detached
|
||||||
|
runc run --pid-file pid.txt -d -b $BUSYBOX_BUNDLE --console /dev/pts/ptmx test_busybox
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
|
||||||
|
# check state
|
||||||
|
wait_for_container 15 1 test_busybox
|
||||||
|
|
||||||
|
testcontainer test_busybox running
|
||||||
|
|
||||||
|
# check pid.txt was generated
|
||||||
|
[ -e pid.txt ]
|
||||||
|
|
||||||
|
run cat pid.txt
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[[ ${lines[0]} == $(__runc state test_busybox | jq '.pid') ]]
|
||||||
|
}
|
||||||
|
|
20
utils.go
20
utils.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/opencontainers/runtime-spec/specs-go"
|
"github.com/opencontainers/runtime-spec/specs-go"
|
||||||
|
@ -39,3 +40,22 @@ func setupSpec(context *cli.Context) (*specs.Spec, error) {
|
||||||
}
|
}
|
||||||
return spec, nil
|
return spec, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func revisePidFile(context *cli.Context) error {
|
||||||
|
pidFile := context.String("pid-file")
|
||||||
|
if pidFile == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// convert pid-file to an absolute path so we can write to the right
|
||||||
|
// file after chdir to bundle
|
||||||
|
pidFile, err := filepath.Abs(pidFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = context.Set("pid-file", pidFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue