github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/cmd/ndm_daemonset/filter/osdiskexcludefilter.go (about) 1 /* 2 Copyright 2018 The OpenEBS 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 filter 18 19 import ( 20 "strings" 21 22 "github.com/openebs/node-disk-manager/blockdevice" 23 "github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller" 24 "github.com/openebs/node-disk-manager/pkg/mount" 25 "github.com/openebs/node-disk-manager/pkg/util" 26 27 "k8s.io/klog/v2" 28 ) 29 30 const ( 31 osDiskExcludeFilterKey = "os-disk-exclude-filter" 32 ) 33 34 var ( 35 defaultMountFilePath = "/proc/self/mounts" 36 mountPoints = []string{"/", "/etc/hosts"} 37 hostMountFilePath = "/host/proc/1/mounts" // hostMountFilePath is the file path mounted inside container 38 oSDiskExcludeFilterName = "os disk exclude filter" // filter name 39 oSDiskExcludeFilterState = defaultEnabled // filter state 40 ) 41 42 // oSDiskExcludeFilterRegister contains registration process of oSDiskExcludeFilter 43 var oSDiskExcludeFilterRegister = func() { 44 ctrl := <-controller.ControllerBroadcastChannel 45 if ctrl == nil { 46 return 47 } 48 if ctrl.NDMConfig != nil { 49 for _, filterConfig := range ctrl.NDMConfig.FilterConfigs { 50 if filterConfig.Key == osDiskExcludeFilterKey { 51 oSDiskExcludeFilterName = filterConfig.Name 52 oSDiskExcludeFilterState = util.CheckTruthy(filterConfig.State) 53 mountPoints = strings.Split(filterConfig.Exclude, ",") 54 break 55 } 56 } 57 } 58 var fi controller.FilterInterface = newNonOsDiskFilter(ctrl) 59 newRegisterFilter := ®isterFilter{ 60 name: oSDiskExcludeFilterName, 61 state: oSDiskExcludeFilterState, 62 fi: fi, 63 controller: ctrl, 64 } 65 newRegisterFilter.register() 66 } 67 68 // oSDiskExcludeFilter controller and path of os disk 69 type oSDiskExcludeFilter struct { 70 controller *controller.Controller 71 excludeDevPaths []string 72 } 73 74 // newOsDiskFilter returns new pointer osDiskFilter 75 func newNonOsDiskFilter(ctrl *controller.Controller) *oSDiskExcludeFilter { 76 return &oSDiskExcludeFilter{ 77 controller: ctrl, 78 } 79 } 80 81 // Start set os disk devPath in nonOsDiskFilter pointer 82 func (odf *oSDiskExcludeFilter) Start() { 83 for _, mountPoint := range mountPoints { 84 var err error 85 var devPath string 86 87 // Check for mountpoints in both: 88 // the host's /proc/1/mounts file 89 // the /proc/self/mounts file 90 // If it is found in either one and we are able to get the 91 // disk's devpath, add it to the Controller struct. Otherwise 92 // log an error. 93 94 mountPointUtil := mount.NewMountUtil(hostMountFilePath, "", mountPoint) 95 if devPath, err = mountPointUtil.GetDiskPath(); err == nil { 96 odf.excludeDevPaths = append(odf.excludeDevPaths, devPath) 97 continue 98 } 99 100 mountPointUtil = mount.NewMountUtil(defaultMountFilePath, "", mountPoint) 101 if devPath, err = mountPointUtil.GetDiskPath(); err == nil { 102 odf.excludeDevPaths = append(odf.excludeDevPaths, devPath) 103 continue 104 } 105 106 klog.Errorf("unable to configure os disk filter for mountpoint: %s, error: %v", mountPoint, err) 107 } 108 } 109 110 // Include contains nothing by default it returns false 111 func (odf *oSDiskExcludeFilter) Include(blockDevice *blockdevice.BlockDevice) bool { 112 return true 113 } 114 115 // Exclude returns true if disk devpath does not match with excludeDevPaths 116 func (odf *oSDiskExcludeFilter) Exclude(blockDevice *blockdevice.BlockDevice) bool { 117 // The partitionRegex is chosen depending on whether the device uses 118 // the p[0-9] partition naming structure or not. 119 var partitionRegex string 120 for _, excludeDevPath := range odf.excludeDevPaths { 121 if util.IsMatchRegex(".+[0-9]+$", excludeDevPath) { 122 // matches loop0, loop0p1, nvme3n0p1 123 partitionRegex = "(p[0-9]+)?$" 124 } else { 125 // matches sda, sda1 126 partitionRegex = "[0-9]*$" 127 } 128 regex := "^" + excludeDevPath + partitionRegex 129 klog.Infof("applying os-filter regex %s on %s", regex, blockDevice.DevPath) 130 if util.IsMatchRegex(regex, blockDevice.DevPath) { 131 return false 132 } 133 } 134 return true 135 }