|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright The containerd Authors. |
| 5 | +
|
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +// mountutil performs local mounts on Linux. This package should likely |
| 20 | +// be replaced with functions in the containerd mount code. |
| 21 | +package mountutil |
| 22 | + |
| 23 | +import ( |
| 24 | + "bytes" |
| 25 | + "context" |
| 26 | + "fmt" |
| 27 | + "os" |
| 28 | + "path/filepath" |
| 29 | + "strings" |
| 30 | + "text/template" |
| 31 | + "time" |
| 32 | + |
| 33 | + types "github.com/containerd/containerd/api/types" |
| 34 | + "github.com/containerd/containerd/v2/core/mount" |
| 35 | + "github.com/containerd/log" |
| 36 | +) |
| 37 | + |
| 38 | +func All(ctx context.Context, rootfs, mdir string, mounts []*types.Mount) (retErr error) { |
| 39 | + log.G(ctx).WithField("mounts", mounts).Debugf("mounting rootfs components") |
| 40 | + active := []mount.ActiveMount{} |
| 41 | + |
| 42 | + // TODO: Use mount manager interface, mount temps to directory |
| 43 | + for i, m := range mounts { |
| 44 | + var target string |
| 45 | + if i < len(mounts)-1 { |
| 46 | + target = filepath.Join(mdir, fmt.Sprintf("%d", i)) |
| 47 | + if err := os.MkdirAll(target, 0711); err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + } else { |
| 51 | + target = rootfs |
| 52 | + } |
| 53 | + if t, ok := strings.CutPrefix(m.Type, "format/"); ok { |
| 54 | + m.Type = t |
| 55 | + for i, o := range m.Options { |
| 56 | + format := formatString(o) |
| 57 | + if format != nil { |
| 58 | + s, err := format(active) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("formatting mount option %q: %w", o, err) |
| 61 | + } |
| 62 | + m.Options[i] = s |
| 63 | + } |
| 64 | + } |
| 65 | + if format := formatString(m.Source); format != nil { |
| 66 | + s, err := format(active) |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("formatting mount source %q: %w", m.Source, err) |
| 69 | + } |
| 70 | + m.Source = s |
| 71 | + } |
| 72 | + if format := formatString(m.Target); format != nil { |
| 73 | + s, err := format(active) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("formatting mount target %q: %w", m.Target, err) |
| 76 | + } |
| 77 | + m.Target = s |
| 78 | + } |
| 79 | + } |
| 80 | + if t, ok := strings.CutPrefix(m.Type, "mkdir/"); ok { |
| 81 | + m.Type = t |
| 82 | + var options []string |
| 83 | + for _, o := range m.Options { |
| 84 | + if strings.HasPrefix(o, "X-containerd.mkdir.") { |
| 85 | + prefix := "X-containerd.mkdir.path=" |
| 86 | + if !strings.HasPrefix(o, prefix) { |
| 87 | + return fmt.Errorf("unknown mkdir mount option %q", o) |
| 88 | + } |
| 89 | + part := strings.SplitN(o[len(prefix):], ":", 4) |
| 90 | + switch len(part) { |
| 91 | + case 4: |
| 92 | + // TODO: Support setting uid/gid |
| 93 | + fallthrough |
| 94 | + case 3: |
| 95 | + fallthrough |
| 96 | + case 2: |
| 97 | + fallthrough |
| 98 | + case 1: |
| 99 | + dir := part[0] |
| 100 | + if !strings.HasPrefix(dir, mdir) { |
| 101 | + return fmt.Errorf("mkdir mount source %q must be under %q", dir, mdir) |
| 102 | + } |
| 103 | + if err := os.MkdirAll(dir, 0755); err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + default: |
| 107 | + return fmt.Errorf("invalid mkdir mount option %q", o) |
| 108 | + } |
| 109 | + } else { |
| 110 | + options = append(options, o) |
| 111 | + } |
| 112 | + } |
| 113 | + m.Options = options |
| 114 | + |
| 115 | + } |
| 116 | + t := time.Now() |
| 117 | + am := mount.ActiveMount{ |
| 118 | + Mount: mount.Mount{ |
| 119 | + Type: m.Type, |
| 120 | + Source: m.Source, |
| 121 | + Target: m.Target, |
| 122 | + Options: m.Options, |
| 123 | + }, |
| 124 | + MountedAt: &t, |
| 125 | + MountPoint: target, |
| 126 | + } |
| 127 | + if err := am.Mount.Mount(target); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + active = append(active, am) |
| 131 | + |
| 132 | + } |
| 133 | + defer func() { |
| 134 | + if retErr != nil { |
| 135 | + for i := len(active) - 1; i >= 0; i-- { |
| 136 | + // TODO: delegate custom types to handlers |
| 137 | + if active[i].Type == "mkdir" { |
| 138 | + continue |
| 139 | + } |
| 140 | + if err := mount.UnmountAll(active[i].MountPoint, 0); err != nil { |
| 141 | + log.G(ctx).WithError(err).WithField("mountpoint", active[i].MountPoint).Warn("failed to cleanup mount") |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + }() |
| 146 | + |
| 147 | + return nil |
| 148 | +} |
| 149 | + |
| 150 | +const formatCheck = "{{" |
| 151 | + |
| 152 | +func formatString(s string) func([]mount.ActiveMount) (string, error) { |
| 153 | + if !strings.Contains(s, formatCheck) { |
| 154 | + return nil |
| 155 | + } |
| 156 | + |
| 157 | + return func(a []mount.ActiveMount) (string, error) { |
| 158 | + fm := template.FuncMap{ |
| 159 | + "source": func(i int) (string, error) { |
| 160 | + if i < 0 || i >= len(a) { |
| 161 | + return "", fmt.Errorf("index out of bounds: %d, has %d active mounts", i, len(a)) |
| 162 | + } |
| 163 | + return a[i].Source, nil |
| 164 | + }, |
| 165 | + "target": func(i int) (string, error) { |
| 166 | + if i < 0 || i >= len(a) { |
| 167 | + return "", fmt.Errorf("index out of bounds: %d, has %d active mounts", i, len(a)) |
| 168 | + } |
| 169 | + return a[i].Target, nil |
| 170 | + }, |
| 171 | + "mount": func(i int) (string, error) { |
| 172 | + if i < 0 || i >= len(a) { |
| 173 | + return "", fmt.Errorf("index out of bounds: %d, has %d active mounts", i, len(a)) |
| 174 | + } |
| 175 | + return a[i].MountPoint, nil |
| 176 | + }, |
| 177 | + "overlay": func(start, end int) (string, error) { |
| 178 | + var dirs []string |
| 179 | + if start > end { |
| 180 | + if start >= len(a) || end < 0 { |
| 181 | + return "", fmt.Errorf("invalid range: %d-%d, has %d active mounts", start, end, len(a)) |
| 182 | + } |
| 183 | + for i := start; i >= end; i-- { |
| 184 | + dirs = append(dirs, a[i].MountPoint) |
| 185 | + } |
| 186 | + } else { |
| 187 | + if start < 0 || end >= len(a) { |
| 188 | + return "", fmt.Errorf("invalid range: %d-%d, has %d active mounts", start, end, len(a)) |
| 189 | + } |
| 190 | + for i := start; i <= end; i++ { |
| 191 | + dirs = append(dirs, a[i].MountPoint) |
| 192 | + } |
| 193 | + } |
| 194 | + return strings.Join(dirs, ":"), nil |
| 195 | + }, |
| 196 | + } |
| 197 | + t, err := template.New("").Funcs(fm).Parse(s) |
| 198 | + if err != nil { |
| 199 | + return "", err |
| 200 | + } |
| 201 | + |
| 202 | + buf := bytes.NewBuffer(nil) |
| 203 | + if err := t.Execute(buf, nil); err != nil { |
| 204 | + return "", err |
| 205 | + } |
| 206 | + return buf.String(), nil |
| 207 | + } |
| 208 | +} |
0 commit comments