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 12, 2024
1 parent 5d0d8f1 commit 409c235
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"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 +127,14 @@ func featureCheck(c *criu.Criu) error {
)
}

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

Check warning on line 136 in test/main.go

View check run for this annotation

Codecov / codecov/patch

test/main.go#L131-L136

Added lines #L131 - L136 were not covered by tests

return nil
}

Expand All @@ -139,6 +148,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 155 in test/main.go

View check run for this annotation

Codecov / codecov/patch

test/main.go#L154-L155

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

Check warning on line 158 in test/main.go

View check run for this annotation

Codecov / codecov/patch

test/main.go#L157-L158

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

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

Check warning on line 170 in test/main.go

View check run for this annotation

Codecov / codecov/patch

test/main.go#L169-L170

Added lines #L169 - L170 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
45 changes: 45 additions & 0 deletions utils/criu_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build linux
// +build linux

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 20 in utils/criu_linux.go

View check run for this annotation

Codecov / codecov/patch

utils/criu_linux.go#L19-L20

Added lines #L19 - L20 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)

Check warning on line 25 in utils/criu_linux.go

View check run for this annotation

Codecov / codecov/patch

utils/criu_linux.go#L25

Added line #L25 was not covered by tests
}

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

Check warning on line 37 in utils/criu_linux.go

View check run for this annotation

Codecov / codecov/patch

utils/criu_linux.go#L36-L37

Added lines #L36 - L37 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 MemTrack() bool {
return false
}

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

0 comments on commit 409c235

Please sign in to comment.