Skip to content

Commit d6baafa

Browse files
committed
mount: basic support for FreeBSD
Signed-off-by: Samuel Karp <[email protected]>
1 parent dc207b6 commit d6baafa

2 files changed

Lines changed: 138 additions & 1 deletion

File tree

mount/mount_freebsd.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// +build freebsd
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+
package mount
20+
21+
import (
22+
"os"
23+
"os/exec"
24+
"time"
25+
26+
"github.com/pkg/errors"
27+
"golang.org/x/sys/unix"
28+
)
29+
30+
var (
31+
// ErrNotImplementOnUnix is returned for methods that are not implemented
32+
ErrNotImplementOnUnix = errors.New("not implemented under unix")
33+
)
34+
35+
// Mount to the provided target path.
36+
func (m *Mount) Mount(target string) error {
37+
// The "syscall" and "golang.org/x/sys/unix" packages do not define a Mount
38+
// function for FreeBSD, so instead we execute mount(8) and trust it to do
39+
// the right thing
40+
return m.mountWithHelper(target)
41+
}
42+
43+
func (m *Mount) mountWithHelper(target string) error {
44+
// target: "/foo/target"
45+
// command: "mount -o ro -t nullfs /foo/source /foo/merged"
46+
// Note: FreeBSD mount(8) is particular about the order of flags and arguments
47+
var args []string
48+
for _, o := range m.Options {
49+
args = append(args, "-o", o)
50+
}
51+
args = append(args, "-t", m.Type)
52+
args = append(args, m.Source, target)
53+
54+
infoBeforeMount, err := Lookup(target)
55+
if err != nil {
56+
return err
57+
}
58+
59+
// cmd.CombinedOutput() may intermittently return ECHILD because of our signal handling in shim.
60+
// See #4387 and wait(2).
61+
const retriesOnECHILD = 10
62+
for i := 0; i < retriesOnECHILD; i++ {
63+
cmd := exec.Command("mount", args...)
64+
out, err := cmd.CombinedOutput()
65+
if err == nil {
66+
return nil
67+
}
68+
if !errors.Is(err, unix.ECHILD) {
69+
return errors.Wrapf(err, "mount [%v] failed: %q", args, string(out))
70+
}
71+
// We got ECHILD, we are not sure whether the mount was successful.
72+
// If the mount ID has changed, we are sure we got some new mount, but still not sure it is fully completed.
73+
// So we attempt to unmount the new mount before retrying.
74+
infoAfterMount, err := Lookup(target)
75+
if err != nil {
76+
return err
77+
}
78+
if infoAfterMount.ID != infoBeforeMount.ID {
79+
_ = unmount(target, 0)
80+
}
81+
}
82+
return errors.Errorf("mount [%v] failed with ECHILD (retired %d times)", args, retriesOnECHILD)
83+
}
84+
85+
// Unmount the provided mount path with the flags
86+
func Unmount(target string, flags int) error {
87+
if err := unmount(target, flags); err != nil && err != unix.EINVAL {
88+
return err
89+
}
90+
return nil
91+
}
92+
93+
func unmount(target string, flags int) error {
94+
for i := 0; i < 50; i++ {
95+
if err := unix.Unmount(target, flags); err != nil {
96+
switch err {
97+
case unix.EBUSY:
98+
time.Sleep(50 * time.Millisecond)
99+
continue
100+
default:
101+
return err
102+
}
103+
}
104+
return nil
105+
}
106+
return errors.Wrapf(unix.EBUSY, "failed to unmount target %s", target)
107+
}
108+
109+
// UnmountAll repeatedly unmounts the given mount point until there
110+
// are no mounts remaining (EINVAL is returned by mount), which is
111+
// useful for undoing a stack of mounts on the same mount point.
112+
// UnmountAll all is noop when the first argument is an empty string.
113+
// This is done when the containerd client did not specify any rootfs
114+
// mounts (e.g. because the rootfs is managed outside containerd)
115+
// UnmountAll is noop when the mount path does not exist.
116+
func UnmountAll(mount string, flags int) error {
117+
if mount == "" {
118+
return nil
119+
}
120+
if _, err := os.Stat(mount); os.IsNotExist(err) {
121+
return nil
122+
}
123+
124+
for {
125+
if err := unmount(mount, flags); err != nil {
126+
// EINVAL is returned if the target is not a
127+
// mount point, indicating that we are
128+
// done. It can also indicate a few other
129+
// things (such as invalid flags) which we
130+
// unfortunately end up squelching here too.
131+
if err == unix.EINVAL {
132+
return nil
133+
}
134+
return err
135+
}
136+
}
137+
}

mount/mount_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build darwin freebsd openbsd
1+
// +build darwin openbsd
22

33
/*
44
Copyright The containerd Authors.

0 commit comments

Comments
 (0)