File: container_copy_common.go

package info (click to toggle)
podman 5.4.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,124 kB
  • sloc: sh: 6,119; perl: 2,710; python: 2,258; ansic: 1,556; makefile: 1,022; xml: 121; ruby: 42; awk: 12; csh: 8
file content (344 lines) | stat: -rw-r--r-- 10,120 bytes parent folder | download
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//go:build !remote && (linux || freebsd)

package libpod

import (
	"errors"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"

	buildahCopiah "github.com/containers/buildah/copier"
	"github.com/containers/buildah/pkg/chrootuser"
	"github.com/containers/buildah/util"
	"github.com/containers/podman/v5/libpod/define"
	"github.com/containers/podman/v5/libpod/shutdown"
	"github.com/containers/podman/v5/pkg/rootless"
	"github.com/containers/storage/pkg/archive"
	"github.com/containers/storage/pkg/idtools"
	"github.com/containers/storage/pkg/stringid"
	"github.com/opencontainers/runtime-spec/specs-go"
	"github.com/sirupsen/logrus"
)

func (c *Container) copyFromArchive(path string, chown, noOverwriteDirNonDir bool, rename map[string]string, reader io.Reader) (func() error, error) {
	var (
		mountPoint   string
		resolvedRoot string
		resolvedPath string
		unmount      func()
		cleanupFuncs []func()
		err          error
		locked       bool = true
	)

	// Make sure that "/" copies the *contents* of the mount point and not
	// the directory.
	if path == "/" {
		path = "/."
	}

	// Optimization: only mount if the container is not already.
	if c.state.Mounted {
		mountPoint = c.state.Mountpoint
		unmount = func() {}
	} else {
		// NOTE: make sure to unmount in error paths.
		mountPoint, err = c.mount()
		if err != nil {
			return nil, err
		}
		c.state.Mountpoint = mountPoint
		if err := c.save(); err != nil {
			return nil, err
		}

		unmount = func() {
			if !locked {
				c.lock.Lock()
				defer c.lock.Unlock()
			}

			if err := c.syncContainer(); err != nil {
				logrus.Errorf("Unable to sync container %s state: %v", c.ID(), err)
				return
			}

			// These have to be first, some of them rely on container rootfs still being mounted.
			for _, cleanupFunc := range cleanupFuncs {
				cleanupFunc()
			}
			if err := c.unmount(false); err != nil {
				logrus.Errorf("Failed to unmount container: %v", err)
			}

			if c.ensureState(define.ContainerStateConfigured, define.ContainerStateExited) {
				c.state.Mountpoint = ""
				if err := c.save(); err != nil {
					logrus.Errorf("Writing container %s state: %v", c.ID(), err)
				}
			}
		}

		// Before we proceed, mount all named volumes associated with the
		// container.
		// This solves two issues:
		// Firstly, it ensures that if the volume actually requires a mount, we
		// will mount it for safe use.
		// (For example, image volumes, volume plugins).
		// Secondly, it copies up into the volume if necessary.
		// This ensures that permissions are correct for copies into volumes on
		// containers that have never started.
		if len(c.config.NamedVolumes) > 0 {
			for _, v := range c.config.NamedVolumes {
				vol, err := c.mountNamedVolume(v, mountPoint)
				if err != nil {
					unmount()
					return nil, err
				}

				volUnmountName := fmt.Sprintf("volume unmount %s %s", vol.Name(), stringid.GenerateNonCryptoID()[0:12])

				// The unmount function can be called in two places:
				// First, from unmount(), our generic cleanup function that gets
				// called on success or on failure by error.
				// Second, from the shutdown handler on receipt of a SIGTERM
				// or similar.
				volUnmountFunc := func() error {
					vol.lock.Lock()
					defer vol.lock.Unlock()

					if err := vol.unmount(false); err != nil {
						return err
					}

					return nil
				}

				cleanupFuncs = append(cleanupFuncs, func() {
					_ = shutdown.Unregister(volUnmountName)

					if err := volUnmountFunc(); err != nil {
						logrus.Errorf("Unmounting container %s volume %s: %v", c.ID(), vol.Name(), err)
					}
				})

				if err := shutdown.Register(volUnmountName, func(_ os.Signal) error {
					return volUnmountFunc()
				}); err != nil && !errors.Is(err, shutdown.ErrHandlerExists) {
					return nil, fmt.Errorf("adding shutdown handler for volume %s unmount: %w", vol.Name(), err)
				}
			}
		}
	}

	resolvedRoot, resolvedPath, volume, err := c.resolveCopyTarget(mountPoint, path)
	if err != nil {
		unmount()
		return nil, err
	}

	if volume != nil {
		// This must be the first cleanup function so it fires before volume unmounts happen.
		cleanupFuncs = append([]func(){func() {
			// This is a gross hack to ensure correct permissions
			// on a volume that was copied into that needed, but did
			// not receive, a copy-up.
			// Why do we need this?
			// Basically: fixVolumePermissions is needed to ensure
			// the volume has the right permissions.
			// However, fixVolumePermissions only fires on a volume
			// that is not empty iff a copy-up occurred.
			// In this case, the volume is not empty as we just
			// copied into it, so in order to get
			// fixVolumePermissions to actually run, we must
			// convince it that a copy-up occurred - even if it did
			// not.
			// At the same time, clear NeedsCopyUp as we just
			// populated the volume and that will block a future
			// copy-up.
			volume.lock.Lock()
			defer volume.lock.Unlock()

			if err := volume.update(); err != nil {
				logrus.Errorf("Unable to update volume %s status: %v", volume.Name(), err)
				return
			}

			if volume.state.NeedsCopyUp && volume.state.NeedsChown {
				volume.state.NeedsCopyUp = false
				volume.state.CopiedUp = true
				if err := volume.save(); err != nil {
					logrus.Errorf("Unable to save volume %s state: %v", volume.Name(), err)
					return
				}

				for _, namedVol := range c.config.NamedVolumes {
					if namedVol.Name == volume.Name() {
						if err := c.fixVolumePermissionsUnlocked(namedVol, volume); err != nil {
							logrus.Errorf("Unable to fix volume %s permissions: %v", volume.Name(), err)
						}
						return
					}
				}
			}
		}}, cleanupFuncs...)
	}

	var idPair *idtools.IDPair
	if chown {
		// Make sure we chown the files to the container's main user and group ID.
		user, err := getContainerUser(c, mountPoint)
		if err != nil {
			unmount()
			return nil, err
		}
		idPair = &idtools.IDPair{UID: int(user.UID), GID: int(user.GID)}
	}

	decompressed, err := archive.DecompressStream(reader)
	if err != nil {
		unmount()
		return nil, err
	}

	locked = false

	logrus.Debugf("Container copy *to* %q (resolved: %q) on container %q (ID: %s)", path, resolvedPath, c.Name(), c.ID())

	return func() error {
		defer unmount()
		defer decompressed.Close()
		putOptions := buildahCopiah.PutOptions{
			UIDMap:               c.config.IDMappings.UIDMap,
			GIDMap:               c.config.IDMappings.GIDMap,
			ChownDirs:            idPair,
			ChownFiles:           idPair,
			NoOverwriteDirNonDir: noOverwriteDirNonDir,
			NoOverwriteNonDirDir: noOverwriteDirNonDir,
			Rename:               rename,
		}

		return c.joinMountAndExec(
			func() error {
				return buildahCopiah.Put(resolvedRoot, resolvedPath, putOptions, decompressed)
			},
		)
	}, nil
}

func (c *Container) copyToArchive(path string, writer io.Writer) (func() error, error) {
	var (
		mountPoint string
		unmount    func()
		err        error
	)

	// Optimization: only mount if the container is not already.
	if c.state.Mounted {
		mountPoint = c.state.Mountpoint
		unmount = func() {}
	} else {
		// NOTE: make sure to unmount in error paths.
		mountPoint, err = c.mount()
		if err != nil {
			return nil, err
		}
		unmount = func() {
			if err := c.unmount(false); err != nil {
				logrus.Errorf("Failed to unmount container: %v", err)
			}
		}
	}

	statInfo, resolvedRoot, resolvedPath, err := c.stat(mountPoint, path)
	if err != nil {
		unmount()
		return nil, err
	}

	// We optimistically chown to the host user.  In case of a hypothetical
	// container-to-container copy, the reading side will chown back to the
	// container user.
	user, err := getContainerUser(c, mountPoint)
	if err != nil {
		unmount()
		return nil, err
	}
	hostUID, hostGID, err := util.GetHostIDs(
		idtoolsToRuntimeSpec(c.config.IDMappings.UIDMap),
		idtoolsToRuntimeSpec(c.config.IDMappings.GIDMap),
		user.UID,
		user.GID,
	)
	if err != nil {
		unmount()
		return nil, err
	}
	idPair := idtools.IDPair{UID: int(hostUID), GID: int(hostGID)}

	logrus.Debugf("Container copy *from* %q (resolved: %q) on container %q (ID: %s)", path, resolvedPath, c.Name(), c.ID())

	return func() error {
		defer unmount()
		getOptions := buildahCopiah.GetOptions{
			// Unless the specified points to ".", we want to copy the base directory.
			KeepDirectoryNames: statInfo.IsDir && filepath.Base(path) != ".",
			UIDMap:             c.config.IDMappings.UIDMap,
			GIDMap:             c.config.IDMappings.GIDMap,
			ChownDirs:          &idPair,
			ChownFiles:         &idPair,
			Excludes:           []string{"dev", "proc", "sys"},
			// Ignore EPERMs when copying from rootless containers
			// since we cannot read TTY devices.  Those are owned
			// by the host's root and hence "nobody" inside the
			// container's user namespace.
			IgnoreUnreadable: rootless.IsRootless() && c.state.State == define.ContainerStateRunning,
		}
		return c.joinMountAndExec(
			func() error {
				return buildahCopiah.Get(resolvedRoot, "", getOptions, []string{resolvedPath}, writer)
			},
		)
	}, nil
}

// getContainerUser returns the specs.User and ID mappings of the container.
func getContainerUser(container *Container, mountPoint string) (specs.User, error) {
	userspec := container.config.User

	uid, gid, _, err := chrootuser.GetUser(mountPoint, userspec)
	u := specs.User{
		UID:      uid,
		GID:      gid,
		Username: userspec,
	}

	if !strings.Contains(userspec, ":") {
		groups, err2 := chrootuser.GetAdditionalGroupsForUser(mountPoint, uint64(u.UID))
		if err2 != nil {
			if !errors.Is(err2, chrootuser.ErrNoSuchUser) && err == nil {
				err = err2
			}
		} else {
			u.AdditionalGids = groups
		}
	}

	return u, err
}

// idtoolsToRuntimeSpec converts idtools ID mapping to the one of the runtime spec.
func idtoolsToRuntimeSpec(idMaps []idtools.IDMap) (convertedIDMap []specs.LinuxIDMapping) {
	for _, idmap := range idMaps {
		tempIDMap := specs.LinuxIDMapping{
			ContainerID: uint32(idmap.ContainerID),
			HostID:      uint32(idmap.HostID),
			Size:        uint32(idmap.Size),
		}
		convertedIDMap = append(convertedIDMap, tempIDMap)
	}
	return convertedIDMap
}