Skip to content

Commit 12bb4c2

Browse files
committed
Merge remote-tracking branch 'origin/fix-vm2-nvme-aliasing-on-ubu20' into build
2 parents dc52a54 + 4e14c70 commit 12bb4c2

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

tools/id_disk_custom_alias

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ from typing import List, Tuple, Optional, Dict
77
import pyudev
88
import sys
99
import os
10+
import re
1011

12+
context = pyudev.Context()
1113

1214
def set_env_var(key, value):
1315
print(f"{key}={value}")
@@ -51,21 +53,19 @@ def get_device_from_devpath(devpath: str) -> pyudev.Device:
5153
Given a DEVPATH (e.g. '/devices/.../tty/ttyUSB0'),
5254
return the corresponding pyudev Device.
5355
"""
54-
context = pyudev.Context()
5556
sys_path = os.path.join("/sys", devpath.lstrip("/"))
5657
return pyudev.Devices.from_sys_path(context, sys_path)
5758

5859

59-
def iter_kernel_ancestors(device: pyudev.Device, include_self: bool = False):
60-
"""
61-
Yield sys_name (kernel name) for the device and its parents.
62-
63-
sys_name is what udev matches with KERNELS=="...".
64-
"""
65-
dev = device if include_self else device.parent
66-
while dev is not None:
67-
yield dev
68-
dev = dev.parent
60+
def get_pcieport_parent_sys_name(device: pyudev.Device) -> Optional[str]:
61+
try:
62+
pcieport_parent = next(
63+
d for d in device.ancestors if d.driver == "pcieport"
64+
)
65+
return pcieport_parent.sys_name
66+
except StopIteration:
67+
# no pcieport parent found
68+
return None
6969

7070

7171
def main():
@@ -86,22 +86,23 @@ def main():
8686
if not devpath:
8787
set_env_var("CUSTOM_ALIAS_ERR", "device path not provided!")
8888
return
89-
89+
9090
device = get_device_from_devpath(devpath)
91-
92-
try:
93-
pcieport_parent = next(
94-
d for d in iter_kernel_ancestors(device) if d.driver == "pcieport"
95-
)
96-
except StopIteration:
97-
# no pcieport parent found
98-
return
99-
100-
if pcieport_parent.sys_name not in alias_parents:
91+
pcieport_parent_sys_name = get_pcieport_parent_sys_name(device)
92+
if pcieport_parent_sys_name is None:
93+
# special case for NVMe:
94+
# try stripping down to nvme subsystem device
95+
nvme_block_name: str = device.sys_name
96+
m = re.match(r"(nvme\d+)n\d+", nvme_block_name)
97+
if m:
98+
nvme_device = pyudev.Devices.from_name(context, "nvme", m.group(1))
99+
pcieport_parent_sys_name = get_pcieport_parent_sys_name(nvme_device)
100+
101+
if pcieport_parent_sys_name not in alias_parents:
101102
# not mapped
102103
return
103104

104-
alias = alias_parents[pcieport_parent.sys_name]
105+
alias = alias_parents[pcieport_parent_sys_name]
105106

106107
set_env_var("ID_VDEV", alias)
107108
set_env_var("ID_VDEV_PATH", os.path.join("disk/by-vdev", alias))

tools/lsdev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def build_server(options):
457457
bay["dev-by-path"] = regex.group(3) # third capture group
458458
bay["bay-id"] = str(CARD) + "-" + str(DRIVE)
459459
# if symlink exists in /dev/disk/by-path/ then bay is occupied
460-
if os.path.islink(bay["dev-by-path"]):
460+
if os.path.exists(bay["dev-by-path"]):
461461
bay["occupied"] = True
462462
bay["dev"] = os.path.realpath(bay["dev-by-path"])
463463
bay["partitions"] = count_partitions(bay["dev"])

udev/68-0-custom-aliases.rules

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
# Josh Boudreau <jboudreau@45drives.com>
1010

1111
SUBSYSTEMS=="pci", DRIVERS=="nvme", SUBSYSTEM=="block", ENV{DEVTYPE}=="disk|partition", IMPORT{program}="/opt/45drives/tools/id_disk_custom_alias %p"
12+
# for when nvme block dev is virtual ?
13+
SUBSYSTEMS=="nvme-subsystem", SUBSYSTEM=="block", ENV{DEVTYPE}=="disk|partition", IMPORT{program}="/opt/45drives/tools/id_disk_custom_alias %p"
1214

1315
# skip running vdev_id
1416
ACTION=="add|change", ENV{SUBSYSTEM}=="block", ENV{ID_VDEV}=="?*", ENV{ID_VDEV_PATH}=="?*", ENV{ID_VDEV_ALT_PATH}=="?*", ENV{DISK_HANDLED_BY_CUSTOM_ALIASES}="1"
1517

16-
ENV{DISK_HANDLED_BY_CUSTOM_ALIASES}=="1", KERNEL=="?*", ENV{SUBSYSTEM}=="block", ENV{DEVTYPE}=="disk", ENV{ID_PATH}=="?*", ENV{ID_VDEV}=="?*", RUN+="/opt/45drives/ubm/patch_vdev_id_conf $env{ID_VDEV} $env{ID_PATH}"
18+
ENV{DISK_HANDLED_BY_CUSTOM_ALIASES}!="1", GOTO="68-0-custom-aliases-end"
19+
KERNEL=="?*", ENV{SUBSYSTEM}=="block", ENV{DEVTYPE}=="disk", ENV{ID_VDEV}=="?*", ENV{ID_PATH}=="?*", RUN+="/opt/45drives/ubm/patch_vdev_id_conf $env{ID_VDEV} $env{ID_PATH}", GOTO="68-0-custom-aliases-end"
20+
# bit of a hack but if ID_PATH is missing, fallback to patching to '/dev/disk/by-path/../../nvme0n1', etc
21+
KERNEL=="?*", ENV{SUBSYSTEM}=="block", ENV{DEVTYPE}=="disk", ENV{ID_VDEV}=="?*", RUN+="/opt/45drives/ubm/patch_vdev_id_conf $env{ID_VDEV} ../../%k", GOTO="68-0-custom-aliases-end"
22+
LABEL="68-0-custom-aliases-end"
1723

1824
# let 68-vdev.rules handle the rest

0 commit comments

Comments
 (0)