Skip to content

Commit

Permalink
Add functions which are used in Podman, CRI-O and containerd
Browse files Browse the repository at this point in the history
Convenience functions to check if CRIU is available and at least a
certain version are now used in containerd, CRI-O and Podman. This
change moves those functions to go-criu.

Signed-off-by: Adrian Reber <[email protected]>
  • Loading branch information
adrianreber committed Mar 13, 2024
1 parent 5d0d8f1 commit 6908ab4
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package main
import (
"fmt"
"log"
"math"
"os"
"strconv"

"github.com/checkpoint-restore/go-criu/v7"
"github.com/checkpoint-restore/go-criu/v7/rpc"
"github.com/checkpoint-restore/go-criu/v7/utils"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -126,6 +128,15 @@ func featureCheck(c *criu.Criu) error {
)
}

isMemTrack := utils.IsMemTrack()
if isMemTrack != featuresToCompare.GetMemTrack() {
return fmt.Errorf(
"unexpected MemTrack FeatureCheck result %v:%v",
isMemTrack,
featuresToCompare.GetMemTrack(),
)
}

Check warning on line 138 in test/main.go

View check run for this annotation

Codecov / codecov/patch

test/main.go#L133-L138

Added lines #L133 - L138 were not covered by tests

return nil
}

Expand All @@ -139,6 +150,14 @@ func main() {
os.Exit(1)
}
log.Println("CRIU version", version)
// Compare if version from convenience function matches
version2, err := utils.GetCriuVersion()
if err != nil {
log.Fatalln(err)
}

Check warning on line 157 in test/main.go

View check run for this annotation

Codecov / codecov/patch

test/main.go#L156-L157

Added lines #L156 - L157 were not covered by tests
if version != version2 {
log.Fatalf("Detected versions do not match (%d != %d)", version, version2)
}

Check warning on line 160 in test/main.go

View check run for this annotation

Codecov / codecov/patch

test/main.go#L159-L160

Added lines #L159 - L160 were not covered by tests
// Check if version at least 3.2
result, err := c.IsCriuAtLeast(30200)
if err != nil {
Expand All @@ -148,6 +167,14 @@ func main() {
log.Fatalln("CRIU version to old")
}

if err := utils.CheckForCriu(30200); err != nil {
log.Fatalln(err)
}

Check warning on line 172 in test/main.go

View check run for this annotation

Codecov / codecov/patch

test/main.go#L171-L172

Added lines #L171 - L172 were not covered by tests

if err := utils.CheckForCriu(math.MaxInt); err == nil {
log.Fatalf("Checking for CRIU version %d should have failed.", math.MaxInt)
}

Check warning on line 176 in test/main.go

View check run for this annotation

Codecov / codecov/patch

test/main.go#L175-L176

Added lines #L175 - L176 were not covered by tests

if err = featureCheck(c); err != nil {
log.Fatalln(err)
}
Expand Down
8 changes: 8 additions & 0 deletions utils/criu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package utils

// MinCriuVersion for Podman at least CRIU 3.11 is required
const MinCriuVersionPodman = 31100

// PodCriuVersion is the version of CRIU needed for
// checkpointing and restoring containers out of and into Pods.
const PodCriuVersion = 31600
42 changes: 42 additions & 0 deletions utils/criu_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package utils

import (
"fmt"

"github.com/checkpoint-restore/go-criu/v7"
"github.com/checkpoint-restore/go-criu/v7/rpc"
"google.golang.org/protobuf/proto"
)

// CheckForCRIU checks if CRIU is available and if it is as least the
// version as specified in the "version" parameter.
func CheckForCriu(version int) error {
criuVersion, err := GetCriuVersion()
if err != nil {
return fmt.Errorf("failed to check for criu version: %w", err)
}

Check warning on line 17 in utils/criu_linux.go

View check run for this annotation

Codecov / codecov/patch

utils/criu_linux.go#L16-L17

Added lines #L16 - L17 were not covered by tests

if criuVersion >= version {
return nil
}
return fmt.Errorf("checkpoint/restore requires at least CRIU %d, current version is %d", version, criuVersion)
}

// Convenience function to easily check if memory tracking is supported.
func IsMemTrack() bool {
features, err := criu.MakeCriu().FeatureCheck(
&rpc.CriuFeatures{
MemTrack: proto.Bool(true),
},
)
if err != nil {
return false
}

Check warning on line 34 in utils/criu_linux.go

View check run for this annotation

Codecov / codecov/patch

utils/criu_linux.go#L33-L34

Added lines #L33 - L34 were not covered by tests

return features.GetMemTrack()
}

func GetCriuVersion() (int, error) {
c := criu.MakeCriu()
return c.GetCriuVersion()
}
18 changes: 18 additions & 0 deletions utils/criu_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build !linux
// +build !linux

package utils

import "fmt"

func CheckForCriu(version int) error {
return fmt.Errorf("CRIU not supported on this platform")
}

func IsMemTrack() bool {
return false
}

func GetCriuVersion() (int, error) {
return 0, fmt.Errorf("CRIU not supported in this platform")
}

0 comments on commit 6908ab4

Please sign in to comment.