Skip to content

Commit

Permalink
Add mount functionality to 'Directory'
Browse files Browse the repository at this point in the history
This is needed for 'chroot' runners that must mount the special
filesystems '/proc' and '/sys' in the input root.
The necessary syscalls are not yet merged into the 'sys' package,
so this includes a patch while we work to upstream the code.
  • Loading branch information
stagnation committed Sep 15, 2023
1 parent c04246b commit 4269bbc
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@bazel_gazelle//:def.bzl", "gazelle")
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
load("@npm//:defs.bzl", "npm_link_all_packages", "npm_link_targets")
load("@npm//:defs.bzl", "npm_link_all_packages")

# gazelle:prefix github.com/buildbarn/bb-storage
# gazelle:resolve proto build/bazel/remote/execution/v2/remote_execution.proto @com_github_bazelbuild_remote_apis//build/bazel/remote/execution/v2:remote_execution_proto
Expand Down
1 change: 1 addition & 0 deletions go_dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,7 @@ def go_dependencies():
name = "org_golang_x_sys",
importpath = "golang.org/x/sys",
sum = "h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=",
patches = ["//:patches/org_golang_x_sys/unix_add_Fsconfig_syscall_on_linux.patch"],
version = "v0.10.0",
)
go_repository(
Expand Down
6 changes: 6 additions & 0 deletions pkg/filesystem/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ type Directory interface {
// Function that base types may use to implement calls that
// require double dispatching, such as hardlinking and renaming.
Apply(arg interface{}) error

// Mount and Unmount.
// This uses `Mountat` functionality, but not `Unmountat`.
// see https://github.com/buildbarn/bb-remote-execution/issues/115 for information.
Mount(mountpoint path.Component, source string, fstype string) error
Unmount(mountpoint path.Component) error
}

// DirectoryCloser is a Directory handle that can be released.
Expand Down
4 changes: 4 additions & 0 deletions pkg/filesystem/local_directory_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ func (d *localDirectory) Mknod(name path.Component, perm os.FileMode, deviceNumb
func clonefileImpl(oldFD int, oldName string, newFD int, newName string) error {
return unix.Clonefileat(oldFD, oldName, newFD, newName, unix.CLONE_NOFOLLOW)
}

func (d *localDirectory) Mount(mountpoint path.Component, source string, fstype string) error {
return status.Error(codes.Unimplemented, "Mount is not supported")
}
4 changes: 4 additions & 0 deletions pkg/filesystem/local_directory_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ func (d *localDirectory) Mknod(name path.Component, perm os.FileMode, deviceNumb
func clonefileImpl(oldFD int, oldName string, newFD int, newName string) error {
return status.Error(codes.Unimplemented, "Clonefile is only supported on Darwin")
}

func (d *localDirectory) Mount(mountpoint path.Component, source string, fstype string) error {
return status.Error(codes.Unimplemented, "Mount is not supported")
}
36 changes: 36 additions & 0 deletions pkg/filesystem/local_directory_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"runtime"

"github.com/buildbarn/bb-storage/pkg/filesystem/path"
"github.com/buildbarn/bb-storage/pkg/util"

"golang.org/x/sys/unix"
"google.golang.org/grpc/codes"
Expand All @@ -34,3 +35,38 @@ func (d *localDirectory) Mknod(name path.Component, perm os.FileMode, deviceNumb
func clonefileImpl(oldFD int, oldName string, newFD int, newName string) error {
return status.Error(codes.Unimplemented, "Clonefile is only supported on Darwin")
}

func (d *localDirectory) Mount(mountpoint path.Component, source string, fstype string) error {
// NB: `Fsmount` creates a file descriptor to the mount object,
// that can be used to move it again.
// We do not use it,
// unmount will fail with `EBUSY`
// if it is left open.
mountname := mountpoint.String()
fd, err := unix.Fsopen(fstype, unix.FSOPEN_CLOEXEC)
if err != nil {
return util.StatusWrapf(err, "Fsopen '%s'", fstype)
}

err = unix.Fsconfig(fd, unix.FSCONFIG_SET_STRING, "source", source, 0)
if err != nil {
return util.StatusWrapf(err, "Fsconfig source '%s'", source)
}

err = unix.Fsconfig(fd, unix.FSCONFIG_CMD_CREATE, "", "", 0)
if err != nil {
return util.StatusWrap(err, "Fsconfig create")
}

mfd, err := unix.Fsmount(fd, unix.FSMOUNT_CLOEXEC, unix.MS_NOEXEC)
defer unix.Close(mfd)
if err != nil {
return util.StatusWrap(err, "Fsmount")
}
err = unix.MoveMount(mfd, "", d.fd, mountname, unix.MOVE_MOUNT_F_EMPTY_PATH)
if err != nil {
return util.StatusWrapf(err, "Movemount mountname '%s' in file descriptor %d", mountname, d.fd)
}

return nil
}
4 changes: 2 additions & 2 deletions pkg/filesystem/local_directory_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (d *localDirectory) Remove(name path.Component) error {

var workingDirectoryLock sync.Mutex

func (d *localDirectory) unmount(name path.Component) error {
func (d *localDirectory) Unmount(name path.Component) error {
defer runtime.KeepAlive(d)

// POSIX systems provide no umountat() system call that permits
Expand Down Expand Up @@ -302,7 +302,7 @@ func (d *localDirectory) removeAllChildren(parentDeviceNumber rawDeviceNumber) e
// unmount until the remaining directory is on the same
// file system.
for parentDeviceNumber != childDeviceNumber {
if err := d.unmount(component); err != nil {
if err := d.Unmount(component); err != nil {
return err
}
fileType, childDeviceNumber, _, err = d.lstat(component)
Expand Down
8 changes: 8 additions & 0 deletions pkg/filesystem/local_directory_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,3 +978,11 @@ func (d *localDirectory) Apply(arg interface{}) error {
return syscall.EXDEV
}
}

func (d *localDirectory) Mount(mountpoint path.Component, source string, fstype string) error {
return status.Error(codes.Unimplemented, "Mount is not supported")
}

func (d *localDirectory) Unmount(mountpoint path.Component) error {
return status.Error(codes.Unimplemented, "Unmount is not supported")
}

0 comments on commit 4269bbc

Please sign in to comment.