Skip to content

Commit

Permalink
Add handling for disk IDs with prefix
Browse files Browse the repository at this point in the history
Some IaaS provider have disk ID with prefix which is separated
by a hyphen character. However the symlink doesn't have the prefix
and the hyphen character that is why the resolution doesn't work in this
case. This pr removes the prefix with the hyphen from the disk ID before
the resolution to provide a solution for this case
  • Loading branch information
beyhan authored and fmoehler committed May 11, 2023
1 parent 1cbfa6a commit 6636681
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
13 changes: 11 additions & 2 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"
"strings"
"time"

boshudev "github.com/cloudfoundry/bosh-agent/platform/udevdevice"
Expand Down Expand Up @@ -51,9 +52,17 @@ func (idpr idDevicePathResolver) GetRealDevicePath(diskSettings boshsettings.Dis
stopAfter := time.Now().Add(idpr.diskWaitTimeout)
found := false

var realPath string
var realPath, diskID string

dashIndex := strings.IndexRune(diskSettings.ID, '-')
if dashIndex != -1 {
//some disks are mounted without the dash or without the prefix,
//we remove it here entirely so the Glob by-id works for all scenarios
diskID = diskSettings.ID[dashIndex+1:]
} else {
diskID = diskSettings.ID
}

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

Expand Down
45 changes: 35 additions & 10 deletions infrastructure/devicepathresolver/id_device_path_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var _ = Describe("IDDevicePathResolver", func() {
udev = fakeudev.NewFakeUdevDevice()
fs = fakesys.NewFakeFileSystem()
diskSettings = boshsettings.DiskSettings{
ID: "fake-disk-id-include-longname",
ID: "some-fake-disk-id-include-longname",
}
})

Expand All @@ -54,21 +54,46 @@ var _ = Describe("IDDevicePathResolver", func() {
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")
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"})
fs.SetGlob("/dev/disk/by-id/*fake_disk_id_include_longname", []string{"/dev/disk/by-id/virtio-fake_disk_id_include_longname"})
})

It("returns fully resolved the path (not potentially relative symlink target)", func() {
path, timeout, err := pathResolver.GetRealDevicePath(diskSettings)
Expect(err).ToNot(HaveOccurred())
Context("and disk id has a prefix separated by hyphen", func() {
BeforeEach(func() {
diskSettings = boshsettings.DiskSettings{
ID: "some-fake_disk_id_include_longname",
}
})
It("returns fully resolved the path (not potentially relative symlink target)", func() {
path, timeout, err := pathResolver.GetRealDevicePath(diskSettings)
Expect(err).ToNot(HaveOccurred())

devicePath, err := filepath.Abs("/dev/fake-device-path")
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())
Expect(path).To(Equal(devicePath))
Expect(timeout).To(BeFalse())
})
})

Context("and disk id has no prefix", func() {
BeforeEach(func() {
diskSettings = boshsettings.DiskSettings{
ID: "fake_disk_id_include_longname",
}
})
It("returns fully resolved the path (not potentially relative symlink target)", 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())
})
})
})

Expand Down

0 comments on commit 6636681

Please sign in to comment.