2019-11-22 16:39:49 +00:00
|
|
|
package libpod
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-27 16:05:22 +00:00
|
|
|
"fmt"
|
2019-11-22 16:39:49 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-04-22 06:01:12 +00:00
|
|
|
"github.com/containers/common/libimage"
|
2022-02-02 17:17:23 +00:00
|
|
|
"github.com/containers/common/libnetwork/types"
|
2022-01-18 09:14:48 +00:00
|
|
|
"github.com/containers/podman/v4/libpod/define"
|
|
|
|
"github.com/containers/podman/v4/pkg/errorhandling"
|
|
|
|
"github.com/containers/podman/v4/pkg/rootless"
|
|
|
|
"github.com/containers/podman/v4/pkg/util"
|
2020-08-27 16:05:22 +00:00
|
|
|
"github.com/containers/storage"
|
2019-11-22 16:39:49 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Reset removes all storage
|
|
|
|
func (r *Runtime) Reset(ctx context.Context) error {
|
2021-09-30 18:43:39 +00:00
|
|
|
var timeout *uint
|
2019-11-22 16:39:49 +00:00
|
|
|
pods, err := r.GetAllPods()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, p := range pods {
|
2021-09-30 18:43:39 +00:00
|
|
|
if err := r.RemovePod(ctx, p, true, true, timeout); err != nil {
|
2019-11-22 16:39:49 +00:00
|
|
|
if errors.Cause(err) == define.ErrNoSuchPod {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-22 13:45:15 +00:00
|
|
|
logrus.Errorf("Removing Pod %s: %v", p.ID(), err)
|
2019-11-22 16:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctrs, err := r.GetAllContainers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range ctrs {
|
2021-09-30 18:43:39 +00:00
|
|
|
if err := r.RemoveContainer(ctx, c, true, true, timeout); err != nil {
|
2019-11-22 16:39:49 +00:00
|
|
|
if err := r.RemoveStorageContainer(c.ID(), true); err != nil {
|
|
|
|
if errors.Cause(err) == define.ErrNoSuchCtr {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-22 13:45:15 +00:00
|
|
|
logrus.Errorf("Removing container %s: %v", c.ID(), err)
|
2019-11-22 16:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 15:05:34 +00:00
|
|
|
if err := r.stopPauseProcess(); err != nil {
|
2021-09-22 13:45:15 +00:00
|
|
|
logrus.Errorf("Stopping pause process: %v", err)
|
2019-11-22 16:39:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 06:01:12 +00:00
|
|
|
rmiOptions := &libimage.RemoveImagesOptions{Filters: []string{"readonly=false"}}
|
|
|
|
if _, rmiErrors := r.LibimageRuntime().RemoveImages(ctx, nil, rmiOptions); rmiErrors != nil {
|
|
|
|
return errorhandling.JoinErrors(rmiErrors)
|
2019-11-22 16:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
volumes, err := r.state.AllVolumes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, v := range volumes {
|
2021-09-30 18:43:39 +00:00
|
|
|
if err := r.RemoveVolume(ctx, v, true, timeout); err != nil {
|
2019-11-22 16:39:49 +00:00
|
|
|
if errors.Cause(err) == define.ErrNoSuchVolume {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-22 13:45:15 +00:00
|
|
|
logrus.Errorf("Removing volume %s: %v", v.config.Name, err)
|
2019-11-22 16:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 17:17:23 +00:00
|
|
|
// remove all networks
|
|
|
|
nets, err := r.network.NetworkList()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, net := range nets {
|
|
|
|
// do not delete the default network
|
|
|
|
if net.Name == r.network.DefaultNetworkName() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// ignore not exists errors because of the TOCTOU problem
|
|
|
|
if err := r.network.NetworkRemove(net.Name); err != nil && !errors.Is(err, types.ErrNoSuchNetwork) {
|
|
|
|
logrus.Errorf("Removing network %s: %v", net.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 13:48:28 +00:00
|
|
|
xdgRuntimeDir := filepath.Clean(os.Getenv("XDG_RUNTIME_DIR"))
|
2019-11-22 16:39:49 +00:00
|
|
|
_, prevError := r.store.Shutdown(true)
|
2020-12-16 13:48:28 +00:00
|
|
|
graphRoot := filepath.Clean(r.store.GraphRoot())
|
|
|
|
if graphRoot == xdgRuntimeDir {
|
2019-11-22 16:39:49 +00:00
|
|
|
if prevError != nil {
|
|
|
|
logrus.Error(prevError)
|
|
|
|
}
|
2020-12-16 13:48:28 +00:00
|
|
|
prevError = errors.Errorf("failed to remove runtime graph root dir %s, since it is the same as XDG_RUNTIME_DIR", graphRoot)
|
|
|
|
} else {
|
|
|
|
if err := os.RemoveAll(graphRoot); err != nil {
|
|
|
|
if prevError != nil {
|
|
|
|
logrus.Error(prevError)
|
|
|
|
}
|
|
|
|
prevError = err
|
|
|
|
}
|
2019-11-22 16:39:49 +00:00
|
|
|
}
|
2020-12-16 13:48:28 +00:00
|
|
|
runRoot := filepath.Clean(r.store.RunRoot())
|
|
|
|
if runRoot == xdgRuntimeDir {
|
2019-11-22 16:39:49 +00:00
|
|
|
if prevError != nil {
|
|
|
|
logrus.Error(prevError)
|
|
|
|
}
|
2020-12-16 13:48:28 +00:00
|
|
|
prevError = errors.Errorf("failed to remove runtime root dir %s, since it is the same as XDG_RUNTIME_DIR", runRoot)
|
|
|
|
} else {
|
|
|
|
if err := os.RemoveAll(runRoot); err != nil {
|
|
|
|
if prevError != nil {
|
|
|
|
logrus.Error(prevError)
|
|
|
|
}
|
|
|
|
prevError = err
|
|
|
|
}
|
2019-11-22 16:39:49 +00:00
|
|
|
}
|
2020-02-18 20:45:48 +00:00
|
|
|
runtimeDir, err := util.GetRuntimeDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-27 14:13:51 +00:00
|
|
|
tempDir := r.config.Engine.TmpDir
|
|
|
|
if tempDir == runtimeDir {
|
|
|
|
tempDir = filepath.Join(tempDir, "containers")
|
2020-02-18 20:45:48 +00:00
|
|
|
}
|
2020-12-16 13:48:28 +00:00
|
|
|
if filepath.Clean(tempDir) == xdgRuntimeDir {
|
2019-11-22 16:39:49 +00:00
|
|
|
if prevError != nil {
|
|
|
|
logrus.Error(prevError)
|
|
|
|
}
|
2020-12-16 13:48:28 +00:00
|
|
|
prevError = errors.Errorf("failed to remove runtime tmpdir %s, since it is the same as XDG_RUNTIME_DIR", tempDir)
|
|
|
|
} else {
|
|
|
|
if err := os.RemoveAll(tempDir); err != nil {
|
|
|
|
if prevError != nil {
|
|
|
|
logrus.Error(prevError)
|
|
|
|
}
|
|
|
|
prevError = err
|
|
|
|
}
|
2019-11-22 16:39:49 +00:00
|
|
|
}
|
2020-08-27 16:05:22 +00:00
|
|
|
if storageConfPath, err := storage.DefaultConfigFile(rootless.IsRootless()); err == nil {
|
|
|
|
if _, err = os.Stat(storageConfPath); err == nil {
|
|
|
|
fmt.Printf("A storage.conf file exists at %s\n", storageConfPath)
|
2021-09-30 15:01:27 +00:00
|
|
|
fmt.Println("You should remove this file if you did not modify the configuration.")
|
2019-11-22 16:39:49 +00:00
|
|
|
}
|
2020-08-27 16:05:22 +00:00
|
|
|
} else {
|
|
|
|
if prevError != nil {
|
|
|
|
logrus.Error(prevError)
|
|
|
|
}
|
|
|
|
prevError = err
|
2019-11-22 16:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return prevError
|
|
|
|
}
|