Skip to content

Commit

Permalink
Merge pull request #165 from adrianreber/2024-03-12-common
Browse files Browse the repository at this point in the history
Add functions which are used in Podman, CRI-O and containerd
  • Loading branch information
snprajwal committed Mar 14, 2024
2 parents 55534ec + 6908ab4 commit 28ccaf6
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(),
)
}

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)
}
if version != version2 {
log.Fatalf("Detected versions do not match (%d != %d)", version, version2)
}
// 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)
}

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

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)
}

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
}

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 28ccaf6

Please sign in to comment.