|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package mount |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os/exec" |
| 22 | + |
| 23 | + "github.com/containerd/containerd/errdefs" |
| 24 | + "github.com/containerd/containerd/log" |
| 25 | + "github.com/pkg/errors" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + // ErrNotImplementOnUnix is returned for methods that are not implemented |
| 30 | + ErrNotImplementOnUnix = errors.New("not implemented under darwin") |
| 31 | + // allowedHelperBinaries is a list of third-party executable, which can |
| 32 | + // be extended in future uses, e.g., mount_nfs, mount_9p. `mount_darwin` |
| 33 | + // is a special case where we will use the internal darwin snapshotter. |
| 34 | + allowedHelperBinaries = []string{"mount_darwin"} |
| 35 | +) |
| 36 | + |
| 37 | +// Mount to the provided target path. |
| 38 | +// |
| 39 | +func (m *Mount) Mount(target string) error { |
| 40 | + var allowed = false |
| 41 | + log.L.Debugf("mount: %s, src %s, type %s", target, m.Source, m.Type) |
| 42 | + |
| 43 | + binary := fmt.Sprintf("mount_%s", m.Type) |
| 44 | + if m.Type != "darwin" { |
| 45 | + if _, err := exec.LookPath(binary); err != nil { |
| 46 | + return errors.Wrapf(errdefs.ErrNotFound, "mount binary not found: '%s(%s)'", m.Type, binary) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + for _, helperBinary := range allowedHelperBinaries { |
| 51 | + if binary == helperBinary { |
| 52 | + allowed = true |
| 53 | + } |
| 54 | + } |
| 55 | + if !allowed { |
| 56 | + return errors.Wrapf(ErrNotImplementOnUnix, "mount binary isn't allowed to use: '%s(%s)'", m.Type, binary) |
| 57 | + } |
| 58 | + |
| 59 | + switch m.Type { |
| 60 | + case "darwin": |
| 61 | + return AttachSparseBundle(target, m.Source, m.Options) |
| 62 | + default: |
| 63 | + // if allowedHelperBinaries is extended, handle it here |
| 64 | + } |
| 65 | + |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +// Unmount the provided mount path with the flags |
| 70 | +func Unmount(target string, flags int) error { |
| 71 | + return unmount(target, flags) |
| 72 | +} |
| 73 | + |
| 74 | +// UnmountAll the provided mount path with the flags |
| 75 | +func UnmountAll(target string, flags int) error { |
| 76 | + return unmount(target, flags) |
| 77 | +} |
| 78 | + |
| 79 | +func unmount(target string, flags int) error { |
| 80 | + log.L.Debugf("unmount: %s", target) |
| 81 | + if err := DetachSparseBundle(target, flags); err != nil { |
| 82 | + log.L.Infof("detach failed (ignored): %s", target) |
| 83 | + } |
| 84 | + return nil |
| 85 | +} |
0 commit comments