volume_path_handler.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package volumepathhandler
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "k8s.io/klog"
  20. utilexec "k8s.io/utils/exec"
  21. "k8s.io/utils/mount"
  22. "k8s.io/apimachinery/pkg/types"
  23. )
  24. const (
  25. losetupPath = "losetup"
  26. statPath = "stat"
  27. ErrDeviceNotFound = "device not found"
  28. ErrDeviceNotSupported = "device not supported"
  29. )
  30. // BlockVolumePathHandler defines a set of operations for handling block volume-related operations
  31. type BlockVolumePathHandler interface {
  32. // MapDevice creates a symbolic link to block device under specified map path
  33. MapDevice(devicePath string, mapPath string, linkName string, bindMount bool) error
  34. // UnmapDevice removes a symbolic link to block device under specified map path
  35. UnmapDevice(mapPath string, linkName string, bindMount bool) error
  36. // RemovePath removes a file or directory on specified map path
  37. RemoveMapPath(mapPath string) error
  38. // IsSymlinkExist retruns true if specified symbolic link exists
  39. IsSymlinkExist(mapPath string) (bool, error)
  40. // IsDeviceBindMountExist retruns true if specified bind mount exists
  41. IsDeviceBindMountExist(mapPath string) (bool, error)
  42. // GetDeviceBindMountRefs searches bind mounts under global map path
  43. GetDeviceBindMountRefs(devPath string, mapPath string) ([]string, error)
  44. // FindGlobalMapPathUUIDFromPod finds {pod uuid} symbolic link under globalMapPath
  45. // corresponding to map path symlink, and then return global map path with pod uuid.
  46. FindGlobalMapPathUUIDFromPod(pluginDir, mapPath string, podUID types.UID) (string, error)
  47. // AttachFileDevice takes a path to a regular file and makes it available as an
  48. // attached block device.
  49. AttachFileDevice(path string) (string, error)
  50. // DetachFileDevice takes a path to the attached block device and
  51. // detach it from block device.
  52. DetachFileDevice(path string) error
  53. // GetLoopDevice returns the full path to the loop device associated with the given path.
  54. GetLoopDevice(path string) (string, error)
  55. }
  56. // NewBlockVolumePathHandler returns a new instance of BlockVolumeHandler.
  57. func NewBlockVolumePathHandler() BlockVolumePathHandler {
  58. var volumePathHandler VolumePathHandler
  59. return volumePathHandler
  60. }
  61. // VolumePathHandler is path related operation handlers for block volume
  62. type VolumePathHandler struct {
  63. }
  64. // MapDevice creates a symbolic link to block device under specified map path
  65. func (v VolumePathHandler) MapDevice(devicePath string, mapPath string, linkName string, bindMount bool) error {
  66. // Example of global map path:
  67. // globalMapPath/linkName: plugins/kubernetes.io/{PluginName}/{DefaultKubeletVolumeDevicesDirName}/{volumePluginDependentPath}/{podUid}
  68. // linkName: {podUid}
  69. //
  70. // Example of pod device map path:
  71. // podDeviceMapPath/linkName: pods/{podUid}/{DefaultKubeletVolumeDevicesDirName}/{escapeQualifiedPluginName}/{volumeName}
  72. // linkName: {volumeName}
  73. if len(devicePath) == 0 {
  74. return fmt.Errorf("failed to map device to map path. devicePath is empty")
  75. }
  76. if len(mapPath) == 0 {
  77. return fmt.Errorf("failed to map device to map path. mapPath is empty")
  78. }
  79. if !filepath.IsAbs(mapPath) {
  80. return fmt.Errorf("the map path should be absolute: map path: %s", mapPath)
  81. }
  82. klog.V(5).Infof("MapDevice: devicePath %s", devicePath)
  83. klog.V(5).Infof("MapDevice: mapPath %s", mapPath)
  84. klog.V(5).Infof("MapDevice: linkName %s", linkName)
  85. // Check and create mapPath
  86. _, err := os.Stat(mapPath)
  87. if err != nil && !os.IsNotExist(err) {
  88. return fmt.Errorf("cannot validate map path: %s: %v", mapPath, err)
  89. }
  90. if err = os.MkdirAll(mapPath, 0750); err != nil {
  91. return fmt.Errorf("failed to mkdir %s: %v", mapPath, err)
  92. }
  93. if bindMount {
  94. return mapBindMountDevice(v, devicePath, mapPath, linkName)
  95. }
  96. return mapSymlinkDevice(v, devicePath, mapPath, linkName)
  97. }
  98. func mapBindMountDevice(v VolumePathHandler, devicePath string, mapPath string, linkName string) error {
  99. // Check bind mount exists
  100. linkPath := filepath.Join(mapPath, string(linkName))
  101. file, err := os.Stat(linkPath)
  102. if err != nil {
  103. if !os.IsNotExist(err) {
  104. return fmt.Errorf("failed to stat file %s: %v", linkPath, err)
  105. }
  106. // Create file
  107. newFile, err := os.OpenFile(linkPath, os.O_CREATE|os.O_RDWR, 0750)
  108. if err != nil {
  109. return fmt.Errorf("failed to open file %s: %v", linkPath, err)
  110. }
  111. if err := newFile.Close(); err != nil {
  112. return fmt.Errorf("failed to close file %s: %v", linkPath, err)
  113. }
  114. } else {
  115. // Check if device file
  116. // TODO: Need to check if this device file is actually the expected bind mount
  117. if file.Mode()&os.ModeDevice == os.ModeDevice {
  118. klog.Warningf("Warning: Map skipped because bind mount already exist on the path: %v", linkPath)
  119. return nil
  120. }
  121. klog.Warningf("Warning: file %s is already exist but not mounted, skip creating file", linkPath)
  122. }
  123. // Bind mount file
  124. mounter := &mount.SafeFormatAndMount{Interface: mount.New(""), Exec: utilexec.New()}
  125. if err := mounter.Mount(devicePath, linkPath, "" /* fsType */, []string{"bind"}); err != nil {
  126. return fmt.Errorf("failed to bind mount devicePath: %s to linkPath %s: %v", devicePath, linkPath, err)
  127. }
  128. return nil
  129. }
  130. func mapSymlinkDevice(v VolumePathHandler, devicePath string, mapPath string, linkName string) error {
  131. // Remove old symbolic link(or file) then create new one.
  132. // This should be done because current symbolic link is
  133. // stale across node reboot.
  134. linkPath := filepath.Join(mapPath, string(linkName))
  135. if err := os.Remove(linkPath); err != nil && !os.IsNotExist(err) {
  136. return fmt.Errorf("failed to remove file %s: %v", linkPath, err)
  137. }
  138. return os.Symlink(devicePath, linkPath)
  139. }
  140. // UnmapDevice removes a symbolic link associated to block device under specified map path
  141. func (v VolumePathHandler) UnmapDevice(mapPath string, linkName string, bindMount bool) error {
  142. if len(mapPath) == 0 {
  143. return fmt.Errorf("failed to unmap device from map path. mapPath is empty")
  144. }
  145. klog.V(5).Infof("UnmapDevice: mapPath %s", mapPath)
  146. klog.V(5).Infof("UnmapDevice: linkName %s", linkName)
  147. if bindMount {
  148. return unmapBindMountDevice(v, mapPath, linkName)
  149. }
  150. return unmapSymlinkDevice(v, mapPath, linkName)
  151. }
  152. func unmapBindMountDevice(v VolumePathHandler, mapPath string, linkName string) error {
  153. // Check bind mount exists
  154. linkPath := filepath.Join(mapPath, string(linkName))
  155. if isMountExist, checkErr := v.IsDeviceBindMountExist(linkPath); checkErr != nil {
  156. return checkErr
  157. } else if !isMountExist {
  158. klog.Warningf("Warning: Unmap skipped because bind mount does not exist on the path: %v", linkPath)
  159. // Check if linkPath still exists
  160. if _, err := os.Stat(linkPath); err != nil {
  161. if !os.IsNotExist(err) {
  162. return fmt.Errorf("failed to check if path %s exists: %v", linkPath, err)
  163. }
  164. // linkPath has already been removed
  165. return nil
  166. }
  167. // Remove file
  168. if err := os.Remove(linkPath); err != nil && !os.IsNotExist(err) {
  169. return fmt.Errorf("failed to remove file %s: %v", linkPath, err)
  170. }
  171. return nil
  172. }
  173. // Unmount file
  174. mounter := &mount.SafeFormatAndMount{Interface: mount.New(""), Exec: utilexec.New()}
  175. if err := mounter.Unmount(linkPath); err != nil {
  176. return fmt.Errorf("failed to unmount linkPath %s: %v", linkPath, err)
  177. }
  178. // Remove file
  179. if err := os.Remove(linkPath); err != nil && !os.IsNotExist(err) {
  180. return fmt.Errorf("failed to remove file %s: %v", linkPath, err)
  181. }
  182. return nil
  183. }
  184. func unmapSymlinkDevice(v VolumePathHandler, mapPath string, linkName string) error {
  185. // Check symbolic link exists
  186. linkPath := filepath.Join(mapPath, string(linkName))
  187. if islinkExist, checkErr := v.IsSymlinkExist(linkPath); checkErr != nil {
  188. return checkErr
  189. } else if !islinkExist {
  190. klog.Warningf("Warning: Unmap skipped because symlink does not exist on the path: %v", linkPath)
  191. return nil
  192. }
  193. return os.Remove(linkPath)
  194. }
  195. // RemoveMapPath removes a file or directory on specified map path
  196. func (v VolumePathHandler) RemoveMapPath(mapPath string) error {
  197. if len(mapPath) == 0 {
  198. return fmt.Errorf("failed to remove map path. mapPath is empty")
  199. }
  200. klog.V(5).Infof("RemoveMapPath: mapPath %s", mapPath)
  201. err := os.RemoveAll(mapPath)
  202. if err != nil && !os.IsNotExist(err) {
  203. return fmt.Errorf("failed to remove directory %s: %v", mapPath, err)
  204. }
  205. return nil
  206. }
  207. // IsSymlinkExist returns true if specified file exists and the type is symbolik link.
  208. // If file doesn't exist, or file exists but not symbolic link, return false with no error.
  209. // On other cases, return false with error from Lstat().
  210. func (v VolumePathHandler) IsSymlinkExist(mapPath string) (bool, error) {
  211. fi, err := os.Lstat(mapPath)
  212. if err != nil {
  213. // If file doesn't exist, return false and no error
  214. if os.IsNotExist(err) {
  215. return false, nil
  216. }
  217. // Return error from Lstat()
  218. return false, fmt.Errorf("failed to Lstat file %s: %v", mapPath, err)
  219. }
  220. // If file exits and it's symbolic link, return true and no error
  221. if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
  222. return true, nil
  223. }
  224. // If file exits but it's not symbolic link, return false and no error
  225. return false, nil
  226. }
  227. // IsDeviceBindMountExist returns true if specified file exists and the type is device.
  228. // If file doesn't exist, or file exists but not device, return false with no error.
  229. // On other cases, return false with error from Lstat().
  230. func (v VolumePathHandler) IsDeviceBindMountExist(mapPath string) (bool, error) {
  231. fi, err := os.Lstat(mapPath)
  232. if err != nil {
  233. // If file doesn't exist, return false and no error
  234. if os.IsNotExist(err) {
  235. return false, nil
  236. }
  237. // Return error from Lstat()
  238. return false, fmt.Errorf("failed to Lstat file %s: %v", mapPath, err)
  239. }
  240. // If file exits and it's device, return true and no error
  241. if fi.Mode()&os.ModeDevice == os.ModeDevice {
  242. return true, nil
  243. }
  244. // If file exits but it's not device, return false and no error
  245. return false, nil
  246. }
  247. // GetDeviceBindMountRefs searches bind mounts under global map path
  248. func (v VolumePathHandler) GetDeviceBindMountRefs(devPath string, mapPath string) ([]string, error) {
  249. var refs []string
  250. files, err := ioutil.ReadDir(mapPath)
  251. if err != nil {
  252. return nil, fmt.Errorf("directory cannot read %v", err)
  253. }
  254. for _, file := range files {
  255. if file.Mode()&os.ModeDevice != os.ModeDevice {
  256. continue
  257. }
  258. filename := file.Name()
  259. // TODO: Might need to check if the file is actually linked to devPath
  260. refs = append(refs, filepath.Join(mapPath, filename))
  261. }
  262. klog.V(5).Infof("GetDeviceBindMountRefs: refs %v", refs)
  263. return refs, nil
  264. }