Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StripVolumeRegex configuration property #311

Merged
merged 2 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions infrastructure/devicepathresolver/id_device_path_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package devicepathresolver
import (
"fmt"
"path"
"regexp"
"time"

boshudev "github.com/cloudfoundry/bosh-agent/platform/udevdevice"
Expand All @@ -12,20 +13,25 @@ import (
)

type idDevicePathResolver struct {
diskWaitTimeout time.Duration
udev boshudev.UdevDevice
fs boshsys.FileSystem
diskWaitTimeout time.Duration
udev boshudev.UdevDevice
fs boshsys.FileSystem
stripVolumeRegex string
stripVolumeCompiled *regexp.Regexp
}

func NewIDDevicePathResolver(
diskWaitTimeout time.Duration,
udev boshudev.UdevDevice,
fs boshsys.FileSystem,
stripVolumeRegex string,
) DevicePathResolver {
return idDevicePathResolver{
diskWaitTimeout: diskWaitTimeout,
udev: udev,
fs: fs,
diskWaitTimeout: diskWaitTimeout,
udev: udev,
fs: fs,
stripVolumeRegex: stripVolumeRegex,
stripVolumeCompiled: nil,
}
}

Expand All @@ -48,13 +54,20 @@ func (idpr idDevicePathResolver) GetRealDevicePath(diskSettings boshsettings.Dis
return "", false, bosherr.WrapError(err, "Running udevadm settle")
}

if idpr.stripVolumeRegex != "" && idpr.stripVolumeCompiled == nil {
idpr.stripVolumeCompiled, err = regexp.Compile(idpr.stripVolumeRegex)
if err != nil {
return "", false, bosherr.WrapError(err, "Compiling stripVolumeRegex")
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to have a function called like stripVolumeIfRequired which does everything and returns a striped ID or not if not required. The current implementation of stripDiskID function doesn't strip always the diskID which is not incorporated in the name. The function will need to return also error so that we fail in case the Compilation is not possible like it is implemented now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@beyhan Done. I've renamed the function made the requested changes.


stopAfter := time.Now().Add(idpr.diskWaitTimeout)
found := false

var realPath string

diskID := diskSettings.ID
deviceGlobPattern := fmt.Sprintf("*%s", diskID)
deviceGlobPattern := fmt.Sprintf("*%s", idpr.stripDiskID(diskID))
deviceIDPathGlobPattern := path.Join("/", "dev", "disk", "by-id", deviceGlobPattern)

for !found {
Expand Down Expand Up @@ -87,3 +100,10 @@ func (idpr idDevicePathResolver) GetRealDevicePath(diskSettings boshsettings.Dis

return realPath, false, nil
}

func (idpr idDevicePathResolver) stripDiskID(diskID string) string {
if idpr.stripVolumeCompiled != nil {
return idpr.stripVolumeCompiled.ReplaceAllLiteralString(diskID, "")
}
return diskID
}
42 changes: 37 additions & 5 deletions infrastructure/devicepathresolver/id_device_path_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (

var _ = Describe("IDDevicePathResolver", func() {
var (
fs *fakesys.FakeFileSystem
udev *fakeudev.FakeUdevDevice
diskSettings boshsettings.DiskSettings
pathResolver DevicePathResolver
fs *fakesys.FakeFileSystem
udev *fakeudev.FakeUdevDevice
diskSettings boshsettings.DiskSettings
pathResolver DevicePathResolver
stripVolumeRegex string
)

BeforeEach(func() {
Expand All @@ -33,7 +34,7 @@ var _ = Describe("IDDevicePathResolver", func() {
})

JustBeforeEach(func() {
pathResolver = NewIDDevicePathResolver(500*time.Millisecond, udev, fs)
pathResolver = NewIDDevicePathResolver(500*time.Millisecond, udev, fs, stripVolumeRegex)
})

Describe("GetRealDevicePath", func() {
Expand Down Expand Up @@ -204,5 +205,36 @@ var _ = Describe("IDDevicePathResolver", func() {
Expect(timeout).To(BeFalse())
})
})

Context("when stripVolumeRegex option is used to remove mismatched disk ID prefix", func() {
BeforeEach(func() {
diskSettings = boshsettings.DiskSettings{
ID: "vol-fake-disk-id-include-longname",
}
stripVolumeRegex = "^vol-"
err := fs.MkdirAll("/dev/fake-device-path", os.FileMode(0750))
Expect(err).ToNot(HaveOccurred())

err = fs.Symlink("/dev/fake-device-path", "/dev/intermediate/fake-device-path")
Expect(err).ToNot(HaveOccurred())

err = fs.Symlink("/dev/intermediate/fake-device-path", "/dev/disk/by-id/virtio-fake-disk-id-include-longname")
Expect(err).ToNot(HaveOccurred())

fs.SetGlob("/dev/disk/by-id/*fake-disk-id-include-longname", []string{"/dev/disk/by-id/virtio-fake-disk-id-include-longname"})
})

It("removes prefix and returns fully resolved path", func() {
path, timeout, err := pathResolver.GetRealDevicePath(diskSettings)
Expect(err).ToNot(HaveOccurred())

devicePath, err := filepath.Abs("/dev/fake-device-path")
Expect(err).ToNot(HaveOccurred())

Expect(path).To(Equal(devicePath))
Expect(timeout).To(BeFalse())
})
})

})
})
4 changes: 4 additions & 0 deletions platform/linux_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ type LinuxOptions struct {
// Strategy for resolving ephemeral & persistent disk partitioners;
// possible values: parted, "" (default is sfdisk if disk < 2TB, parted otherwise)
PartitionerType string

// Regular expression specifying what part of volume ID to strip.
// possible values: valid RE2 regex e.g. "^vol-", "" (default is not to strip)
StripVolumeRegex string
}

type linux struct {
Expand Down
2 changes: 1 addition & 1 deletion platform/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func NewProvider(logger boshlog.Logger, dirProvider boshdirs.Provider, statsColl
switch options.Linux.DevicePathResolutionType {
case "virtio":
udev := boshudev.NewConcreteUdevDevice(runner, logger)
idDevicePathResolver := devicepathresolver.NewIDDevicePathResolver(500*time.Millisecond, udev, fs)
idDevicePathResolver := devicepathresolver.NewIDDevicePathResolver(500*time.Millisecond, udev, fs, options.Linux.StripVolumeRegex)
mappedDevicePathResolver := devicepathresolver.NewMappedDevicePathResolver(30000*time.Millisecond, fs)
devicePathResolver = devicepathresolver.NewVirtioDevicePathResolver(idDevicePathResolver, mappedDevicePathResolver, logger)
case "scsi":
Expand Down