disk_manager.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2015 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 fc
  14. import (
  15. "os"
  16. "k8s.io/klog"
  17. "k8s.io/kubernetes/pkg/util/mount"
  18. "k8s.io/kubernetes/pkg/volume"
  19. "k8s.io/kubernetes/pkg/volume/util"
  20. )
  21. // Abstract interface to disk operations.
  22. type diskManager interface {
  23. MakeGlobalPDName(disk fcDisk) string
  24. MakeGlobalVDPDName(disk fcDisk) string
  25. // Attaches the disk to the kubelet's host machine.
  26. AttachDisk(b fcDiskMounter) (string, error)
  27. // Detaches the disk from the kubelet's host machine.
  28. DetachDisk(disk fcDiskUnmounter, devicePath string) error
  29. // Detaches the block disk from the kubelet's host machine.
  30. DetachBlockFCDisk(disk fcDiskUnmapper, mntPath, devicePath string) error
  31. }
  32. // utility to mount a disk based filesystem
  33. func diskSetUp(manager diskManager, b fcDiskMounter, volPath string, mounter mount.Interface, fsGroup *int64) error {
  34. globalPDPath := manager.MakeGlobalPDName(*b.fcDisk)
  35. noMnt, err := mounter.IsLikelyNotMountPoint(volPath)
  36. if err != nil && !os.IsNotExist(err) {
  37. klog.Errorf("cannot validate mountpoint: %s", volPath)
  38. return err
  39. }
  40. if !noMnt {
  41. return nil
  42. }
  43. if err := os.MkdirAll(volPath, 0750); err != nil {
  44. klog.Errorf("failed to mkdir:%s", volPath)
  45. return err
  46. }
  47. // Perform a bind mount to the full path to allow duplicate mounts of the same disk.
  48. options := []string{"bind"}
  49. if b.readOnly {
  50. options = append(options, "ro")
  51. }
  52. mountOptions := util.JoinMountOptions(options, b.mountOptions)
  53. err = mounter.Mount(globalPDPath, volPath, "", mountOptions)
  54. if err != nil {
  55. klog.Errorf("Failed to bind mount: source:%s, target:%s, err:%v", globalPDPath, volPath, err)
  56. noMnt, mntErr := b.mounter.IsLikelyNotMountPoint(volPath)
  57. if mntErr != nil {
  58. klog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
  59. return err
  60. }
  61. if !noMnt {
  62. if mntErr = b.mounter.Unmount(volPath); mntErr != nil {
  63. klog.Errorf("Failed to unmount: %v", mntErr)
  64. return err
  65. }
  66. noMnt, mntErr = b.mounter.IsLikelyNotMountPoint(volPath)
  67. if mntErr != nil {
  68. klog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
  69. return err
  70. }
  71. if !noMnt {
  72. // will most likely retry on next sync loop.
  73. klog.Errorf("%s is still mounted, despite call to unmount(). Will try again next sync loop.", volPath)
  74. return err
  75. }
  76. }
  77. os.Remove(volPath)
  78. return err
  79. }
  80. if !b.readOnly {
  81. volume.SetVolumeOwnership(&b, fsGroup)
  82. }
  83. return nil
  84. }