Skip to content

Commit 1bb2147

Browse files
committed
Refactor diskutil to get uncached device-ids
1 parent cd78965 commit 1bb2147

1 file changed

Lines changed: 53 additions & 21 deletions

File tree

pkg/internal/diskutil.go

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ const (
3131
DiskDMDir = "/dev/mapper/"
3232
)
3333

34+
var (
35+
preferredPatterns = []string{
36+
"wwn",
37+
"scsi-3",
38+
"scsi-2",
39+
"scsi-8",
40+
"scsi-S",
41+
"scsi-1",
42+
"scsi-0",
43+
"scsi",
44+
"nvme-eui",
45+
"nvme",
46+
"",
47+
}
48+
)
49+
3450
// IDPathNotFoundError indicates that a symlink to the device was not found in /dev/disk/by-id/
3551
type IDPathNotFoundError struct {
3652
DeviceName string
@@ -176,20 +192,44 @@ func (b *BlockDevice) GetPathByID(existingDeviceID string) (string, error) {
176192
if err != nil {
177193
return "", fmt.Errorf("error listing files in %s: %v", DiskByIDDir, err)
178194
}
179-
preferredPatterns := []string{
180-
"wwn",
181-
"scsi-3",
182-
"scsi-2",
183-
"scsi-8",
184-
"scsi-S",
185-
"scsi-1",
186-
"scsi-0",
187-
"scsi",
188-
"nvme-eui",
189-
"nvme",
190-
"",
195+
196+
diskPathID, err := b.findDeviceInSortedSymlink(existingDeviceID, allDisks)
197+
if err != nil {
198+
return "", err
191199
}
192200

201+
if diskPathID != "" {
202+
b.PathByID = diskPathID
203+
return diskPathID, nil
204+
}
205+
206+
devPath, err := b.GetDevPath()
207+
if err != nil {
208+
return "", err
209+
}
210+
// return path by label and error
211+
return devPath, IDPathNotFoundError{DeviceName: b.KName}
212+
}
213+
214+
func (b *BlockDevice) GetUncachedPathID() (string, error) {
215+
allDisks, err := FilePathGlob(filepath.Join(DiskByIDDir, "/*"))
216+
if err != nil {
217+
return "", fmt.Errorf("error listing files in %s: %v", DiskByIDDir, err)
218+
}
219+
diskPathID, err := b.findDeviceInSortedSymlink("", allDisks)
220+
if err != nil {
221+
return "", err
222+
}
223+
224+
if diskPathID != "" {
225+
b.PathByID = diskPathID
226+
return diskPathID, nil
227+
}
228+
// return path by label and error
229+
return "", IDPathNotFoundError{DeviceName: b.KName}
230+
}
231+
232+
func (b *BlockDevice) findDeviceInSortedSymlink(existingDeviceID string, allDisks []string) (string, error) {
193233
// sortedSymlinks sorts symlinks in 4 buckets.
194234
// - [0] - symlinks that match wwn
195235
// - [1] - symlinks that match scsi - these are further sorted by the "328S10" prefix priority list used by lsscsi:
@@ -208,7 +248,6 @@ func (b *BlockDevice) GetPathByID(existingDeviceID string) (string, error) {
208248
return "", err
209249
}
210250
if isMatch {
211-
b.PathByID = path
212251
return path, nil
213252
}
214253
}
@@ -228,18 +267,11 @@ func (b *BlockDevice) GetPathByID(existingDeviceID string) (string, error) {
228267
return "", err
229268
}
230269
if isMatch {
231-
b.PathByID = path
232270
return path, nil
233271
}
234272
}
235273
}
236-
237-
devPath, err := b.GetDevPath()
238-
if err != nil {
239-
return "", err
240-
}
241-
// return path by label and error
242-
return devPath, IDPathNotFoundError{DeviceName: b.KName}
274+
return "", nil
243275
}
244276

245277
// PathEvalsToDiskLabel checks if the path is a symplink to a file devName

0 commit comments

Comments
 (0)