From c5f0ce979b7043a97266546b5e6bdab36ad626b4 Mon Sep 17 00:00:00 2001 From: Adrian Reber Date: Wed, 26 Jul 2017 06:49:09 +0000 Subject: [PATCH] checkCriuVersion: only ask criu once about its version If the version of criu has already been determined there is no need to ask criu for the version again. Use the value from c.criuVersion. v2: * reduce unnecessary code movement in the patch series * factor out the criu version parsing into a separate function Signed-off-by: Adrian Reber --- libcontainer/container_linux.go | 40 ++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/libcontainer/container_linux.go b/libcontainer/container_linux.go index cda48255..0d085d3b 100644 --- a/libcontainer/container_linux.go +++ b/libcontainer/container_linux.go @@ -590,13 +590,12 @@ func (c *linuxContainer) checkCriuFeatures(criuOpts *CriuOpts, rpcOpts *criurpc. return nil } -// checkCriuVersion checks Criu version greater than or equal to minVersion -func (c *linuxContainer) checkCriuVersion(minVersion int) error { +func parseCriuVersion(path string) (int, error) { var x, y, z int - out, err := exec.Command(c.criuPath, "-V").Output() + out, err := exec.Command(path, "-V").Output() if err != nil { - return fmt.Errorf("Unable to execute CRIU command: %s", c.criuPath) + return 0, fmt.Errorf("Unable to execute CRIU command: %s", path) } x = 0 @@ -608,7 +607,7 @@ func (c *linuxContainer) checkCriuVersion(minVersion int) error { if sp := strings.Index(string(out), "GitID"); sp > 0 { version = string(out)[sp:ep] } else { - return fmt.Errorf("Unable to parse the CRIU version: %s", c.criuPath) + return 0, fmt.Errorf("Unable to parse the CRIU version: %s", path) } n, err := fmt.Sscanf(string(version), "GitID: v%d.%d.%d", &x, &y, &z) // 1.5.2 @@ -619,7 +618,7 @@ func (c *linuxContainer) checkCriuVersion(minVersion int) error { z++ } if n < 2 || err != nil { - return fmt.Errorf("Unable to parse the CRIU version: %s %d %s", version, n, err) + return 0, fmt.Errorf("Unable to parse the CRIU version: %s %d %s", version, n, err) } } else { // criu release version format @@ -628,19 +627,40 @@ func (c *linuxContainer) checkCriuVersion(minVersion int) error { n, err = fmt.Sscanf(string(out), "Version: %d.%d\n", &x, &y) // 1.6 } if n < 2 || err != nil { - return fmt.Errorf("Unable to parse the CRIU version: %s %d %s", out, n, err) + return 0, fmt.Errorf("Unable to parse the CRIU version: %s %d %s", out, n, err) } } - c.criuVersion = x*10000 + y*100 + z + return x*10000 + y*100 + z, nil +} - if c.criuVersion < minVersion { - return fmt.Errorf("CRIU version %d must be %d or higher", c.criuVersion, minVersion) +func compareCriuVersion(criuVersion int, minVersion int) error { + // simple function to perform the actual version compare + if criuVersion < minVersion { + return fmt.Errorf("CRIU version %d must be %d or higher", criuVersion, minVersion) } return nil } +// checkCriuVersion checks Criu version greater than or equal to minVersion +func (c *linuxContainer) checkCriuVersion(minVersion int) error { + + // If the version of criu has already been determined there is no need + // to ask criu for the version again. Use the value from c.criuVersion. + if c.criuVersion != 0 { + return compareCriuVersion(c.criuVersion, minVersion) + } + + var err error + c.criuVersion, err = parseCriuVersion(c.criuPath) + if err != nil { + return err + } + + return compareCriuVersion(c.criuVersion, minVersion) +} + const descriptorsFilename = "descriptors.json" func (c *linuxContainer) addCriuDumpMount(req *criurpc.CriuReq, m *configs.Mount) {