From 85aabe233ec17b25b65d547e59b9a7f5d2e8479b Mon Sep 17 00:00:00 2001 From: Liu Hua Date: Thu, 13 May 2021 11:42:53 +0800 Subject: [PATCH] C/R: let criu use its default if --work-path is not set Now runc puts dump/restore logs in c.root defaultly, which will be deleted when container exits. So if checkpinting/restoring failed, we can not get these logs and analyze why. This patch lets criu use its default if --work-path is not set: - Use WorkDirectory found in criu's configfile. - Use ImageDirectory. Signed-off-by: Liu Hua --- libcontainer/container_linux.go | 55 ++++++++++++++++----------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/libcontainer/container_linux.go b/libcontainer/container_linux.go index 6ce1854f6..7409f6f88 100644 --- a/libcontainer/container_linux.go +++ b/libcontainer/container_linux.go @@ -972,20 +972,6 @@ func (c *linuxContainer) Checkpoint(criuOpts *CriuOpts) error { return err } - if criuOpts.WorkDirectory == "" { - criuOpts.WorkDirectory = filepath.Join(c.root, "criu.work") - } - - if err := os.Mkdir(criuOpts.WorkDirectory, 0o700); err != nil && !os.IsExist(err) { - return err - } - - workDir, err := os.Open(criuOpts.WorkDirectory) - if err != nil { - return err - } - defer workDir.Close() - imageDir, err := os.Open(criuOpts.ImagesDirectory) if err != nil { return err @@ -994,7 +980,6 @@ func (c *linuxContainer) Checkpoint(criuOpts *CriuOpts) error { rpcOpts := criurpc.CriuOpts{ ImagesDirFd: proto.Int32(int32(imageDir.Fd())), - WorkDirFd: proto.Int32(int32(workDir.Fd())), LogLevel: proto.Int32(4), LogFile: proto.String("dump.log"), Root: proto.String(c.config.Rootfs), @@ -1012,6 +997,19 @@ func (c *linuxContainer) Checkpoint(criuOpts *CriuOpts) error { LazyPages: proto.Bool(criuOpts.LazyPages), } + // if criuOpts.WorkDirectory is not set, criu default is used. + if criuOpts.WorkDirectory != "" { + if err := os.Mkdir(criuOpts.WorkDirectory, 0o700); err != nil && !os.IsExist(err) { + return err + } + workDir, err := os.Open(criuOpts.WorkDirectory) + if err != nil { + return err + } + defer workDir.Close() + rpcOpts.WorkDirFd = proto.Int32(int32(workDir.Fd())) + } + c.handleCriuConfigurationFile(&rpcOpts) // If the container is running in a network namespace and has @@ -1311,19 +1309,6 @@ func (c *linuxContainer) Restore(process *Process, criuOpts *CriuOpts) error { if err := c.checkCriuVersion(30000); err != nil { return err } - if criuOpts.WorkDirectory == "" { - criuOpts.WorkDirectory = filepath.Join(c.root, "criu.work") - } - // Since a container can be C/R'ed multiple times, - // the work directory may already exist. - if err := os.Mkdir(criuOpts.WorkDirectory, 0o700); err != nil && !os.IsExist(err) { - return err - } - workDir, err := os.Open(criuOpts.WorkDirectory) - if err != nil { - return err - } - defer workDir.Close() if criuOpts.ImagesDirectory == "" { return errors.New("invalid directory to restore checkpoint") } @@ -1356,7 +1341,6 @@ func (c *linuxContainer) Restore(process *Process, criuOpts *CriuOpts) error { Type: &t, Opts: &criurpc.CriuOpts{ ImagesDirFd: proto.Int32(int32(imageDir.Fd())), - WorkDirFd: proto.Int32(int32(workDir.Fd())), EvasiveDevices: proto.Bool(true), LogLevel: proto.Int32(4), LogFile: proto.String("restore.log"), @@ -1384,6 +1368,19 @@ func (c *linuxContainer) Restore(process *Process, criuOpts *CriuOpts) error { req.Opts.LsmProfile = proto.String(criuOpts.LsmProfile) } + if criuOpts.WorkDirectory != "" { + // Since a container can be C/R'ed multiple times, + // the work directory may already exist. + if err := os.Mkdir(criuOpts.WorkDirectory, 0o700); err != nil && !os.IsExist(err) { + return err + } + workDir, err := os.Open(criuOpts.WorkDirectory) + if err != nil { + return err + } + defer workDir.Close() + req.Opts.WorkDirFd = proto.Int32(int32(workDir.Fd())) + } c.handleCriuConfigurationFile(req.Opts) if err := c.handleRestoringNamespaces(req.Opts, &extraFiles); err != nil {