CRIT (Go library)

Revision as of 09:00, 12 August 2022 by Prajwal S N (talk | contribs) (Fix indents)

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:

import "github.com/checkpoint-restore/go-criu/v6/crit"

To build the CLI tool, run make bin/crit in the crit/ directory.

Operations

Every operation requires a CRIT service instance. This can be created in two ways:

  • New(): 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.
  • NewCli(): 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

The below operations are provided by the service.

    Decode() (*CriuImage, error)
    Info() (*CriuImage, error)
    Parse() (*CriuImage, error)
    Encode(*CriuImage) error
    ExplorePs() (*PsTree, error)
    ExploreFds() ([]*Fd, error)
    ExploreMems() ([]*MemMap, error)
    ExploreRss() ([]*RssMap, error)

Documentation about the types and methods provided by the library can be found here.

Examples

Here is a simple example of decoding inventory.img and printing the image version:

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

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 crit explore. 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.