mount_linux.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package libcontainer
  2. import (
  3. "strconv"
  4. "golang.org/x/sys/unix"
  5. )
  6. // mountError holds an error from a failed mount or unmount operation.
  7. type mountError struct {
  8. op string
  9. source string
  10. target string
  11. procfd string
  12. flags uintptr
  13. data string
  14. err error
  15. }
  16. // Error provides a string error representation.
  17. func (e *mountError) Error() string {
  18. out := e.op + " "
  19. if e.source != "" {
  20. out += e.source + ":" + e.target
  21. } else {
  22. out += e.target
  23. }
  24. if e.procfd != "" {
  25. out += " (via " + e.procfd + ")"
  26. }
  27. if e.flags != uintptr(0) {
  28. out += ", flags: 0x" + strconv.FormatUint(uint64(e.flags), 16)
  29. }
  30. if e.data != "" {
  31. out += ", data: " + e.data
  32. }
  33. out += ": " + e.err.Error()
  34. return out
  35. }
  36. // Unwrap returns the underlying error.
  37. // This is a convention used by Go 1.13+ standard library.
  38. func (e *mountError) Unwrap() error {
  39. return e.err
  40. }
  41. // mount is a simple unix.Mount wrapper. If procfd is not empty, it is used
  42. // instead of target (and the target is only used to add context to an error).
  43. func mount(source, target, procfd, fstype string, flags uintptr, data string) error {
  44. dst := target
  45. if procfd != "" {
  46. dst = procfd
  47. }
  48. if err := unix.Mount(source, dst, fstype, flags, data); err != nil {
  49. return &mountError{
  50. op: "mount",
  51. source: source,
  52. target: target,
  53. procfd: procfd,
  54. flags: flags,
  55. data: data,
  56. err: err,
  57. }
  58. }
  59. return nil
  60. }
  61. // unmount is a simple unix.Unmount wrapper.
  62. func unmount(target string, flags int) error {
  63. err := unix.Unmount(target, flags)
  64. if err != nil {
  65. return &mountError{
  66. op: "unmount",
  67. target: target,
  68. flags: uintptr(flags),
  69. err: err,
  70. }
  71. }
  72. return nil
  73. }