Skip to content

Commit b1e88d5

Browse files
author
Abhishek Agarwal
authored
feat(path-filter): Filter blockdevice using custom udev rules (#670)
Signed-off-by: Abhishek Agarwal <abhishek.agarwal@mayadata.io>
1 parent 7875a09 commit b1e88d5

File tree

7 files changed

+59
-6
lines changed

7 files changed

+59
-6
lines changed

cmd/ndm_daemonset/filter/pathfilter.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package filter
1818

1919
import (
2020
"github.com/openebs/node-disk-manager/blockdevice"
21+
libudevwrapper "github.com/openebs/node-disk-manager/pkg/udev"
22+
2123
"strings"
2224

2325
"github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller"
@@ -100,7 +102,19 @@ func (pf *pathFilter) Include(blockDevice *blockdevice.BlockDevice) bool {
100102
return true
101103
}
102104
}
103-
return util.MatchIgnoredCase(pf.includePaths, blockDevice.DevPath)
105+
if util.MatchIgnoredCase(pf.includePaths, blockDevice.DevPath) {
106+
return true
107+
}
108+
for _, link := range blockDevice.DevLinks {
109+
if link.Kind == libudevwrapper.SYMLINK {
110+
for _, symlink := range link.Links {
111+
if util.Contains(pf.includePaths, symlink) {
112+
return true
113+
}
114+
}
115+
}
116+
}
117+
return false
104118
}
105119

106120
// Exclude returns true if the disk path does not match any given
@@ -116,5 +130,17 @@ func (pf *pathFilter) Exclude(blockDevice *blockdevice.BlockDevice) bool {
116130
return false
117131
}
118132
}
119-
return !util.MatchIgnoredCase(pf.excludePaths, blockDevice.DevPath)
133+
if util.MatchIgnoredCase(pf.excludePaths, blockDevice.DevPath) {
134+
return false
135+
}
136+
for _, link := range blockDevice.DevLinks {
137+
if link.Kind == libudevwrapper.SYMLINK {
138+
for _, symlink := range link.Links {
139+
if util.Contains(pf.excludePaths, symlink) {
140+
return false
141+
}
142+
}
143+
}
144+
}
145+
return true
120146
}

cmd/ndm_daemonset/probe/udevprobe.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,13 @@ func (up *udevProbe) FillBlockDeviceDetails(blockDevice *blockdevice.BlockDevice
325325
})
326326
}
327327

328+
if len(udevDiskDetails.SymLinks) != 0 {
329+
blockDevice.DevLinks = append(blockDevice.DevLinks, blockdevice.DevLink{
330+
Kind: libudevwrapper.SYMLINK,
331+
Links: udevDiskDetails.SymLinks,
332+
})
333+
}
334+
328335
// filesystem info of the attached device. Only filesystem data will be filled in the struct,
329336
// as the mountpoint related information will be filled in by the mount probe
330337
blockDevice.FSInfo.FileSystem = udevDiskDetails.FileSystem

cmd/ndm_daemonset/probe/udevprobe_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ func TestFillDiskDetails(t *testing.T) {
125125
Links: mockOsDiskDetails.ByPathDevLinks,
126126
})
127127
}
128+
if len(mockOsDiskDetails.SymLinks) > 0 {
129+
expectedDiskInfo.DevLinks = append(expectedDiskInfo.DevLinks, blockdevice.DevLink{
130+
Kind: libudevwrapper.SYMLINK,
131+
Links: mockOsDiskDetails.SymLinks,
132+
})
133+
}
128134

129135
// The devlinks are compared separately as the ordering of devlinks can be different in some systems
130136
// eg: ubuntu 20.04 in github actions

pkg/blkid/blkid.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import (
3131
)
3232

3333
const (
34-
fsTypeIdentifier = "TYPE"
35-
labelIdentifier = "LABEL"
34+
fsTypeIdentifier = "TYPE"
35+
labelIdentifier = "LABEL"
3636
partitionTableUUIDIdentifier = "PTUUID"
3737
partitionEntryUUIDIdentifier = "PARTUUID"
3838
)

pkg/udev/common.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ const (
6060
UDEV_DEVLINKS = "DEVLINKS" // udev attribute contain devlinks of a disk
6161
BY_ID_LINK = "by-id" // by-path devlink contains this string
6262
BY_PATH_LINK = "by-path" // by-path devlink contains this string
63+
BY_UUID_LINK = "by-uuid" // by-uuid devlink contains the string
64+
BY_PARTUUID_LINK = "by-partuuid" // by-partuuid devlink contains the string
6365
LINK_ID_INDEX = 4 // this is used to get link index from dev link
6466
UDEV_FS_TYPE = "ID_FS_TYPE" // file system type the partition
6567
UDEV_FS_UUID = "ID_FS_UUID" // UUID of the filesystem present
@@ -71,6 +73,8 @@ const (
7173
UDEV_DM_UUID = "DM_UUID" // udev attribute to get the device mapper uuid
7274
// UDEV_DM_NAME is udev attribute to get the name of the dm device. This is used to generate the device mapper path
7375
UDEV_DM_NAME = "DM_NAME"
76+
// SYMLINK is used to represent any manually created device symlinks
77+
SYMLINK = "symlink"
7478
)
7579

7680
// UdevDiskDetails struct contain different attribute of disk.
@@ -82,6 +86,7 @@ type UdevDiskDetails struct {
8286
Path string // Path is Path of a disk.
8387
ByIdDevLinks []string // ByIdDevLinks contains by-id devlinks
8488
ByPathDevLinks []string // ByPathDevLinks contains by-path devlinks
89+
SymLinks []string // SymLinks contains device symlinks if any
8590
DiskType string // DeviceType can be disk, partition
8691
// IDType is used for uuid generation using the legacy algorithm
8792
IDType string
@@ -112,6 +117,7 @@ func (device *UdevDevice) DiskInfoFromLibudev() UdevDiskDetails {
112117
Path: device.GetPropertyValue(UDEV_DEVNAME),
113118
ByIdDevLinks: devLinks[BY_ID_LINK],
114119
ByPathDevLinks: devLinks[BY_PATH_LINK],
120+
SymLinks: devLinks[SYMLINK],
115121
DiskType: device.GetDevtype(),
116122
IDType: device.GetPropertyValue(UDEV_TYPE),
117123
FileSystem: device.GetFileSystemInfo(),
@@ -210,6 +216,7 @@ func (device *UdevDevice) GetDevLinks() map[string][]string {
210216
devLinkMap := make(map[string][]string)
211217
byIdLink := make([]string, 0)
212218
byPathLink := make([]string, 0)
219+
symLink := make([]string, 0)
213220
for _, link := range strings.Split(device.GetPropertyValue(UDEV_DEVLINKS), " ") {
214221
/*
215222
devlink is like - /dev/disk/by-id/scsi-0Google_PersistentDisk_demo-disk
@@ -228,12 +235,14 @@ func (device *UdevDevice) GetDevLinks() map[string][]string {
228235
} else {
229236
byIdLink = append(byIdLink, link)
230237
}
231-
}
232-
if util.Contains(parts, BY_PATH_LINK) {
238+
} else if util.Contains(parts, BY_PATH_LINK) {
233239
byPathLink = append(byPathLink, link)
234240
}
241+
// we can add very dev link in symlink map as there is a 1:1 mapping between the two links
242+
symLink = append(symLink, link)
235243
}
236244
devLinkMap[BY_ID_LINK] = byIdLink
237245
devLinkMap[BY_PATH_LINK] = byPathLink
246+
devLinkMap[SYMLINK] = symLink
238247
return devLinkMap
239248
}

pkg/udev/common_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func TestDiskInfoFromLibudev(t *testing.T) {
8585
Path: diskDetails.DevNode,
8686
ByIdDevLinks: diskDetails.ByIdDevLinks,
8787
ByPathDevLinks: diskDetails.ByPathDevLinks,
88+
SymLinks: diskDetails.SymLinks,
8889
PartitionTableType: diskDetails.PartTableType,
8990
IDType: diskDetails.IdType,
9091
}
@@ -101,8 +102,10 @@ func TestDiskInfoFromLibudev(t *testing.T) {
101102
// need to make this nil as devlinks already compared.
102103
test.expectedDetails.ByIdDevLinks = nil
103104
test.expectedDetails.ByPathDevLinks = nil
105+
test.expectedDetails.SymLinks = nil
104106
test.actualDetails.ByIdDevLinks = nil
105107
test.actualDetails.ByPathDevLinks = nil
108+
test.actualDetails.SymLinks = nil
106109
assert.Equal(t, test.expectedDetails, test.actualDetails)
107110
})
108111
}

pkg/udev/mockdata.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type MockOsDiskDetails struct {
5555
IdType string
5656
ByIdDevLinks []string
5757
ByPathDevLinks []string
58+
SymLinks []string
5859
Dependents bd.DependentBlockDevices
5960
}
6061

@@ -108,6 +109,7 @@ func MockDiskDetails() (MockOsDiskDetails, error) {
108109
devLinks := device.GetDevLinks()
109110
diskDetails.ByIdDevLinks = devLinks[BY_ID_LINK]
110111
diskDetails.ByPathDevLinks = devLinks[BY_PATH_LINK]
112+
diskDetails.SymLinks = devLinks[SYMLINK]
111113
return diskDetails, nil
112114
}
113115

0 commit comments

Comments
 (0)