github.com/AbhinandanKurakure/podman/v3@v3.4.10/libpod/reset.go (about)

     1  package libpod
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/containers/common/libimage"
    10  	"github.com/containers/podman/v3/libpod/define"
    11  	"github.com/containers/podman/v3/pkg/errorhandling"
    12  	"github.com/containers/podman/v3/pkg/rootless"
    13  	"github.com/containers/podman/v3/pkg/util"
    14  	"github.com/containers/storage"
    15  	"github.com/pkg/errors"
    16  	"github.com/sirupsen/logrus"
    17  )
    18  
    19  // Reset removes all storage
    20  func (r *Runtime) Reset(ctx context.Context) error {
    21  	pods, err := r.GetAllPods()
    22  	if err != nil {
    23  		return err
    24  	}
    25  	for _, p := range pods {
    26  		if err := r.RemovePod(ctx, p, true, true); err != nil {
    27  			if errors.Cause(err) == define.ErrNoSuchPod {
    28  				continue
    29  			}
    30  			logrus.Errorf("Error removing Pod %s: %v", p.ID(), err)
    31  		}
    32  	}
    33  
    34  	ctrs, err := r.GetAllContainers()
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	for _, c := range ctrs {
    40  		if err := r.RemoveContainer(ctx, c, true, true); err != nil {
    41  			if err := r.RemoveStorageContainer(c.ID(), true); err != nil {
    42  				if errors.Cause(err) == define.ErrNoSuchCtr {
    43  					continue
    44  				}
    45  				logrus.Errorf("Error removing container %s: %v", c.ID(), err)
    46  			}
    47  		}
    48  	}
    49  
    50  	if err := r.stopPauseProcess(); err != nil {
    51  		logrus.Errorf("Error stopping pause process: %v", err)
    52  	}
    53  
    54  	rmiOptions := &libimage.RemoveImagesOptions{Filters: []string{"readonly=false"}}
    55  	if _, rmiErrors := r.LibimageRuntime().RemoveImages(ctx, nil, rmiOptions); rmiErrors != nil {
    56  		return errorhandling.JoinErrors(rmiErrors)
    57  	}
    58  
    59  	volumes, err := r.state.AllVolumes()
    60  	if err != nil {
    61  		return err
    62  	}
    63  	for _, v := range volumes {
    64  		if err := r.RemoveVolume(ctx, v, true); err != nil {
    65  			if errors.Cause(err) == define.ErrNoSuchVolume {
    66  				continue
    67  			}
    68  			logrus.Errorf("Error removing volume %s: %v", v.config.Name, err)
    69  		}
    70  	}
    71  
    72  	xdgRuntimeDir := filepath.Clean(os.Getenv("XDG_RUNTIME_DIR"))
    73  	_, prevError := r.store.Shutdown(true)
    74  	graphRoot := filepath.Clean(r.store.GraphRoot())
    75  	if graphRoot == xdgRuntimeDir {
    76  		if prevError != nil {
    77  			logrus.Error(prevError)
    78  		}
    79  		prevError = errors.Errorf("failed to remove runtime graph root dir %s, since it is the same as XDG_RUNTIME_DIR", graphRoot)
    80  	} else {
    81  		if err := os.RemoveAll(graphRoot); err != nil {
    82  			if prevError != nil {
    83  				logrus.Error(prevError)
    84  			}
    85  			prevError = err
    86  		}
    87  	}
    88  	runRoot := filepath.Clean(r.store.RunRoot())
    89  	if runRoot == xdgRuntimeDir {
    90  		if prevError != nil {
    91  			logrus.Error(prevError)
    92  		}
    93  		prevError = errors.Errorf("failed to remove runtime root dir %s, since it is the same as XDG_RUNTIME_DIR", runRoot)
    94  	} else {
    95  		if err := os.RemoveAll(runRoot); err != nil {
    96  			if prevError != nil {
    97  				logrus.Error(prevError)
    98  			}
    99  			prevError = err
   100  		}
   101  	}
   102  	runtimeDir, err := util.GetRuntimeDir()
   103  	if err != nil {
   104  		return err
   105  	}
   106  	tempDir := r.config.Engine.TmpDir
   107  	if tempDir == runtimeDir {
   108  		tempDir = filepath.Join(tempDir, "containers")
   109  	}
   110  	if filepath.Clean(tempDir) == xdgRuntimeDir {
   111  		if prevError != nil {
   112  			logrus.Error(prevError)
   113  		}
   114  		prevError = errors.Errorf("failed to remove runtime tmpdir %s, since it is the same as XDG_RUNTIME_DIR", tempDir)
   115  	} else {
   116  		if err := os.RemoveAll(tempDir); err != nil {
   117  			if prevError != nil {
   118  				logrus.Error(prevError)
   119  			}
   120  			prevError = err
   121  		}
   122  	}
   123  	if storageConfPath, err := storage.DefaultConfigFile(rootless.IsRootless()); err == nil {
   124  		if _, err = os.Stat(storageConfPath); err == nil {
   125  			fmt.Printf("A storage.conf file exists at %s\n", storageConfPath)
   126  			fmt.Println("You should remove this file if you did not modified the configuration.")
   127  		}
   128  	} else {
   129  		if prevError != nil {
   130  			logrus.Error(prevError)
   131  		}
   132  		prevError = err
   133  	}
   134  
   135  	return prevError
   136  }