mirror of
https://github.com/containers/podman.git
synced 2025-06-29 06:57:13 +08:00
Merge pull request #15868 from rst0git/podman-run-checkpoint-img
cmd/podman: add support for checkpoint images
This commit is contained in:
@ -10,7 +10,6 @@ import (
|
||||
"github.com/containers/podman/v4/cmd/podman/registry"
|
||||
"github.com/containers/podman/v4/cmd/podman/utils"
|
||||
"github.com/containers/podman/v4/cmd/podman/validate"
|
||||
"github.com/containers/podman/v4/libpod/define"
|
||||
"github.com/containers/podman/v4/pkg/domain/entities"
|
||||
"github.com/containers/podman/v4/pkg/rootless"
|
||||
"github.com/spf13/cobra"
|
||||
@ -94,7 +93,7 @@ func init() {
|
||||
|
||||
func restore(cmd *cobra.Command, args []string) error {
|
||||
var (
|
||||
e error
|
||||
err error
|
||||
errs utils.OutputErrors
|
||||
)
|
||||
podmanStart := time.Now()
|
||||
@ -105,9 +104,9 @@ func restore(cmd *cobra.Command, args []string) error {
|
||||
// Check if the container exists (#15055)
|
||||
exists := &entities.BoolReport{Value: false}
|
||||
for _, ctr := range args {
|
||||
exists, e = registry.ContainerEngine().ContainerExists(registry.GetContext(), ctr, entities.ContainerExistsOptions{})
|
||||
if e != nil {
|
||||
return e
|
||||
exists, err = registry.ContainerEngine().ContainerExists(registry.GetContext(), ctr, entities.ContainerExistsOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists.Value {
|
||||
break
|
||||
@ -116,27 +115,10 @@ func restore(cmd *cobra.Command, args []string) error {
|
||||
|
||||
if !exists.Value {
|
||||
// Find out if this is an image
|
||||
inspectOpts := entities.InspectOptions{}
|
||||
imgData, _, err := registry.ImageEngine().Inspect(context.Background(), args, inspectOpts)
|
||||
restoreOptions.CheckpointImage, err = utils.IsCheckpointImage(context.Background(), args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hostInfo, err := registry.ContainerEngine().Info(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := range imgData {
|
||||
restoreOptions.CheckpointImage = true
|
||||
checkpointRuntimeName, found := imgData[i].Annotations[define.CheckpointAnnotationRuntimeName]
|
||||
if !found {
|
||||
return fmt.Errorf("image is not a checkpoint: %s", imgData[i].ID)
|
||||
}
|
||||
if hostInfo.Host.OCIRuntime.Name != checkpointRuntimeName {
|
||||
return fmt.Errorf("container image \"%s\" requires runtime: \"%s\"", imgData[i].ID, checkpointRuntimeName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
notImport := (!restoreOptions.CheckpointImage && restoreOptions.Import == "")
|
||||
|
@ -148,6 +148,35 @@ func run(cmd *cobra.Command, args []string) error {
|
||||
imageName = name
|
||||
}
|
||||
|
||||
// If this is a checkpoint image, invoke container restore.
|
||||
// We do not return `err` when checkpointImage is false, because the value
|
||||
// of `err` could be "image is not a checkpoint". In this case, the run
|
||||
// command should continue as usual, preserving backwards compatibility.
|
||||
checkpointImage, err := utils.IsCheckpointImage(registry.GetContext(), []string{imageName})
|
||||
if checkpointImage {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var restoreOptions entities.RestoreOptions
|
||||
responses, err := registry.ContainerEngine().ContainerRestore(registry.GetContext(), []string{imageName}, restoreOptions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var errs utils.OutputErrors
|
||||
for _, r := range responses {
|
||||
switch {
|
||||
case r.Err != nil:
|
||||
errs = append(errs, r.Err)
|
||||
case r.RawInput != "":
|
||||
fmt.Println(r.RawInput)
|
||||
default:
|
||||
fmt.Println(r.Id)
|
||||
}
|
||||
}
|
||||
return errs.PrintErrors()
|
||||
}
|
||||
|
||||
if cliVals.Replace {
|
||||
if err := replaceContainer(cliVals.Name); err != nil {
|
||||
return err
|
||||
|
@ -1,9 +1,12 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/containers/podman/v4/cmd/podman/registry"
|
||||
"github.com/containers/podman/v4/libpod/define"
|
||||
"github.com/containers/podman/v4/pkg/domain/entities"
|
||||
"github.com/containers/podman/v4/pkg/domain/entities/reports"
|
||||
)
|
||||
@ -99,3 +102,40 @@ func PrintNetworkPruneResults(networkPruneReport []*entities.NetworkPruneReport,
|
||||
}
|
||||
return errs.PrintErrors()
|
||||
}
|
||||
|
||||
// IsCheckpointImage returns true with no error only if all values in
|
||||
// namesOrIDs correspond to checkpoint images AND these images are
|
||||
// compatible with the container runtime that is currently in use,
|
||||
// e.g., crun or runc.
|
||||
//
|
||||
// IsCheckpointImage returns false with no error when none of the values
|
||||
// in namesOrIDs corresponds to an ID or name of an image.
|
||||
//
|
||||
// Otherwise, IsCheckpointImage returns false with appropriate error.
|
||||
func IsCheckpointImage(ctx context.Context, namesOrIDs []string) (bool, error) {
|
||||
inspectOpts := entities.InspectOptions{}
|
||||
imgData, _, err := registry.ImageEngine().Inspect(ctx, namesOrIDs, inspectOpts)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(imgData) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
imgID := imgData[0].ID
|
||||
|
||||
hostInfo, err := registry.ContainerEngine().Info(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
for i := range imgData {
|
||||
checkpointRuntimeName, found := imgData[i].Annotations[define.CheckpointAnnotationRuntimeName]
|
||||
if !found {
|
||||
return false, fmt.Errorf("image is not a checkpoint: %s", imgID)
|
||||
}
|
||||
if hostInfo.Host.OCIRuntime.Name != checkpointRuntimeName {
|
||||
return false, fmt.Errorf("container image \"%s\" requires runtime: \"%s\"", imgID, checkpointRuntimeName)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
@ -295,4 +295,52 @@ var _ = Describe("Podman checkpoint", func() {
|
||||
Expect(result).Should(Exit(0))
|
||||
Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))
|
||||
})
|
||||
|
||||
It("podman run checkpoint image to restore container", func() {
|
||||
SkipIfContainerized("FIXME: #15015. All checkpoint tests hang when containerized.")
|
||||
// Container image must be lowercase
|
||||
checkpointImage := "alpine-checkpoint-" + strings.ToLower(RandomString(6))
|
||||
containerName := "alpine-container-" + RandomString(6)
|
||||
|
||||
// Create container
|
||||
localRunString := []string{"run", "-d", "--name", containerName, ALPINE, "top"}
|
||||
session := podmanTest.Podman(localRunString)
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session).Should(Exit(0))
|
||||
containerID1 := session.OutputToString()
|
||||
|
||||
// Checkpoint container, create checkpoint image
|
||||
result := podmanTest.Podman([]string{"container", "checkpoint", "--create-image", checkpointImage, "--keep", containerID1})
|
||||
result.WaitWithDefaultTimeout()
|
||||
Expect(result).Should(Exit(0))
|
||||
Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))
|
||||
|
||||
// Remove existing container
|
||||
result = podmanTest.Podman([]string{"rm", "-t", "1", "-f", containerName})
|
||||
result.WaitWithDefaultTimeout()
|
||||
Expect(result).Should(Exit(0))
|
||||
|
||||
// Restore containers from image using `podman run`
|
||||
result = podmanTest.Podman([]string{"run", checkpointImage})
|
||||
result.WaitWithDefaultTimeout()
|
||||
Expect(result).Should(Exit(0))
|
||||
Expect(podmanTest.NumberOfContainersRunning()).To(Equal(1))
|
||||
|
||||
// Check if the container is running
|
||||
status := podmanTest.Podman([]string{"inspect", containerName, "--format={{.State.Status}}"})
|
||||
status.WaitWithDefaultTimeout()
|
||||
Expect(status).Should(Exit(0))
|
||||
Expect(status.OutputToString()).To(Equal("running"))
|
||||
|
||||
// Clean-up
|
||||
result = podmanTest.Podman([]string{"rm", "-t", "0", "-fa"})
|
||||
result.WaitWithDefaultTimeout()
|
||||
Expect(result).Should(Exit(0))
|
||||
Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))
|
||||
|
||||
result = podmanTest.Podman([]string{"rmi", checkpointImage})
|
||||
result.WaitWithDefaultTimeout()
|
||||
Expect(result).Should(Exit(0))
|
||||
Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user