Use mount.Target to specify subdirectory of rootfs mount
- Add Target to mount.Mount. - Add UnmountMounts to unmount a list of mounts in reverse order. - Add UnmountRecursive to unmount deepest mount first for a given target, using moby/sys/mountinfo. Signed-off-by: Edgar Lee <edgarhinshunlee@gmail.com>
This commit is contained in:
@@ -172,3 +172,67 @@ func TestMountAt(t *testing.T) {
|
||||
t.Fatalf("unexpected working directory: %s", newWD)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmountMounts(t *testing.T) {
|
||||
testutil.RequiresRoot(t)
|
||||
|
||||
target, mounts := setupMounts(t)
|
||||
if err := UnmountMounts(mounts, target, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmountRecursive(t *testing.T) {
|
||||
testutil.RequiresRoot(t)
|
||||
|
||||
target, _ := setupMounts(t)
|
||||
if err := UnmountRecursive(target, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func setupMounts(t *testing.T) (target string, mounts []Mount) {
|
||||
dir1 := t.TempDir()
|
||||
dir2 := t.TempDir()
|
||||
|
||||
if err := os.Mkdir(filepath.Join(dir1, "foo"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mounts = append(mounts, Mount{
|
||||
Type: "bind",
|
||||
Source: dir1,
|
||||
Options: []string{
|
||||
"ro",
|
||||
"rbind",
|
||||
},
|
||||
})
|
||||
|
||||
if err := os.WriteFile(filepath.Join(dir2, "bar"), []byte("bar"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mounts = append(mounts, Mount{
|
||||
Type: "bind",
|
||||
Source: dir2,
|
||||
Target: "foo",
|
||||
Options: []string{
|
||||
"ro",
|
||||
"rbind",
|
||||
},
|
||||
})
|
||||
|
||||
target = t.TempDir()
|
||||
if err := All(mounts, target); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
b, err := os.ReadFile(filepath.Join(target, "foo/bar"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if string(b) != "bar" {
|
||||
t.Fatalf("unexpected file content: %s", b)
|
||||
}
|
||||
|
||||
return target, mounts
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user