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 all commits
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
44 changes: 35 additions & 9 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,24 +13,29 @@ 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,
return &idDevicePathResolver{
diskWaitTimeout: diskWaitTimeout,
udev: udev,
fs: fs,
stripVolumeRegex: stripVolumeRegex,
stripVolumeCompiled: nil,
}
}

func (idpr idDevicePathResolver) GetRealDevicePath(diskSettings boshsettings.DiskSettings) (string, bool, error) {
func (idpr *idDevicePathResolver) GetRealDevicePath(diskSettings boshsettings.DiskSettings) (string, bool, error) {
if diskSettings.ID == "" {
return "", false, bosherr.Errorf("Disk ID is not set")
}
Expand All @@ -54,7 +60,12 @@ func (idpr idDevicePathResolver) GetRealDevicePath(diskSettings boshsettings.Dis
var realPath string

diskID := diskSettings.ID
deviceGlobPattern := fmt.Sprintf("*%s", diskID)
strippedDiskID, err := idpr.stripVolumeIfRequired(diskID)
if err != nil {
return "", false, err
}

deviceGlobPattern := fmt.Sprintf("*%s", strippedDiskID)
deviceIDPathGlobPattern := path.Join("/", "dev", "disk", "by-id", deviceGlobPattern)

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

return realPath, false, nil
}

func (idpr *idDevicePathResolver) stripVolumeIfRequired(diskID string) (string, error) {
if idpr.stripVolumeRegex == "" {
return diskID, nil
}

if idpr.stripVolumeCompiled == nil {
var err error
idpr.stripVolumeCompiled, err = regexp.Compile(idpr.stripVolumeRegex)
if err != nil {
return "", bosherr.WrapError(err, "Compiling stripVolumeRegex")
}
}
return idpr.stripVolumeCompiled.ReplaceAllLiteralString(diskID, ""), nil
}
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