WithTempMount mounts the provided mounts to a temp dir, and pass the temp dir to f. The mounts are valid during the call to the f. Finally we will unmount and remove the temp dir regardless of the result of f. NOTE: The volatile option of overlayfs doesn't allow to mount again using the same upper
(ctx context.Context, mounts []Mount, f func(root string) error)
| 35 | // same upper / work dirs. Since it's a temp mount, avoid using that option here |
| 36 | // if found. |
| 37 | func WithTempMount(ctx context.Context, mounts []Mount, f func(root string) error) (err error) { |
| 38 | root, uerr := os.MkdirTemp(tempMountLocation, "containerd-mount") |
| 39 | if uerr != nil { |
| 40 | return fmt.Errorf("failed to create temp dir: %w", uerr) |
| 41 | } |
| 42 | // We use Remove here instead of RemoveAll. |
| 43 | // The RemoveAll will delete the temp dir and all children it contains. |
| 44 | // When the Unmount fails, RemoveAll will incorrectly delete data from |
| 45 | // the mounted dir. However, if we use Remove, even though we won't |
| 46 | // successfully delete the temp dir and it may leak, we won't loss data |
| 47 | // from the mounted dir. |
| 48 | // For details, please refer to #1868 #1785. |
| 49 | defer func() { |
| 50 | if uerr = os.Remove(root); uerr != nil { |
| 51 | log.G(ctx).WithError(uerr).WithField("dir", root).Error("failed to remove mount temp dir") |
| 52 | } |
| 53 | }() |
| 54 | |
| 55 | // We should do defer first, if not we will not do Unmount when only a part of Mounts are failed. |
| 56 | defer func() { |
| 57 | if uerr = UnmountMounts(mounts, root, 0); uerr != nil { |
| 58 | uerr = fmt.Errorf("failed to unmount %s: %w", root, uerr) |
| 59 | if err == nil { |
| 60 | err = uerr |
| 61 | } else { |
| 62 | err = fmt.Errorf("%s: %w", uerr.Error(), err) |
| 63 | } |
| 64 | } |
| 65 | }() |
| 66 | |
| 67 | if uerr = All(RemoveVolatileOption(mounts), root); uerr != nil { |
| 68 | return fmt.Errorf("failed to mount %s: %w", root, uerr) |
| 69 | } |
| 70 | if err := f(root); err != nil { |
| 71 | return fmt.Errorf("mount callback failed on %s: %w", root, err) |
| 72 | } |
| 73 | return nil |
| 74 | } |
| 75 | |
| 76 | // RemoveVolatileOption copies and remove the volatile option for overlay |
| 77 | // type, since overlayfs doesn't allow to mount again using the same upper/work |
searching dependent graphs…