diff --git a/cmd/podman/system/reset.go b/cmd/podman/system/reset.go index adac3bbc60..9e83518a5b 100644 --- a/cmd/podman/system/reset.go +++ b/cmd/podman/system/reset.go @@ -20,6 +20,7 @@ var ( systemResetDescription = `Reset podman storage back to default state All containers will be stopped and removed, and all images, volumes, networks and container content will be removed. + This command does not restart podman.service and podman.socket systemd units. You may need to manually restart it after running this command. ` systemResetCommand = &cobra.Command{ Annotations: map[string]string{registry.EngineMode: registry.ABIMode}, diff --git a/cmd/podman/system/reset_machine.go b/cmd/podman/system/reset_machine.go index 2c49e1758a..6152b75a96 100644 --- a/cmd/podman/system/reset_machine.go +++ b/cmd/podman/system/reset_machine.go @@ -67,7 +67,7 @@ func resetMachine() error { logrus.Errorf("unable to remove machine data dir %q: %q", dirs.DataDir.GetPath(), err) } - if err := utils.GuardedRemoveAll(dirs.RuntimeDir.GetPath()); err != nil { + if err := utils.RemoveFilesExcept(dirs.RuntimeDir.GetPath(), "podman.sock"); err != nil { logrus.Errorf("unable to remove machine runtime dir %q: %q", dirs.RuntimeDir.GetPath(), err) } diff --git a/docs/source/markdown/podman-system-reset.1.md b/docs/source/markdown/podman-system-reset.1.md index 3dae2cbc93..bbc80fe5c7 100644 --- a/docs/source/markdown/podman-system-reset.1.md +++ b/docs/source/markdown/podman-system-reset.1.md @@ -19,6 +19,8 @@ or `volume_path`. of the relevant configurations. If the administrator modified the configuration files first, `podman system reset` might not be able to clean up the previous storage. +`podman system reset` does not restart podman.service and podman.socket systemd units. You may need to manually restart it after running this command. + ## OPTIONS #### **--force**, **-f** diff --git a/utils/utils.go b/utils/utils.go index 9643fded8d..c5f773964a 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -6,6 +6,7 @@ import ( "io" "os" "os/exec" + "path/filepath" "strings" "time" @@ -119,6 +120,27 @@ func GuardedRemoveAll(path string) error { return os.RemoveAll(path) } +// RemoveFilesExcept removes all files in a directory except for the one specified +// by excludeFile and will not delete certain catastrophic paths. +func RemoveFilesExcept(path string, excludeFile string) error { + if path == "" || path == "/" { + return fmt.Errorf("refusing to recursively delete `%s`", path) + } + + files, err := os.ReadDir(path) + if err != nil { + return err + } + for _, file := range files { + if file.Name() != excludeFile { + if err := os.RemoveAll(filepath.Join(path, file.Name())); err != nil { + return err + } + } + } + return nil +} + func ProgressBar(prefix string, size int64, onComplete string) (*mpb.Progress, *mpb.Bar) { p := mpb.New( mpb.WithWidth(80), // Do not go below 80, see bug #17718