Skip to content

Commit 2f5c0af

Browse files
committed
pause and resume multi-containers
With this patch, `runc pasue` and `runc resume` can pause and resume multi-containers. Signed-off-by: Wang Long <[email protected]>
1 parent a6284a7 commit 2f5c0af

File tree

4 files changed

+132
-11
lines changed

4 files changed

+132
-11
lines changed

man/runc-pause.8.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
runc pause - pause suspends all processes inside the container
33

44
# SYNOPSIS
5-
runc pause <container-id>
5+
runc pause <container-id> [container-id...]
66

77
Where "<container-id>" is the name for the instance of the container to be
88
paused.

man/runc-resume.8.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
runc resume - resumes all processes that have been previously paused
33

44
# SYNOPSIS
5-
runc resume <container-id>
5+
runc resume <container-id> [container-id...]
66

77
Where "<container-id>" is the name for the instance of the container to be
88
resumed.

pause.go

+52-9
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,49 @@
22

33
package main
44

5-
import "github.com/urfave/cli"
5+
import (
6+
"fmt"
7+
"os"
8+
9+
"github.com/urfave/cli"
10+
)
611

712
var pauseCommand = cli.Command{
813
Name: "pause",
914
Usage: "pause suspends all processes inside the container",
10-
ArgsUsage: `<container-id>
15+
ArgsUsage: `<container-id> [container-id...]
1116
1217
Where "<container-id>" is the name for the instance of the container to be
1318
paused. `,
1419
Description: `The pause command suspends all processes in the instance of the container.
1520
1621
Use runc list to identiy instances of containers and their current status.`,
1722
Action: func(context *cli.Context) error {
18-
container, err := getContainer(context)
23+
hasError := false
24+
if !context.Args().Present() {
25+
return fmt.Errorf("runc: \"pause\" requires a minimum of 1 argument")
26+
}
27+
28+
factory, err := loadFactory(context)
1929
if err != nil {
2030
return err
2131
}
22-
if err := container.Pause(); err != nil {
23-
return err
32+
33+
for _, id := range context.Args() {
34+
container, err := factory.Load(id)
35+
if err != nil {
36+
fmt.Fprintf(os.Stderr, "container %s is not exist\n", id)
37+
hasError = true
38+
continue
39+
}
40+
if err := container.Pause(); err != nil {
41+
fmt.Fprintf(os.Stderr, "pause container %s : %s\n", id, err)
42+
hasError = true
43+
}
44+
}
45+
46+
if hasError {
47+
return fmt.Errorf("one or more of container pause failed")
2448
}
2549
return nil
2650
},
@@ -29,20 +53,39 @@ Use runc list to identiy instances of containers and their current status.`,
2953
var resumeCommand = cli.Command{
3054
Name: "resume",
3155
Usage: "resumes all processes that have been previously paused",
32-
ArgsUsage: `<container-id>
56+
ArgsUsage: `<container-id> [container-id...]
3357
3458
Where "<container-id>" is the name for the instance of the container to be
3559
resumed.`,
3660
Description: `The resume command resumes all processes in the instance of the container.
3761
3862
Use runc list to identiy instances of containers and their current status.`,
3963
Action: func(context *cli.Context) error {
40-
container, err := getContainer(context)
64+
hasError := false
65+
if !context.Args().Present() {
66+
return fmt.Errorf("runc: \"resume\" requires a minimum of 1 argument")
67+
}
68+
69+
factory, err := loadFactory(context)
4170
if err != nil {
4271
return err
4372
}
44-
if err := container.Resume(); err != nil {
45-
return err
73+
74+
for _, id := range context.Args() {
75+
container, err := factory.Load(id)
76+
if err != nil {
77+
fmt.Fprintf(os.Stderr, "container %s is not exist\n", id)
78+
hasError = true
79+
continue
80+
}
81+
if err := container.Resume(); err != nil {
82+
fmt.Fprintf(os.Stderr, "resume container %s : %s\n", id, err)
83+
hasError = true
84+
}
85+
}
86+
87+
if hasError {
88+
return fmt.Errorf("one or more of container resume failed")
4689
}
4790
return nil
4891
},

tests/integration/pause.bats

+78
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,81 @@ function teardown() {
3232
# test state of busybox is back to running
3333
testcontainer test_busybox running
3434
}
35+
36+
@test "runc pause and resume with multi-container" {
37+
# run test_busybox1 detached
38+
runc run -d --console /dev/pts/ptmx test_busybox1
39+
[ "$status" -eq 0 ]
40+
41+
wait_for_container 15 1 test_busybox1
42+
43+
# run test_busybox2 detached
44+
runc run -d --console /dev/pts/ptmx test_busybox2
45+
[ "$status" -eq 0 ]
46+
47+
wait_for_container 15 1 test_busybox2
48+
49+
# pause test_busybox1 and test_busybox2
50+
runc pause test_busybox1 test_busybox2
51+
[ "$status" -eq 0 ]
52+
53+
# test state of test_busybox1 and test_busybox2 is paused
54+
testcontainer test_busybox1 paused
55+
testcontainer test_busybox2 paused
56+
57+
# resume test_busybox1 and test_busybox2
58+
runc resume test_busybox1 test_busybox2
59+
[ "$status" -eq 0 ]
60+
61+
# test state of two containers is back to running
62+
testcontainer test_busybox1 running
63+
testcontainer test_busybox2 running
64+
65+
# delete test_busybox1 and test_busybox2
66+
runc delete --force test_busybox1 test_busybox2
67+
68+
runc state test_busybox1
69+
[ "$status" -ne 0 ]
70+
71+
runc state test_busybox2
72+
[ "$status" -ne 0 ]
73+
}
74+
75+
@test "runc pause and resume with nonexist container" {
76+
# run test_busybox1 detached
77+
runc run -d --console /dev/pts/ptmx test_busybox1
78+
[ "$status" -eq 0 ]
79+
80+
wait_for_container 15 1 test_busybox1
81+
82+
# run test_busybox2 detached
83+
runc run -d --console /dev/pts/ptmx test_busybox2
84+
[ "$status" -eq 0 ]
85+
86+
wait_for_container 15 1 test_busybox2
87+
88+
# pause test_busybox1, test_busybox2 and nonexistant container
89+
runc pause test_busybox1 test_busybox2 nonexistant
90+
[ "$status" -ne 0 ]
91+
92+
# test state of test_busybox1 and test_busybox2 is paused
93+
testcontainer test_busybox1 paused
94+
testcontainer test_busybox2 paused
95+
96+
# resume test_busybox1, test_busybox2 and nonexistant container
97+
runc resume test_busybox1 test_busybox2 nonexistant
98+
[ "$status" -ne 0 ]
99+
100+
# test state of two containers is back to running
101+
testcontainer test_busybox1 running
102+
testcontainer test_busybox2 running
103+
104+
# delete test_busybox1 and test_busybox2
105+
runc delete --force test_busybox1 test_busybox2
106+
107+
runc state test_busybox1
108+
[ "$status" -ne 0 ]
109+
110+
runc state test_busybox2
111+
[ "$status" -ne 0 ]
112+
}

0 commit comments

Comments
 (0)