MCPcopy
hub / github.com/opencontainers/runc / doTmpfsCopyUp

Function doTmpfsCopyUp

libcontainer/rootfs_linux.go:436–492  ·  view source on GitHub ↗
(m mountEntry, mountLabel string)

Source from the content-addressed store, hash-verified

434}
435
436func doTmpfsCopyUp(m mountEntry, mountLabel string) (Err error) {
437 // Set up a scratch dir for the tmpfs on the host.
438 tmpdir, err := prepareTmp("/tmp")
439 if err != nil {
440 return fmt.Errorf("tmpcopyup: failed to setup tmpdir: %w", err)
441 }
442 defer cleanupTmp(tmpdir)
443 tmpDir, err := os.MkdirTemp(tmpdir, "runctmpdir")
444 if err != nil {
445 return fmt.Errorf("tmpcopyup: failed to create tmpdir: %w", err)
446 }
447 defer os.RemoveAll(tmpDir)
448
449 tmpDirFile, err := os.OpenFile(tmpDir, unix.O_DIRECTORY|unix.O_CLOEXEC, 0)
450 if err != nil {
451 return fmt.Errorf("tmpcopyup: %w", err)
452 }
453 defer tmpDirFile.Close()
454
455 hostRootFd, err := os.OpenFile("/", unix.O_DIRECTORY|unix.O_CLOEXEC|unix.O_PATH, 0)
456 if err != nil {
457 return fmt.Errorf("tmpcopyup: open host root: %w", err)
458 }
459 defer hostRootFd.Close()
460
461 // Configure the *host* tmpdir as if it's the container mount. We change
462 // m.dstFile since we are going to mount *on the host*.
463 hostMount := mountEntry{
464 Mount: m.Mount,
465 dstFile: tmpDirFile,
466 }
467 if err := hostMount.mountPropagate(hostRootFd, mountLabel); err != nil {
468 return err
469 }
470 hostRootFd.Close()
471 defer func() {
472 if Err != nil {
473 if err := unmount(tmpDir, unix.MNT_DETACH); err != nil {
474 logrus.Warnf("tmpcopyup: %v", err)
475 }
476 }
477 }()
478
479 return utils.WithProcfdFile(m.dstFile, func(dstFd string) (Err error) {
480 // Copy the container data to the host tmpdir. We append "/" to force
481 // CopyDirectory to resolve the symlink rather than trying to copy the
482 // symlink itself.
483 if err := fileutils.CopyDirectory(dstFd+"/", tmpDir); err != nil {
484 return fmt.Errorf("tmpcopyup: failed to copy %s to %s (%s): %w", m.Destination, dstFd, tmpDir, err)
485 }
486 // Now move the mount into the container.
487 if err := mountViaFds(tmpDir, nil, m.Destination, dstFd, "", unix.MS_MOVE, ""); err != nil {
488 return fmt.Errorf("tmpcopyup: failed to move mount: %w", err)
489 }
490 return nil
491 })
492}
493

Callers 1

mountToRootfsFunction · 0.85

Calls 7

mountPropagateMethod · 0.95
WithProcfdFileFunction · 0.92
prepareTmpFunction · 0.85
cleanupTmpFunction · 0.85
unmountFunction · 0.85
mountViaFdsFunction · 0.85
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…