11package main
22
33import (
4+ "context"
45 "errors"
56 "fmt"
67 "net"
@@ -11,13 +12,13 @@ import (
1112 "github.com/moby/sys/userns"
1213 "github.com/opencontainers/runtime-spec/specs-go"
1314 "github.com/sirupsen/logrus"
14- "github.com/urfave/cli"
15+ "github.com/urfave/cli/v3 "
1516 "golang.org/x/sys/unix"
1617
1718 "github.com/opencontainers/runc/libcontainer"
1819)
1920
20- var checkpointCommand = cli.Command {
21+ var checkpointCommand = & cli.Command {
2122 Name : "checkpoint" ,
2223 Usage : "checkpoint a running container" ,
2324 ArgsUsage : `<container-id>
@@ -26,34 +27,34 @@ Where "<container-id>" is the name for the instance of the container to be
2627checkpointed.` ,
2728 Description : `The checkpoint command saves the state of the container instance.` ,
2829 Flags : []cli.Flag {
29- cli.StringFlag {Name : "image-path" , Value : "" , Usage : "path for saving criu image files" },
30- cli.StringFlag {Name : "work-path" , Value : "" , Usage : "path for saving work files and logs" },
31- cli.StringFlag {Name : "parent-path" , Value : "" , Usage : "path for previous criu image files in pre-dump" },
32- cli.BoolFlag {Name : "leave-running" , Usage : "leave the process running after checkpointing" },
33- cli.BoolFlag {Name : "tcp-established" , Usage : "allow open tcp connections" },
34- cli.BoolFlag {Name : "tcp-skip-in-flight" , Usage : "skip in-flight tcp connections" },
35- cli.BoolFlag {Name : "link-remap" , Usage : "allow one to link unlinked files back when possible" },
36- cli.BoolFlag {Name : "ext-unix-sk" , Usage : "allow external unix sockets" },
37- cli.BoolFlag {Name : "shell-job" , Usage : "allow shell jobs" },
38- cli.BoolFlag {Name : "lazy-pages" , Usage : "use userfaultfd to lazily restore memory pages" },
39- cli.IntFlag {Name : "status-fd" , Value : - 1 , Usage : "criu writes \\ 0 to this FD once lazy-pages is ready" },
40- cli.StringFlag {Name : "page-server" , Value : "" , Usage : "ADDRESS:PORT of the page server" },
41- cli.BoolFlag {Name : "file-locks" , Usage : "handle file locks, for safety" },
42- cli.BoolFlag {Name : "pre-dump" , Usage : "dump container's memory information only, leave the container running after this" },
43- cli.StringFlag {Name : "manage-cgroups-mode" , Value : "" , Usage : "cgroups mode: soft|full|strict|ignore (default: soft)" },
44- cli.StringSliceFlag {Name : "empty-ns" , Usage : "create a namespace, but don't restore its properties" },
45- cli.BoolFlag {Name : "auto-dedup" , Usage : "enable auto deduplication of memory images" },
30+ & cli.StringFlag {Name : "image-path" , Value : "" , Usage : "path for saving criu image files" },
31+ & cli.StringFlag {Name : "work-path" , Value : "" , Usage : "path for saving work files and logs" },
32+ & cli.StringFlag {Name : "parent-path" , Value : "" , Usage : "path for previous criu image files in pre-dump" },
33+ & cli.BoolFlag {Name : "leave-running" , Usage : "leave the process running after checkpointing" },
34+ & cli.BoolFlag {Name : "tcp-established" , Usage : "allow open tcp connections" },
35+ & cli.BoolFlag {Name : "tcp-skip-in-flight" , Usage : "skip in-flight tcp connections" },
36+ & cli.BoolFlag {Name : "link-remap" , Usage : "allow one to link unlinked files back when possible" },
37+ & cli.BoolFlag {Name : "ext-unix-sk" , Usage : "allow external unix sockets" },
38+ & cli.BoolFlag {Name : "shell-job" , Usage : "allow shell jobs" },
39+ & cli.BoolFlag {Name : "lazy-pages" , Usage : "use userfaultfd to lazily restore memory pages" },
40+ & cli.IntFlag {Name : "status-fd" , Value : - 1 , Usage : "criu writes \\ 0 to this FD once lazy-pages is ready" },
41+ & cli.StringFlag {Name : "page-server" , Value : "" , Usage : "ADDRESS:PORT of the page server" },
42+ & cli.BoolFlag {Name : "file-locks" , Usage : "handle file locks, for safety" },
43+ & cli.BoolFlag {Name : "pre-dump" , Usage : "dump container's memory information only, leave the container running after this" },
44+ & cli.StringFlag {Name : "manage-cgroups-mode" , Value : "" , Usage : "cgroups mode: soft|full|strict|ignore (default: soft)" },
45+ & cli.StringSliceFlag {Name : "empty-ns" , Usage : "create a namespace, but don't restore its properties" },
46+ & cli.BoolFlag {Name : "auto-dedup" , Usage : "enable auto deduplication of memory images" },
4647 },
47- Action : func (context * cli.Context ) error {
48- if err := checkArgs (context , 1 , exactArgs ); err != nil {
48+ Action : func (_ context. Context , cmd * cli.Command ) error {
49+ if err := checkArgs (cmd , 1 , exactArgs ); err != nil {
4950 return err
5051 }
5152 // XXX: Currently this is untested with rootless containers.
5253 if os .Geteuid () != 0 || userns .RunningInUserNS () {
5354 logrus .Warn ("runc checkpoint is untested with rootless containers" )
5455 }
5556
56- container , err := getContainer (context )
57+ container , err := getContainer (cmd )
5758 if err != nil {
5859 return err
5960 }
@@ -64,7 +65,7 @@ checkpointed.`,
6465 if status == libcontainer .Created || status == libcontainer .Stopped {
6566 return fmt .Errorf ("Container cannot be checkpointed in %s state" , status .String ())
6667 }
67- options , err := criuOptions (context )
68+ options , err := criuOptions (cmd )
6869 if err != nil {
6970 return err
7071 }
@@ -80,8 +81,8 @@ checkpointed.`,
8081 },
8182}
8283
83- func prepareImagePaths (context * cli.Context ) (string , string , error ) {
84- imagePath := context .String ("image-path" )
84+ func prepareImagePaths (cmd * cli.Command ) (string , string , error ) {
85+ imagePath := cmd .String ("image-path" )
8586 if imagePath == "" {
8687 imagePath = getDefaultImagePath ()
8788 }
@@ -90,7 +91,7 @@ func prepareImagePaths(context *cli.Context) (string, string, error) {
9091 return "" , "" , err
9192 }
9293
93- parentPath := context .String ("parent-path" )
94+ parentPath := cmd .String ("parent-path" )
9495 if parentPath == "" {
9596 return imagePath , parentPath , nil
9697 }
@@ -112,35 +113,35 @@ func prepareImagePaths(context *cli.Context) (string, string, error) {
112113 return imagePath , parentPath , nil
113114}
114115
115- func criuOptions (context * cli.Context ) (* libcontainer.CriuOpts , error ) {
116- imagePath , parentPath , err := prepareImagePaths (context )
116+ func criuOptions (cmd * cli.Command ) (* libcontainer.CriuOpts , error ) {
117+ imagePath , parentPath , err := prepareImagePaths (cmd )
117118 if err != nil {
118119 return nil , err
119120 }
120121
121122 opts := & libcontainer.CriuOpts {
122123 ImagesDirectory : imagePath ,
123- WorkDirectory : context .String ("work-path" ),
124+ WorkDirectory : cmd .String ("work-path" ),
124125 ParentImage : parentPath ,
125- LeaveRunning : context .Bool ("leave-running" ),
126- TcpEstablished : context .Bool ("tcp-established" ),
127- TcpSkipInFlight : context .Bool ("tcp-skip-in-flight" ),
128- LinkRemap : context .Bool ("link-remap" ),
129- ExternalUnixConnections : context .Bool ("ext-unix-sk" ),
130- ShellJob : context .Bool ("shell-job" ),
131- FileLocks : context .Bool ("file-locks" ),
132- PreDump : context .Bool ("pre-dump" ),
133- AutoDedup : context .Bool ("auto-dedup" ),
134- LazyPages : context .Bool ("lazy-pages" ),
135- StatusFd : context .Int ("status-fd" ),
136- LsmProfile : context .String ("lsm-profile" ),
137- LsmMountContext : context .String ("lsm-mount-context" ),
138- ManageCgroupsMode : context .String ("manage-cgroups-mode" ),
126+ LeaveRunning : cmd .Bool ("leave-running" ),
127+ TcpEstablished : cmd .Bool ("tcp-established" ),
128+ TcpSkipInFlight : cmd .Bool ("tcp-skip-in-flight" ),
129+ LinkRemap : cmd .Bool ("link-remap" ),
130+ ExternalUnixConnections : cmd .Bool ("ext-unix-sk" ),
131+ ShellJob : cmd .Bool ("shell-job" ),
132+ FileLocks : cmd .Bool ("file-locks" ),
133+ PreDump : cmd .Bool ("pre-dump" ),
134+ AutoDedup : cmd .Bool ("auto-dedup" ),
135+ LazyPages : cmd .Bool ("lazy-pages" ),
136+ StatusFd : cmd .Int ("status-fd" ),
137+ LsmProfile : cmd .String ("lsm-profile" ),
138+ LsmMountContext : cmd .String ("lsm-mount-context" ),
139+ ManageCgroupsMode : cmd .String ("manage-cgroups-mode" ),
139140 }
140141
141142 // CRIU options below may or may not be set.
142143
143- if psOpt := context .String ("page-server" ); psOpt != "" {
144+ if psOpt := cmd .String ("page-server" ); psOpt != "" {
144145 address , port , err := net .SplitHostPort (psOpt )
145146
146147 if err != nil || address == "" || port == "" {
@@ -159,12 +160,12 @@ func criuOptions(context *cli.Context) (*libcontainer.CriuOpts, error) {
159160 // runc doesn't manage network devices and their configuration.
160161 nsmask := unix .CLONE_NEWNET
161162
162- if context .IsSet ("empty-ns" ) {
163+ if cmd .IsSet ("empty-ns" ) {
163164 namespaceMapping := map [specs.LinuxNamespaceType ]int {
164165 specs .NetworkNamespace : unix .CLONE_NEWNET ,
165166 }
166167
167- for _ , ns := range context .StringSlice ("empty-ns" ) {
168+ for _ , ns := range cmd .StringSlice ("empty-ns" ) {
168169 f , exists := namespaceMapping [specs .LinuxNamespaceType (ns )]
169170 if ! exists {
170171 return nil , fmt .Errorf ("namespace %q is not supported" , ns )
0 commit comments