Replace mount fork hack with CLONE_FS

This change spins up a new goroutine, locks it to a thread, then
unshares CLONE_FS which allows us to `Chdir` from inside the thread
without affecting the rest of the program.

The thread is no longer usable after unshare so it leaves the thread
locked to prevent go from returning the thread to the thread pool.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff
2022-10-11 23:47:16 +00:00
parent c21d1baa88
commit a24ef09937
7 changed files with 68 additions and 406 deletions

View File

@@ -25,6 +25,7 @@ import (
"github.com/containerd/continuity/testutil"
exec "golang.org/x/sys/execabs"
"golang.org/x/sys/unix"
)
func TestLongestCommonPrefix(t *testing.T) {
@@ -126,3 +127,48 @@ func TestFUSEHelper(t *testing.T) {
t.Fatal(err)
}
}
func TestMountAt(t *testing.T) {
testutil.RequiresRoot(t)
dir1 := t.TempDir()
dir2 := t.TempDir()
defer unix.Unmount(filepath.Join(dir2, "bar"), unix.MNT_DETACH)
if err := os.WriteFile(filepath.Join(dir1, "foo"), []byte("foo"), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir2, "bar"), []byte{}, 0644); err != nil {
t.Fatal(err)
}
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
// mount ${dir1}/foo at ${dir2}/bar
// But since we are using `mountAt` we only need to specify the relative path to dir2 as the target mountAt will chdir to there.
if err := mountAt(dir2, filepath.Join(dir1, "foo"), "bar", "none", unix.MS_BIND, ""); err != nil {
t.Fatal(err)
}
b, err := os.ReadFile(filepath.Join(dir2, "bar"))
if err != nil {
t.Fatal(err)
}
if string(b) != "foo" {
t.Fatalf("unexpected file content: %s", b)
}
newWD, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
if wd != newWD {
t.Fatalf("unexpected working directory: %s", newWD)
}
}