forked from containers/podman
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreset.go
295 lines (266 loc) · 8.21 KB
/
reset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//go:build !remote
package libpod
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/containers/common/libimage"
"github.com/containers/common/libnetwork/types"
blobinfocache "github.com/containers/image/v5/pkg/blobinfocache"
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/pkg/errorhandling"
"github.com/containers/podman/v5/pkg/util"
"github.com/containers/storage"
"github.com/containers/storage/pkg/lockfile"
stypes "github.com/containers/storage/types"
"github.com/sirupsen/logrus"
)
// removeAllDirs removes all Podman storage directories. It is intended to be
// used as a backup for reset() when that function cannot be used due to
// failures in initializing libpod.
// It does not expect that all the directories match what is in use by Podman,
// as this is a common failure point for `system reset`. As such, our ability to
// interface with containers and pods is somewhat limited.
// This function assumes that we do not have a working c/storage store.
func (r *Runtime) removeAllDirs() error {
var lastErr error
// Grab the runtime alive lock.
// This ensures that no other Podman process can run while we are doing
// a reset, so no race conditions with containers/pods/etc being created
// while we are resetting storage.
// TODO: maybe want a helper for getting the path? This is duped from
// runtime.go
runtimeAliveLock := filepath.Join(r.config.Engine.TmpDir, "alive.lck")
aliveLock, err := lockfile.GetLockFile(runtimeAliveLock)
if err != nil {
logrus.Errorf("Lock runtime alive lock %s: %v", runtimeAliveLock, err)
} else {
aliveLock.Lock()
defer aliveLock.Unlock()
}
// We do not have a store - so we can't really try and remove containers
// or pods or volumes...
// Try and remove the directories, in hopes that they are unmounted.
// This is likely to fail but it's the best we can do.
// Volume path
if err := os.RemoveAll(r.config.Engine.VolumePath); err != nil {
lastErr = fmt.Errorf("removing volume path: %w", err)
}
// Tmpdir
if err := os.RemoveAll(r.config.Engine.TmpDir); err != nil {
if lastErr != nil {
logrus.Errorf("Reset: %v", lastErr)
}
lastErr = fmt.Errorf("removing tmp dir: %w", err)
}
// Runroot
if err := os.RemoveAll(r.storageConfig.RunRoot); err != nil {
if lastErr != nil {
logrus.Errorf("Reset: %v", lastErr)
}
lastErr = fmt.Errorf("removing run root: %w", err)
}
// Static dir
if err := os.RemoveAll(r.config.Engine.StaticDir); err != nil {
if lastErr != nil {
logrus.Errorf("Reset: %v", lastErr)
}
lastErr = fmt.Errorf("removing static dir: %w", err)
}
// Graph root
if err := os.RemoveAll(r.storageConfig.GraphRoot); err != nil {
if lastErr != nil {
logrus.Errorf("Reset: %v", lastErr)
}
lastErr = fmt.Errorf("removing graph root: %w", err)
}
return lastErr
}
// 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 = 0
pods, err := r.GetAllPods()
if err != nil {
return err
}
for _, p := range pods {
if ctrs, err := r.RemovePod(ctx, p, true, true, &timeout); err != nil {
if errors.Is(err, define.ErrNoSuchPod) {
continue
}
for ctr, err := range ctrs {
if err != nil {
logrus.Errorf("Error removing pod %s container %s: %v", p.ID(), ctr, err)
}
}
logrus.Errorf("Removing Pod %s: %v", p.ID(), err)
}
}
ctrs, err := r.GetAllContainers()
if err != nil {
return err
}
for _, c := range ctrs {
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)
}
// Volumes before images, as volumes can mount images.
volumes, err := r.state.AllVolumes()
if err != nil {
return err
}
for _, v := range volumes {
if err := r.RemoveVolume(ctx, v, true, &timeout); err != nil {
if errors.Is(err, define.ErrNoSuchVolume) {
continue
}
logrus.Errorf("Removing volume %s: %v", v.config.Name, err)
}
}
// 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 {
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)
}
}
xdgRuntimeDir := os.Getenv("XDG_RUNTIME_DIR")
if xdgRuntimeDir != "" {
xdgRuntimeDir, err = filepath.EvalSymlinks(xdgRuntimeDir)
if err != nil {
return err
}
}
_, prevError := r.store.Shutdown(true)
graphRoot := filepath.Clean(r.store.GraphRoot())
if graphRoot == xdgRuntimeDir {
if prevError != nil {
logrus.Error(prevError)
}
prevError = fmt.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
}
}
runRoot := filepath.Clean(r.store.RunRoot())
if runRoot == xdgRuntimeDir {
if prevError != nil {
logrus.Error(prevError)
}
prevError = fmt.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
}
}
runtimeDir, err := util.GetRootlessRuntimeDir()
if err != nil {
return err
}
tempDir := r.config.Engine.TmpDir
if tempDir == runtimeDir {
tempDir = filepath.Join(tempDir, "containers")
}
if filepath.Clean(tempDir) == xdgRuntimeDir {
if prevError != nil {
logrus.Error(prevError)
}
prevError = fmt.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
}
}
if err := blobinfocache.CleanupDefaultCache(nil); err != nil {
if prevError != nil {
logrus.Error(prevError)
}
prevError = err
}
if storageConfPath, err := storage.DefaultConfigFile(); err == nil {
switch storageConfPath {
case stypes.SystemConfigFile:
break
default:
if _, err = os.Stat(storageConfPath); err == nil {
fmt.Printf(" A %q config file exists.\n", storageConfPath)
fmt.Println("Remove this file if you did not modify the configuration.")
}
}
} else {
if prevError != nil {
logrus.Error(prevError)
}
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
}