switchroot_linux.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package mounttree // import "github.com/docker/docker/internal/mounttree"
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/moby/sys/mount"
  7. "github.com/moby/sys/mountinfo"
  8. "golang.org/x/sys/unix"
  9. )
  10. // SwitchRoot changes path to be the root of the mount tree and changes the
  11. // current working directory to the new root.
  12. //
  13. // This function bind-mounts onto path; it is the caller's responsibility to set
  14. // the desired propagation mode of path's parent mount beforehand to prevent
  15. // unwanted propagation into different mount namespaces.
  16. func SwitchRoot(path string) error {
  17. if mounted, _ := mountinfo.Mounted(path); !mounted {
  18. if err := mount.Mount(path, path, "bind", "rbind,rw"); err != nil {
  19. return realChroot(path)
  20. }
  21. }
  22. // setup oldRoot for pivot_root
  23. pivotDir, err := os.MkdirTemp(path, ".pivot_root")
  24. if err != nil {
  25. return fmt.Errorf("Error setting up pivot dir: %v", err)
  26. }
  27. var mounted bool
  28. defer func() {
  29. if mounted {
  30. // make sure pivotDir is not mounted before we try to remove it
  31. if errCleanup := unix.Unmount(pivotDir, unix.MNT_DETACH); errCleanup != nil {
  32. if err == nil {
  33. err = errCleanup
  34. }
  35. return
  36. }
  37. }
  38. errCleanup := os.Remove(pivotDir)
  39. // pivotDir doesn't exist if pivot_root failed and chroot+chdir was successful
  40. // because we already cleaned it up on failed pivot_root
  41. if errCleanup != nil && !os.IsNotExist(errCleanup) {
  42. errCleanup = fmt.Errorf("Error cleaning up after pivot: %v", errCleanup)
  43. if err == nil {
  44. err = errCleanup
  45. }
  46. }
  47. }()
  48. if err := unix.PivotRoot(path, pivotDir); err != nil {
  49. // If pivot fails, fall back to the normal chroot after cleaning up temp dir
  50. if err := os.Remove(pivotDir); err != nil {
  51. return fmt.Errorf("Error cleaning up after failed pivot: %v", err)
  52. }
  53. return realChroot(path)
  54. }
  55. mounted = true
  56. // This is the new path for where the old root (prior to the pivot) has been moved to
  57. // This dir contains the rootfs of the caller, which we need to remove so it is not visible during extraction
  58. pivotDir = filepath.Join("/", filepath.Base(pivotDir))
  59. if err := unix.Chdir("/"); err != nil {
  60. return fmt.Errorf("Error changing to new root: %v", err)
  61. }
  62. // Make the pivotDir (where the old root lives) private so it can be unmounted without propagating to the host
  63. if err := unix.Mount("", pivotDir, "", unix.MS_PRIVATE|unix.MS_REC, ""); err != nil {
  64. return fmt.Errorf("Error making old root private after pivot: %v", err)
  65. }
  66. // Now unmount the old root so it's no longer visible from the new root
  67. if err := unix.Unmount(pivotDir, unix.MNT_DETACH); err != nil {
  68. return fmt.Errorf("Error while unmounting old root after pivot: %v", err)
  69. }
  70. mounted = false
  71. return nil
  72. }
  73. func realChroot(path string) error {
  74. if err := unix.Chroot(path); err != nil {
  75. return fmt.Errorf("Error after fallback to chroot: %v", err)
  76. }
  77. if err := unix.Chdir("/"); err != nil {
  78. return fmt.Errorf("Error changing to new root after chroot: %v", err)
  79. }
  80. return nil
  81. }