k8s.io/kubernetes@v1.29.3/pkg/volume/fc/disk_manager.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors. 3 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 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package fc 18 19 import ( 20 "os" 21 22 v1 "k8s.io/api/core/v1" 23 "k8s.io/klog/v2" 24 "k8s.io/mount-utils" 25 26 "k8s.io/kubernetes/pkg/volume" 27 "k8s.io/kubernetes/pkg/volume/util" 28 ) 29 30 // Abstract interface to disk operations. 31 type diskManager interface { 32 MakeGlobalPDName(disk fcDisk) string 33 MakeGlobalVDPDName(disk fcDisk) string 34 // Attaches the disk to the kubelet's host machine. 35 AttachDisk(b fcDiskMounter) (string, error) 36 // Detaches the disk from the kubelet's host machine. 37 DetachDisk(disk fcDiskUnmounter, devicePath string) error 38 // Detaches the block disk from the kubelet's host machine. 39 DetachBlockFCDisk(disk fcDiskUnmapper, mntPath, devicePath string) error 40 } 41 42 // utility to mount a disk based filesystem 43 func diskSetUp(manager diskManager, b fcDiskMounter, volPath string, mounter mount.Interface, fsGroup *int64, fsGroupChangePolicy *v1.PodFSGroupChangePolicy) error { 44 globalPDPath := manager.MakeGlobalPDName(*b.fcDisk) 45 noMnt, err := mounter.IsLikelyNotMountPoint(volPath) 46 47 if err != nil && !os.IsNotExist(err) { 48 klog.Errorf("cannot validate mountpoint: %s", volPath) 49 return err 50 } 51 if !noMnt { 52 return nil 53 } 54 if err := os.MkdirAll(volPath, 0750); err != nil { 55 klog.Errorf("failed to mkdir:%s", volPath) 56 return err 57 } 58 // Perform a bind mount to the full path to allow duplicate mounts of the same disk. 59 options := []string{"bind"} 60 if b.readOnly { 61 options = append(options, "ro") 62 } 63 mountOptions := util.JoinMountOptions(options, b.mountOptions) 64 err = mounter.MountSensitiveWithoutSystemd(globalPDPath, volPath, "", mountOptions, nil) 65 if err != nil { 66 klog.Errorf("Failed to bind mount: source:%s, target:%s, err:%v", globalPDPath, volPath, err) 67 noMnt, mntErr := b.mounter.IsLikelyNotMountPoint(volPath) 68 if mntErr != nil { 69 klog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr) 70 return err 71 } 72 if !noMnt { 73 if mntErr = b.mounter.Unmount(volPath); mntErr != nil { 74 klog.Errorf("Failed to unmount: %v", mntErr) 75 return err 76 } 77 noMnt, mntErr = b.mounter.IsLikelyNotMountPoint(volPath) 78 if mntErr != nil { 79 klog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr) 80 return err 81 } 82 if !noMnt { 83 // will most likely retry on next sync loop. 84 klog.Errorf("%s is still mounted, despite call to unmount(). Will try again next sync loop.", volPath) 85 return err 86 } 87 } 88 os.Remove(volPath) 89 90 return err 91 } 92 93 if !b.readOnly { 94 volume.SetVolumeOwnership(&b, volPath, fsGroup, fsGroupChangePolicy, util.FSGroupCompleteHook(b.plugin, nil)) 95 } 96 97 return nil 98 }