-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support to convert checkpoint archives to OCI images #125
Open
Parthiba-Hazra
wants to merge
1
commit into
checkpoint-restore:main
Choose a base branch
from
Parthiba-Hazra:archiveToOCI
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bash has a builtin optarg parser which would be nicer than purely positional parameters.
Also add checks that all the input files actually exist.