-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to build OCI images from checkpoint archives
- With this enhancement, users can now build OCI images from checkpoint archives using the `checkpointctl build` command. This command accepts checkpoint archive and a image name as input and generates an OCI image suitable for use with container runtimes like CRI-O or Podman. Users can inspect the image to get information about runtime, container, pod, namespace, image name etc. Signed-off-by: Parthiba-Hazra <[email protected]>
- Loading branch information
1 parent
9a448d5
commit c6d1a17
Showing
7 changed files
with
279 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/checkpoint-restore/checkpointctl/internal" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func BuildCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "build [checkpoint-path] [image-name]", | ||
Short: "Create an OCI image from a container checkpoint archive", | ||
RunE: convertArchive, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func convertArchive(cmd *cobra.Command, args []string) error { | ||
if len(args) != 2 { | ||
return fmt.Errorf("please provide both the checkpoint path and the image name") | ||
} | ||
|
||
checkpointPath := args[0] | ||
imageName := args[1] | ||
|
||
imageCreater := internal.NewImageCreator(imageName, checkpointPath) | ||
|
||
err := imageCreater.CreateImageFromCheckpoint(context.Background()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("Image '%s' created successfully from checkpoint '%s'\n", imageName, checkpointPath) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
= checkpointctl-build(1) | ||
include::footer.adoc[] | ||
|
||
== Name | ||
|
||
*checkpointctl-build* - Create OCI image from a checkpoint tar file. | ||
|
||
== Synopsis | ||
|
||
*checkpointctl build* CHECKPOINT_PATH IMAGE_NAME | ||
|
||
== Options | ||
|
||
*-h*, *--help*:: | ||
Show help for checkpointctl build | ||
|
||
== Description | ||
|
||
Creates an OCI image from a checkpoint tar file. This command requires `buildah` to be installed on the system. | ||
|
||
Please ensure that `buildah` is installed before running this command. | ||
|
||
== See also | ||
|
||
checkpointctl(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
|
||
metadata "github.com/checkpoint-restore/checkpointctl/lib" | ||
) | ||
|
||
const ( | ||
BUILD_SCRIPT = "/usr/libexec/build_image.sh" | ||
PODMAN_ENGINE = "libpod" | ||
) | ||
|
||
type ImageCreator struct { | ||
imageName string | ||
checkpointPath string | ||
} | ||
|
||
func NewImageCreator(imageName, checkpointPath string) *ImageCreator { | ||
return &ImageCreator{ | ||
imageName: imageName, | ||
checkpointPath: checkpointPath, | ||
} | ||
} | ||
|
||
func (ic *ImageCreator) CreateImageFromCheckpoint(ctx context.Context) error { | ||
tempDir, err := os.MkdirTemp("", "checkpoint_tmp") | ||
if err != nil { | ||
return err | ||
} | ||
defer os.RemoveAll(tempDir) | ||
|
||
annotationsFilePath, err := ic.setCheckpointAnnotations(tempDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var stdout bytes.Buffer | ||
var stderr bytes.Buffer | ||
cmd := exec.Command(BUILD_SCRIPT, "-a", annotationsFilePath, "-c", ic.checkpointPath, "-i", ic.imageName) | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
err = cmd.Run() | ||
if err != nil { | ||
return fmt.Errorf("failed to execute script: %v, %v, %w", stdout.String(), stderr.String(), err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func writeAnnotationsToFile(tempDir string, annotations map[string]string) (string, error) { | ||
tempFile, err := os.CreateTemp(tempDir, "annotations_*.txt") | ||
if err != nil { | ||
return "", err | ||
} | ||
defer tempFile.Close() | ||
|
||
for key, value := range annotations { | ||
_, err := fmt.Fprintf(tempFile, "%s=%s\n", key, value) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
return tempFile.Name(), nil | ||
} | ||
|
||
func (ic *ImageCreator) setCheckpointAnnotations(tempDir string) (string, error) { | ||
filesToExtract := []string{"spec.dump", "config.dump"} | ||
if err := UntarFiles(ic.checkpointPath, tempDir, filesToExtract); err != nil { | ||
log.Printf("Error extracting files from archive %s: %v\n", ic.checkpointPath, err) | ||
return "", err | ||
} | ||
|
||
var err error | ||
info := &checkpointInfo{} | ||
info.configDump, _, err = metadata.ReadContainerCheckpointConfigDump(tempDir) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
info.specDump, _, err = metadata.ReadContainerCheckpointSpecDump(tempDir) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
info.containerInfo, err = getContainerInfo(info.specDump, info.configDump) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
checkpointImageAnnotations := map[string]string{} | ||
checkpointImageAnnotations[metadata.CheckpointAnnotationEngine] = info.containerInfo.Engine | ||
checkpointImageAnnotations[metadata.CheckpointAnnotationName] = info.containerInfo.Name | ||
checkpointImageAnnotations[metadata.CheckpointAnnotationPod] = info.containerInfo.Pod | ||
checkpointImageAnnotations[metadata.CheckpointAnnotationNamespace] = info.containerInfo.Namespace | ||
checkpointImageAnnotations[metadata.CheckpointAnnotationRootfsImageUserRequested] = info.configDump.RootfsImage | ||
checkpointImageAnnotations[metadata.CheckpointAnnotationRootfsImageName] = info.configDump.RootfsImageName | ||
checkpointImageAnnotations[metadata.CheckpointAnnotationRootfsImageID] = info.configDump.RootfsImageRef | ||
checkpointImageAnnotations[metadata.CheckpointAnnotationRuntimeName] = info.configDump.OCIRuntime | ||
|
||
annotationsFilePath, err := writeAnnotationsToFile(tempDir, checkpointImageAnnotations) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return annotationsFilePath, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/bin/bash | ||
|
||
set -euo pipefail | ||
|
||
usage() { | ||
cat <<EOF | ||
Usage: ${0##*/} [-a ANNOTATIONS_FILE] [-c CHECKPOINT_PATH] [-i IMAGE_NAME] | ||
Create OCI image from a checkpoint tar file. | ||
-a path to the annotations file | ||
-c path to the checkpoint file | ||
-i name of the resulting image | ||
EOF | ||
exit 1 | ||
} | ||
|
||
annotationsFilePath="" | ||
checkpointPath="" | ||
imageName="" | ||
|
||
while getopts ":a:c:i:" opt; do | ||
case ${opt} in | ||
a) | ||
annotationsFilePath=$OPTARG | ||
;; | ||
c) | ||
checkpointPath=$OPTARG | ||
;; | ||
i) | ||
imageName=$OPTARG | ||
;; | ||
:) | ||
echo "Option -$OPTARG requires an argument." | ||
usage | ||
;; | ||
\?) | ||
echo "Invalid option: -$OPTARG" | ||
usage | ||
;; | ||
esac | ||
done | ||
shift $((OPTIND - 1)) | ||
|
||
if [[ -z $annotationsFilePath || -z $checkpointPath || -z $imageName ]]; then | ||
echo "All options (-a, -c, -i) are required." | ||
usage | ||
fi | ||
|
||
if ! command -v buildah &>/dev/null; then | ||
echo "buildah is not installed. Please install buildah before running 'checkpointctl build' command." | ||
exit 1 | ||
fi | ||
|
||
if [[ ! -f $annotationsFilePath ]]; then | ||
echo "Annotations file not found: $annotationsFilePath" | ||
exit 1 | ||
fi | ||
|
||
if [[ ! -f $checkpointPath ]]; then | ||
echo "Checkpoint file not found: $checkpointPath" | ||
exit 1 | ||
fi | ||
|
||
newcontainer=$(buildah from scratch) | ||
|
||
buildah add "$newcontainer" "$checkpointPath" | ||
|
||
while IFS= read -r line; do | ||
key=$(echo "$line" | cut -d '=' -f 1) | ||
value=$(echo "$line" | cut -d '=' -f 2-) | ||
buildah config --annotation "$key=$value" "$newcontainer" | ||
done <"$annotationsFilePath" | ||
|
||
buildah commit "$newcontainer" "$imageName" | ||
|
||
buildah rm "$newcontainer" | ||
|
||
echo "Checkpoint image created successfully: $imageName" |