idmap: improve error messages

add more context to errors returned by unix.* functions

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano 2024-07-29 10:18:44 +02:00
parent a85af63434
commit 175c6e0745
No known key found for this signature in database
GPG Key ID: 67E38F7A8BA21772
1 changed files with 9 additions and 4 deletions

View File

@ -4,7 +4,9 @@
package idmap package idmap
import ( import (
"errors"
"fmt" "fmt"
"io/fs"
"os" "os"
"runtime" "runtime"
"syscall" "syscall"
@ -26,7 +28,7 @@ func CreateIDMappedMount(source, target string, pid int) error {
targetDirFd, err := unix.OpenTree(0, source, unix.OPEN_TREE_CLONE) targetDirFd, err := unix.OpenTree(0, source, unix.OPEN_TREE_CLONE)
if err != nil { if err != nil {
return err return &os.PathError{Op: "open_tree", Path: source, Err: err}
} }
defer unix.Close(targetDirFd) defer unix.Close(targetDirFd)
@ -35,13 +37,16 @@ func CreateIDMappedMount(source, target string, pid int) error {
Attr_set: unix.MOUNT_ATTR_IDMAP, Attr_set: unix.MOUNT_ATTR_IDMAP,
Userns_fd: uint64(userNsFile.Fd()), Userns_fd: uint64(userNsFile.Fd()),
}); err != nil { }); err != nil {
return err return &os.PathError{Op: "mount_setattr", Path: source, Err: err}
} }
if err := os.Mkdir(target, 0o700); err != nil && !os.IsExist(err) { if err := os.Mkdir(target, 0o700); err != nil && !errors.Is(err, fs.ErrExist) {
return err return err
} }
return unix.MoveMount(targetDirFd, "", 0, target, unix.MOVE_MOUNT_F_EMPTY_PATH) if err := unix.MoveMount(targetDirFd, "", 0, target, unix.MOVE_MOUNT_F_EMPTY_PATH); err != nil {
return &os.PathError{Op: "move_mount", Path: target, Err: err}
}
return nil
} }
// CreateUsernsProcess forks the current process and creates a user namespace using the specified // CreateUsernsProcess forks the current process and creates a user namespace using the specified