3,237 bytes added
, 08:46, 12 August 2022
[https://github.com/checkpoint-restore/go-criu go-criu] provides a Go library based on [[CRIT]]. It supports encoding, decoding, and modification of CRIU images natively within Go code. It also ships with a CLI tool similar to the Python variant (not completely identical, see [[#Difference between Go and Python CLI]]).
== Usage ==
You can import the package into your Go project and use it like below:
<code>import "github.com/checkpoint-restore/go-criu/v6/crit"</code>
To build the CLI tool, run <code>make bin/crit</code> in the <code>crit/</code> directory.
== Operations ==
Every operation requires a CRIT service instance. This can be created in two ways:
* <code>New()</code>: Create a new service to use within a Go program. It does not support reading from stdin or printing to stdout, and is meant to be used purely non-interactively.
* <code>NewCli()</code>: Create a new service to use as a CLI. It supports reading from stdin and printing to stdout if no file paths are provided, and can be used interactively.
Both functions take the same parameters:
* Path of the input file
* Path of the output file
* Path of the input directory (for `crit explore`)
* Boolean to generate indented and multi-line JSON output
* Boolean to skip payload data
<syntaxhighlight lang="go">
c := crit.New(
"inventory.img", /* input path */
"", /* output path */
"", /* input dir path (for Explore*()) */
false, /* indenting for JSON */
false, /* no payload */
)
</syntaxhighlight>
The below operations are provided by the service.
<syntaxhighlight lang="go">
Decode() (*CriuImage, error)
Info() (*CriuImage, error)
Parse() (*CriuImage, error)
Encode(*CriuImage) error
ExplorePs() (*PsTree, error)
ExploreFds() ([]*Fd, error)
ExploreMems() ([]*MemMap, error)
ExploreRss() ([]*RssMap, error)
</syntaxhighlight>
Documentation about the types and methods provided by the library can be found [https://pkg.go.dev/github.com/checkpoint-restore/go-criu/v5/crit|here]
== Examples ==
Here is a simple example of decoding <code>inventory.img</code> and printing the image version:
'''example.go'''
<syntaxhighlight lang="go">
package main
import (
"fmt"
"github.com/checkpoint-restore/go-criu/v6/crit"
"github.com/checkpoint-restore/go-criu/v6/crit/images"
)
func main() {
c := crit.New(
"inventory.img", /* input path */
"", /* output path */
"", /* input dir path (for Explore*()) */
false, /* indenting for JSON */
false, /* no payload */
)
img, err := c.Decode()
if err != nil {
fmt.Println("Error: ", err)
}
imgVersion := img.(*images.InventoryEntry).GetImgVersion()
fmt.Println("Image version is ", imgVersion)
}
</syntaxhighlight>
== Difference between Go and Python CLI ==
* The Go CLI uses JSON as the standard output format for all commands, whereas the Python CLI uses custom formats for <code>crit explore</code>. This results in different outputs for the same command, although the content remains the same.
* The Go JSON output uses camelCased field names, whereas the Python CLI JSON output uses snake_case field names.
[[Category: Images]]
[[Category: API]]
[[Category: Go]]