Skip to content

Commit

Permalink
Merge pull request #21259 from mheon/backport_21226_48
Browse files Browse the repository at this point in the history
Backport #21226 to the v4.8 branch
  • Loading branch information
openshift-merge-bot[bot] authored Jan 15, 2024
2 parents 721d9ee + 023a354 commit 0c9a007
Show file tree
Hide file tree
Showing 19 changed files with 212 additions and 313 deletions.
8 changes: 8 additions & 0 deletions cmd/podman/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func ContainerEngine() entities.ContainerEngine {
func NewContainerEngine(cmd *cobra.Command, args []string) (entities.ContainerEngine, error) {
if containerEngine == nil {
podmanOptions.FlagSet = cmd.Flags()
if cmd.Name() == "reset" && cmd.Parent().Name() == "system" {
logrus.Debugf("Performing system reset, runtime validation checks will be relaxed")
podmanOptions.IsReset = true
}
if cmd.Name() == "renumber" && cmd.Parent().Name() == "system" {
logrus.Debugf("Performing system renumber, runtime validation checks will be relaxed")
podmanOptions.IsRenumber = true
}
engine, err := infra.NewContainerEngine(&podmanOptions)
if err != nil {
return nil, err
Expand Down
15 changes: 1 addition & 14 deletions cmd/podman/system/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"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/domain/infra"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -55,19 +54,7 @@ func init() {
}

func migrate(cmd *cobra.Command, args []string) {
// Shutdown all running engines, `renumber` will hijack repository
registry.ContainerEngine().Shutdown(registry.Context())
registry.ImageEngine().Shutdown(registry.Context())

engine, err := infra.NewSystemEngine(entities.MigrateMode, registry.PodmanConfig())
if err != nil {
fmt.Println(err)
os.Exit(define.ExecErrorCodeGeneric)
}
defer engine.Shutdown(registry.Context())

err = engine.Migrate(registry.Context(), cmd.Flags(), registry.PodmanConfig(), migrateOptions)
if err != nil {
if err := registry.ContainerEngine().Migrate(registry.Context(), migrateOptions); err != nil {
fmt.Println(err)

// FIXME change this to return the error like other commands
Expand Down
16 changes: 1 addition & 15 deletions cmd/podman/system/renumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/containers/podman/v4/cmd/podman/registry"
"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/domain/infra"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -42,19 +40,7 @@ func init() {
})
}
func renumber(cmd *cobra.Command, args []string) {
// Shutdown all running engines, `renumber` will hijack all methods
registry.ContainerEngine().Shutdown(registry.Context())
registry.ImageEngine().Shutdown(registry.Context())

engine, err := infra.NewSystemEngine(entities.RenumberMode, registry.PodmanConfig())
if err != nil {
fmt.Println(err)
os.Exit(define.ExecErrorCodeGeneric)
}
defer engine.Shutdown(registry.Context())

err = engine.Renumber(registry.Context(), cmd.Flags(), registry.PodmanConfig())
if err != nil {
if err := registry.ContainerEngine().Renumber(registry.Context()); err != nil {
fmt.Println(err)
// FIXME change this to return the error like other commands
// defer will never run on os.Exit()
Expand Down
27 changes: 6 additions & 21 deletions cmd/podman/system/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import (
"github.com/containers/common/pkg/completion"
"github.com/containers/podman/v4/cmd/podman/registry"
"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/domain/infra"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -49,10 +46,9 @@ func init() {

func reset(cmd *cobra.Command, args []string) {
// Get all the external containers in use
listCtn, _ := registry.ContainerEngine().ContainerListExternal(registry.Context())
listCtnIds := make([]string, 0, len(listCtn))
for _, externalCtn := range listCtn {
listCtnIds = append(listCtnIds, externalCtn.ID)
listCtn, err := registry.ContainerEngine().ContainerListExternal(registry.Context())
if err != nil {
logrus.Error(err)
}
// Prompt for confirmation if --force is not set
if !forceFlag {
Expand Down Expand Up @@ -91,25 +87,14 @@ func reset(cmd *cobra.Command, args []string) {
}
}

// Purge all the external containers with storage
_, err := registry.ContainerEngine().ContainerRm(registry.Context(), listCtnIds, entities.RmOptions{Force: true, All: true, Ignore: true, Volumes: true})
if err != nil {
logrus.Error(err)
}
// Clean build cache if any
err = volumes.CleanCacheMount()
if err != nil {
if err := volumes.CleanCacheMount(); err != nil {
logrus.Error(err)
}
// Shutdown all running engines, `reset` will hijack repository
registry.ContainerEngine().Shutdown(registry.Context())
registry.ImageEngine().Shutdown(registry.Context())

// Do not try to shut the engine down, as a Reset engine is not valid
// after its creation.
if _, err := infra.NewSystemEngine(entities.ResetMode, registry.PodmanConfig()); err != nil {
// ContainerEngine() is unusable and shut down after this.
if err := registry.ContainerEngine().Reset(registry.Context()); err != nil {
logrus.Error(err)
os.Exit(define.ExecErrorCodeGeneric)
}

// Shutdown podman-machine and delete all machine files
Expand Down
50 changes: 6 additions & 44 deletions libpod/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,10 @@ func WithDefaultInfraCommand(cmd string) RuntimeOption {
}
}

// WithReset instructs libpod to reset all storage to factory defaults.
// All containers, pods, volumes, images, and networks will be removed.
// All directories created by Libpod will be removed.
// WithReset tells Libpod that the runtime will be used to perform a system
// reset. A number of checks at initialization are relaxed as the runtime is
// going to be used to remove all containers, pods, volumes, images, and
// networks.
func WithReset() RuntimeOption {
return func(rt *Runtime) error {
if rt.valid {
Expand All @@ -480,10 +481,8 @@ func WithReset() RuntimeOption {
}
}

// WithRenumber instructs libpod to perform a lock renumbering while
// initializing. This will handle migrations from early versions of libpod with
// file locks to newer versions with SHM locking, as well as changes in the
// number of configured locks.
// WithRenumber tells Libpod that the runtime will be used to perform a system
// renumber. A number of checks on initialization related to locks are relaxed.
func WithRenumber() RuntimeOption {
return func(rt *Runtime) error {
if rt.valid {
Expand All @@ -496,43 +495,6 @@ func WithRenumber() RuntimeOption {
}
}

// WithMigrate instructs libpod to migrate container configurations to account
// for changes between Engine versions. All running containers will be stopped
// during a migration, then restarted after the migration is complete.
func WithMigrate() RuntimeOption {
return func(rt *Runtime) error {
if rt.valid {
return define.ErrRuntimeFinalized
}

rt.doMigrate = true

return nil
}
}

// WithMigrateRuntime instructs Engine to change the default OCI runtime on all
// containers during a migration. This is not used if `MigrateRuntime()` is not
// also passed.
// Engine makes no promises that your containers continue to work with the new
// runtime - migrations between dissimilar runtimes may well break things.
// Use with caution.
func WithMigrateRuntime(requestedRuntime string) RuntimeOption {
return func(rt *Runtime) error {
if rt.valid {
return define.ErrRuntimeFinalized
}

if requestedRuntime == "" {
return fmt.Errorf("must provide a non-empty name for new runtime: %w", define.ErrInvalidArg)
}

rt.migrateRuntime = requestedRuntime

return nil
}
}

// WithEventsLogger sets the events backend to use.
// Currently supported values are "file" for file backend and "journald" for
// journald backend.
Expand Down
69 changes: 56 additions & 13 deletions libpod/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,24 @@ func (r *Runtime) removeAllDirs() error {
return lastErr
}

// Reset removes all storage
func (r *Runtime) reset(ctx context.Context) error {
// Reset removes all Libpod files.
// All containers, images, volumes, pods, and networks will be removed.
// Calls Shutdown(), rendering the runtime unusable after this is run.
func (r *Runtime) Reset(ctx context.Context) error {
// Acquire the alive lock and hold it.
// Ensures that we don't let other Podman commands run while we are
// removing everything.
aliveLock, err := r.getRuntimeAliveLock()
if err != nil {
return fmt.Errorf("retrieving alive lock: %w", err)
}
aliveLock.Lock()
defer aliveLock.Unlock()

if !r.valid {
return define.ErrRuntimeStopped
}

var timeout *uint
pods, err := r.GetAllPods()
if err != nil {
Expand All @@ -119,25 +135,31 @@ func (r *Runtime) reset(ctx context.Context) error {
}

for _, c := range ctrs {
if err := r.RemoveContainer(ctx, c, true, true, timeout); err != nil {
if err := r.RemoveStorageContainer(c.ID(), true); err != nil {
if errors.Is(err, define.ErrNoSuchCtr) {
continue
}
logrus.Errorf("Removing container %s: %v", c.ID(), err)
if ctrs, _, err := r.RemoveContainerAndDependencies(ctx, c, true, true, timeout); err != nil {
for ctr, err := range ctrs {
logrus.Errorf("Error removing container %s: %v", ctr, err)
}

if errors.Is(err, define.ErrNoSuchCtr) || errors.Is(err, define.ErrCtrRemoved) {
continue
}

logrus.Errorf("Removing container %s: %v", c.ID(), err)

// There's no point trying a storage container removal
// here. High likelihood it doesn't work
// (RemoveStorageContainer will refuse to remove if a
// container exists in the Libpod database) and, even if
// it would, we'll get the container later on during
// image removal.
}
}

if err := r.stopPauseProcess(); err != nil {
logrus.Errorf("Stopping pause process: %v", err)
}

rmiOptions := &libimage.RemoveImagesOptions{Filters: []string{"readonly=false"}}
if _, rmiErrors := r.LibimageRuntime().RemoveImages(ctx, nil, rmiOptions); rmiErrors != nil {
return errorhandling.JoinErrors(rmiErrors)
}

// Volumes before images, as volumes can mount images.
volumes, err := r.state.AllVolumes()
if err != nil {
return err
Expand All @@ -151,6 +173,19 @@ func (r *Runtime) reset(ctx context.Context) error {
}
}

// Set force and ignore.
// Ignore shouldn't be necessary, but it seems safer. We want everything
// gone anyways...
rmiOptions := &libimage.RemoveImagesOptions{
Force: true,
Ignore: true,
RemoveContainerFunc: r.RemoveContainersForImageCallback(ctx),
Filters: []string{"readonly=false"},
}
if _, rmiErrors := r.LibimageRuntime().RemoveImages(ctx, nil, rmiOptions); rmiErrors != nil {
return errorhandling.JoinErrors(rmiErrors)
}

// remove all networks
nets, err := r.network.NetworkList()
if err != nil {
Expand Down Expand Up @@ -241,5 +276,13 @@ func (r *Runtime) reset(ctx context.Context) error {
prevError = err
}

// Shut down the runtime, it's no longer usable after mass-deletion.
if err := r.Shutdown(false); err != nil {
if prevError != nil {
logrus.Error(prevError)
}
prevError = err
}

return prevError
}
Loading

0 comments on commit 0c9a007

Please sign in to comment.