Skip to content

Commit

Permalink
feat(path-filter): Filter blockdevice using custom udev rules (#670)
Browse files Browse the repository at this point in the history
Signed-off-by: Abhishek Agarwal <abhishek.agarwal@mayadata.io>
  • Loading branch information
Abhishek Agarwal authored Jun 30, 2022
1 parent 7875a09 commit b1e88d5
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 6 deletions.
30 changes: 28 additions & 2 deletions cmd/ndm_daemonset/filter/pathfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package filter

import (
"github.com/openebs/node-disk-manager/blockdevice"
libudevwrapper "github.com/openebs/node-disk-manager/pkg/udev"

"strings"

"github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller"
Expand Down Expand Up @@ -100,7 +102,19 @@ func (pf *pathFilter) Include(blockDevice *blockdevice.BlockDevice) bool {
return true
}
}
return util.MatchIgnoredCase(pf.includePaths, blockDevice.DevPath)
if util.MatchIgnoredCase(pf.includePaths, blockDevice.DevPath) {
return true
}
for _, link := range blockDevice.DevLinks {
if link.Kind == libudevwrapper.SYMLINK {
for _, symlink := range link.Links {
if util.Contains(pf.includePaths, symlink) {
return true
}
}
}
}
return false
}

// Exclude returns true if the disk path does not match any given
Expand All @@ -116,5 +130,17 @@ func (pf *pathFilter) Exclude(blockDevice *blockdevice.BlockDevice) bool {
return false
}
}
return !util.MatchIgnoredCase(pf.excludePaths, blockDevice.DevPath)
if util.MatchIgnoredCase(pf.excludePaths, blockDevice.DevPath) {
return false
}
for _, link := range blockDevice.DevLinks {
if link.Kind == libudevwrapper.SYMLINK {
for _, symlink := range link.Links {
if util.Contains(pf.excludePaths, symlink) {
return false
}
}
}
}
return true
}
7 changes: 7 additions & 0 deletions cmd/ndm_daemonset/probe/udevprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,13 @@ func (up *udevProbe) FillBlockDeviceDetails(blockDevice *blockdevice.BlockDevice
})
}

if len(udevDiskDetails.SymLinks) != 0 {
blockDevice.DevLinks = append(blockDevice.DevLinks, blockdevice.DevLink{
Kind: libudevwrapper.SYMLINK,
Links: udevDiskDetails.SymLinks,
})
}

// filesystem info of the attached device. Only filesystem data will be filled in the struct,
// as the mountpoint related information will be filled in by the mount probe
blockDevice.FSInfo.FileSystem = udevDiskDetails.FileSystem
Expand Down
6 changes: 6 additions & 0 deletions cmd/ndm_daemonset/probe/udevprobe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ func TestFillDiskDetails(t *testing.T) {
Links: mockOsDiskDetails.ByPathDevLinks,
})
}
if len(mockOsDiskDetails.SymLinks) > 0 {
expectedDiskInfo.DevLinks = append(expectedDiskInfo.DevLinks, blockdevice.DevLink{
Kind: libudevwrapper.SYMLINK,
Links: mockOsDiskDetails.SymLinks,
})
}

// The devlinks are compared separately as the ordering of devlinks can be different in some systems
// eg: ubuntu 20.04 in github actions
Expand Down
4 changes: 2 additions & 2 deletions pkg/blkid/blkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
)

const (
fsTypeIdentifier = "TYPE"
labelIdentifier = "LABEL"
fsTypeIdentifier = "TYPE"
labelIdentifier = "LABEL"
partitionTableUUIDIdentifier = "PTUUID"
partitionEntryUUIDIdentifier = "PARTUUID"
)
Expand Down
13 changes: 11 additions & 2 deletions pkg/udev/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const (
UDEV_DEVLINKS = "DEVLINKS" // udev attribute contain devlinks of a disk
BY_ID_LINK = "by-id" // by-path devlink contains this string
BY_PATH_LINK = "by-path" // by-path devlink contains this string
BY_UUID_LINK = "by-uuid" // by-uuid devlink contains the string
BY_PARTUUID_LINK = "by-partuuid" // by-partuuid devlink contains the string
LINK_ID_INDEX = 4 // this is used to get link index from dev link
UDEV_FS_TYPE = "ID_FS_TYPE" // file system type the partition
UDEV_FS_UUID = "ID_FS_UUID" // UUID of the filesystem present
Expand All @@ -71,6 +73,8 @@ const (
UDEV_DM_UUID = "DM_UUID" // udev attribute to get the device mapper uuid
// UDEV_DM_NAME is udev attribute to get the name of the dm device. This is used to generate the device mapper path
UDEV_DM_NAME = "DM_NAME"
// SYMLINK is used to represent any manually created device symlinks
SYMLINK = "symlink"
)

// UdevDiskDetails struct contain different attribute of disk.
Expand All @@ -82,6 +86,7 @@ type UdevDiskDetails struct {
Path string // Path is Path of a disk.
ByIdDevLinks []string // ByIdDevLinks contains by-id devlinks
ByPathDevLinks []string // ByPathDevLinks contains by-path devlinks
SymLinks []string // SymLinks contains device symlinks if any
DiskType string // DeviceType can be disk, partition
// IDType is used for uuid generation using the legacy algorithm
IDType string
Expand Down Expand Up @@ -112,6 +117,7 @@ func (device *UdevDevice) DiskInfoFromLibudev() UdevDiskDetails {
Path: device.GetPropertyValue(UDEV_DEVNAME),
ByIdDevLinks: devLinks[BY_ID_LINK],
ByPathDevLinks: devLinks[BY_PATH_LINK],
SymLinks: devLinks[SYMLINK],
DiskType: device.GetDevtype(),
IDType: device.GetPropertyValue(UDEV_TYPE),
FileSystem: device.GetFileSystemInfo(),
Expand Down Expand Up @@ -210,6 +216,7 @@ func (device *UdevDevice) GetDevLinks() map[string][]string {
devLinkMap := make(map[string][]string)
byIdLink := make([]string, 0)
byPathLink := make([]string, 0)
symLink := make([]string, 0)
for _, link := range strings.Split(device.GetPropertyValue(UDEV_DEVLINKS), " ") {
/*
devlink is like - /dev/disk/by-id/scsi-0Google_PersistentDisk_demo-disk
Expand All @@ -228,12 +235,14 @@ func (device *UdevDevice) GetDevLinks() map[string][]string {
} else {
byIdLink = append(byIdLink, link)
}
}
if util.Contains(parts, BY_PATH_LINK) {
} else if util.Contains(parts, BY_PATH_LINK) {
byPathLink = append(byPathLink, link)
}
// we can add very dev link in symlink map as there is a 1:1 mapping between the two links
symLink = append(symLink, link)
}
devLinkMap[BY_ID_LINK] = byIdLink
devLinkMap[BY_PATH_LINK] = byPathLink
devLinkMap[SYMLINK] = symLink
return devLinkMap
}
3 changes: 3 additions & 0 deletions pkg/udev/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func TestDiskInfoFromLibudev(t *testing.T) {
Path: diskDetails.DevNode,
ByIdDevLinks: diskDetails.ByIdDevLinks,
ByPathDevLinks: diskDetails.ByPathDevLinks,
SymLinks: diskDetails.SymLinks,
PartitionTableType: diskDetails.PartTableType,
IDType: diskDetails.IdType,
}
Expand All @@ -101,8 +102,10 @@ func TestDiskInfoFromLibudev(t *testing.T) {
// need to make this nil as devlinks already compared.
test.expectedDetails.ByIdDevLinks = nil
test.expectedDetails.ByPathDevLinks = nil
test.expectedDetails.SymLinks = nil
test.actualDetails.ByIdDevLinks = nil
test.actualDetails.ByPathDevLinks = nil
test.actualDetails.SymLinks = nil
assert.Equal(t, test.expectedDetails, test.actualDetails)
})
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/udev/mockdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type MockOsDiskDetails struct {
IdType string
ByIdDevLinks []string
ByPathDevLinks []string
SymLinks []string
Dependents bd.DependentBlockDevices
}

Expand Down Expand Up @@ -108,6 +109,7 @@ func MockDiskDetails() (MockOsDiskDetails, error) {
devLinks := device.GetDevLinks()
diskDetails.ByIdDevLinks = devLinks[BY_ID_LINK]
diskDetails.ByPathDevLinks = devLinks[BY_PATH_LINK]
diskDetails.SymLinks = devLinks[SYMLINK]
return diskDetails, nil
}

Expand Down

0 comments on commit b1e88d5

Please sign in to comment.