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 4783429 commit 1b74f65
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 49 deletions.
15 changes: 8 additions & 7 deletions infrastructure/devicepathresolver/id_device_path_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ func (idpr idDevicePathResolver) GetRealDevicePath(diskSettings boshsettings.Dis
stopAfter := time.Now().Add(idpr.diskWaitTimeout)
found := false

var realPath string

diskID := diskSettings.ID
index := strings.IndexRune(diskSettings.ID, '-')

if index != -1 {
diskID = diskSettings.ID[index+1:]
var realPath, diskID string

dashIndex := strings.IndexRune(diskSettings.ID, '-')
if dashIndex != -1 {
//TODO a comment why this is required
diskID = diskSettings.ID[dashIndex+1:]
} else {
diskID = diskSettings.ID
}

deviceGlobPattern := fmt.Sprintf("*%s", diskID)
Expand Down
77 changes: 35 additions & 42 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/*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 Expand Up @@ -96,38 +121,6 @@ var _ = Describe("IDDevicePathResolver", func() {
})
})

Context("when disks without a hyphen character exist ", func() {
BeforeEach(func() {
err := fs.MkdirAll("/dev", os.FileMode(0750))
Expect(err).ToNot(HaveOccurred())

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"})

diskSettings = boshsettings.DiskSettings{
ID: "fake_disk_id_include_longname",
}
})
It("returns an error", 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())
})
})

Context("when path does not exist", func() {
BeforeEach(func() {
err := fs.Symlink("fake-device-path", "/dev/disk/by-id/virtio-fake-disk-id-include-longname")
Expand Down

0 comments on commit 1b74f65

Please sign in to comment.