disk_manager.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 iscsi
  14. import (
  15. "os"
  16. "k8s.io/klog"
  17. "k8s.io/utils/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 iscsiDisk) string
  24. MakeGlobalVDPDName(disk iscsiDisk) string
  25. // Attaches the disk to the kubelet's host machine.
  26. AttachDisk(b iscsiDiskMounter) (string, error)
  27. // Detaches the disk from the kubelet's host machine.
  28. DetachDisk(disk iscsiDiskUnmounter, mntPath string) error
  29. // Detaches the block disk from the kubelet's host machine.
  30. DetachBlockISCSIDisk(disk iscsiDiskUnmapper, mntPath string) error
  31. }
  32. // utility to mount a disk based filesystem
  33. // globalPDPath: global mount path like, /var/lib/kubelet/plugins/kubernetes.io/iscsi/{ifaceName}/{portal-some_iqn-lun-lun_id}
  34. // volPath: pod volume dir path like, /var/lib/kubelet/pods/{podUID}/volumes/kubernetes.io~iscsi/{volumeName}
  35. func diskSetUp(manager diskManager, b iscsiDiskMounter, volPath string, mounter mount.Interface, fsGroup *int64) error {
  36. notMnt, err := mounter.IsLikelyNotMountPoint(volPath)
  37. if err != nil && !os.IsNotExist(err) {
  38. klog.Errorf("cannot validate mountpoint: %s", volPath)
  39. return err
  40. }
  41. if !notMnt {
  42. return nil
  43. }
  44. if err := os.MkdirAll(volPath, 0750); err != nil {
  45. klog.Errorf("failed to mkdir:%s", volPath)
  46. return err
  47. }
  48. // Perform a bind mount to the full path to allow duplicate mounts of the same disk.
  49. options := []string{"bind"}
  50. if b.readOnly {
  51. options = append(options, "ro")
  52. }
  53. if b.iscsiDisk.InitiatorName != "" {
  54. // new iface name is <target portal>:<volume name>
  55. b.iscsiDisk.Iface = b.iscsiDisk.Portals[0] + ":" + b.iscsiDisk.VolName
  56. }
  57. globalPDPath := manager.MakeGlobalPDName(*b.iscsiDisk)
  58. mountOptions := util.JoinMountOptions(b.mountOptions, options)
  59. err = mounter.Mount(globalPDPath, volPath, "", mountOptions)
  60. if err != nil {
  61. klog.Errorf("Failed to bind mount: source:%s, target:%s, err:%v", globalPDPath, volPath, err)
  62. noMnt, mntErr := b.mounter.IsLikelyNotMountPoint(volPath)
  63. if mntErr != nil {
  64. klog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
  65. return err
  66. }
  67. if !noMnt {
  68. if mntErr = b.mounter.Unmount(volPath); mntErr != nil {
  69. klog.Errorf("Failed to unmount: %v", mntErr)
  70. return err
  71. }
  72. noMnt, mntErr = b.mounter.IsLikelyNotMountPoint(volPath)
  73. if mntErr != nil {
  74. klog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
  75. return err
  76. }
  77. if !noMnt {
  78. // will most likely retry on next sync loop.
  79. klog.Errorf("%s is still mounted, despite call to unmount(). Will try again next sync loop.", volPath)
  80. return err
  81. }
  82. }
  83. os.Remove(volPath)
  84. return err
  85. }
  86. if !b.readOnly {
  87. volume.SetVolumeOwnership(&b, fsGroup)
  88. }
  89. return nil
  90. }