exec_mount.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // +build linux
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package exec
  15. import (
  16. "fmt"
  17. "os"
  18. "k8s.io/klog"
  19. "k8s.io/kubernetes/pkg/util/mount"
  20. )
  21. // ExecMounter is a mounter that uses provided Exec interface to mount and
  22. // unmount a filesystem. For all other calls it uses a wrapped mounter.
  23. type execMounter struct {
  24. wrappedMounter mount.Interface
  25. exec mount.Exec
  26. }
  27. // NewExecMounter returns a mounter that uses provided Exec interface to mount and
  28. // unmount a filesystem. For all other calls it uses a wrapped mounter.
  29. func NewExecMounter(exec mount.Exec, wrapped mount.Interface) mount.Interface {
  30. return &execMounter{
  31. wrappedMounter: wrapped,
  32. exec: exec,
  33. }
  34. }
  35. // execMounter implements mount.Interface
  36. var _ mount.Interface = &execMounter{}
  37. // Mount runs mount(8) using given exec interface.
  38. func (m *execMounter) Mount(source string, target string, fstype string, options []string) error {
  39. bind, bindOpts, bindRemountOpts := mount.IsBind(options)
  40. if bind {
  41. err := m.doExecMount(source, target, fstype, bindOpts)
  42. if err != nil {
  43. return err
  44. }
  45. return m.doExecMount(source, target, fstype, bindRemountOpts)
  46. }
  47. return m.doExecMount(source, target, fstype, options)
  48. }
  49. // doExecMount calls exec(mount <what> <where>) using given exec interface.
  50. func (m *execMounter) doExecMount(source, target, fstype string, options []string) error {
  51. klog.V(5).Infof("Exec Mounting %s %s %s %v", source, target, fstype, options)
  52. mountArgs := mount.MakeMountArgs(source, target, fstype, options)
  53. output, err := m.exec.Run("mount", mountArgs...)
  54. klog.V(5).Infof("Exec mounted %v: %v: %s", mountArgs, err, string(output))
  55. if err != nil {
  56. return fmt.Errorf("mount failed: %v\nMounting command: %s\nMounting arguments: %s %s %s %v\nOutput: %s",
  57. err, "mount", source, target, fstype, options, string(output))
  58. }
  59. return err
  60. }
  61. // Unmount runs umount(8) using given exec interface.
  62. func (m *execMounter) Unmount(target string) error {
  63. outputBytes, err := m.exec.Run("umount", target)
  64. if err == nil {
  65. klog.V(5).Infof("Exec unmounted %s: %s", target, string(outputBytes))
  66. } else {
  67. klog.V(5).Infof("Failed to exec unmount %s: err: %q, umount output: %s", target, err, string(outputBytes))
  68. }
  69. return err
  70. }
  71. // List returns a list of all mounted filesystems.
  72. func (m *execMounter) List() ([]mount.MountPoint, error) {
  73. return m.wrappedMounter.List()
  74. }
  75. // IsLikelyNotMountPoint determines whether a path is a mountpoint.
  76. func (m *execMounter) IsLikelyNotMountPoint(file string) (bool, error) {
  77. return m.wrappedMounter.IsLikelyNotMountPoint(file)
  78. }
  79. // DeviceOpened checks if block device in use by calling Open with O_EXCL flag.
  80. // Returns true if open returns errno EBUSY, and false if errno is nil.
  81. // Returns an error if errno is any error other than EBUSY.
  82. // Returns with error if pathname is not a device.
  83. func (m *execMounter) DeviceOpened(pathname string) (bool, error) {
  84. return m.wrappedMounter.DeviceOpened(pathname)
  85. }
  86. // PathIsDevice uses FileInfo returned from os.Stat to check if path refers
  87. // to a device.
  88. func (m *execMounter) PathIsDevice(pathname string) (bool, error) {
  89. return m.wrappedMounter.PathIsDevice(pathname)
  90. }
  91. //GetDeviceNameFromMount given a mount point, find the volume id from checking /proc/mounts
  92. func (m *execMounter) GetDeviceNameFromMount(mountPath, pluginMountDir string) (string, error) {
  93. return m.wrappedMounter.GetDeviceNameFromMount(mountPath, pluginMountDir)
  94. }
  95. func (m *execMounter) IsMountPointMatch(mp mount.MountPoint, dir string) bool {
  96. return m.wrappedMounter.IsMountPointMatch(mp, dir)
  97. }
  98. func (m *execMounter) MakeRShared(path string) error {
  99. return m.wrappedMounter.MakeRShared(path)
  100. }
  101. func (m *execMounter) GetFileType(pathname string) (mount.FileType, error) {
  102. return m.wrappedMounter.GetFileType(pathname)
  103. }
  104. func (m *execMounter) MakeFile(pathname string) error {
  105. return m.wrappedMounter.MakeFile(pathname)
  106. }
  107. func (m *execMounter) MakeDir(pathname string) error {
  108. return m.wrappedMounter.MakeDir(pathname)
  109. }
  110. func (m *execMounter) ExistsPath(pathname string) (bool, error) {
  111. return m.wrappedMounter.ExistsPath(pathname)
  112. }
  113. func (m *execMounter) EvalHostSymlinks(pathname string) (string, error) {
  114. return m.wrappedMounter.EvalHostSymlinks(pathname)
  115. }
  116. func (m *execMounter) GetMountRefs(pathname string) ([]string, error) {
  117. return m.wrappedMounter.GetMountRefs(pathname)
  118. }
  119. func (m *execMounter) GetFSGroup(pathname string) (int64, error) {
  120. return m.wrappedMounter.GetFSGroup(pathname)
  121. }
  122. func (m *execMounter) GetSELinuxSupport(pathname string) (bool, error) {
  123. return m.wrappedMounter.GetSELinuxSupport(pathname)
  124. }
  125. func (m *execMounter) GetMode(pathname string) (os.FileMode, error) {
  126. return m.wrappedMounter.GetMode(pathname)
  127. }