@@ -68,6 +68,7 @@ def nvme_connect(uri, delay=10, tmo=600):
68
68
command = (
69
69
f"nix-sudo nvme connect -t tcp -s { port } -a { host } -n { nqn } -c { delay } -l { tmo } "
70
70
)
71
+ print (command )
71
72
subprocess .run (command , check = True , shell = True , capture_output = False )
72
73
time .sleep (1 )
73
74
command = "nix-sudo nvme list -v -o json"
@@ -97,6 +98,43 @@ def nvme_id_ctrl(device):
97
98
return id_ctrl
98
99
99
100
101
+ def match_host_port (addr , host , port ):
102
+ traddr = f"traddr={ host } "
103
+ trsvcid = f"trsvcid={ port } "
104
+
105
+ return traddr in addr and trsvcid in addr
106
+
107
+
108
+ def nvme_find_ctrl (uri ):
109
+ """Find controller from the device uri."""
110
+ u = urlparse (uri )
111
+ port = u .port
112
+ host = u .hostname
113
+ nqn = u .path [1 :]
114
+
115
+ command = "nix-sudo nvme list -v -o json"
116
+ discover = json .loads (
117
+ subprocess .run (
118
+ command , shell = True , check = True , text = True , capture_output = True
119
+ ).stdout
120
+ )
121
+
122
+ # Finds correct Device
123
+ devs = list (filter (lambda d : nqn in d .get ("SubsystemNQN" ), discover .get ("Devices" )))
124
+ assert len (devs ) is 1 , "Multiple devices with the same subnqn"
125
+
126
+ # Find correct Controller
127
+ ctrls = list (
128
+ filter (
129
+ lambda d : match_host_port (d .get ("Address" ), host , port ),
130
+ devs [0 ].get ("Controllers" ),
131
+ )
132
+ )
133
+ assert len (ctrls ) is 1 , "Multiple controllers with the same address"
134
+
135
+ return ctrls [0 ].get ("Controller" )
136
+
137
+
100
138
def nvme_resv_report (device ):
101
139
"""Reservation report."""
102
140
command = "nix-sudo nvme resv-report {0} -c 1 -o json" .format (device )
@@ -129,18 +167,21 @@ def nvme_disconnect(uri):
129
167
nqn = u .path [1 :]
130
168
131
169
command = "nix-sudo nvme disconnect -n {0}" .format (nqn )
170
+ print (command )
132
171
subprocess .run (command , check = True , shell = True , capture_output = True )
133
172
134
173
135
174
def nvme_disconnect_controller (name ):
136
175
"""Disconnect the given NVMe controller on this host."""
137
176
command = "nix-sudo nvme disconnect -d {0}" .format (name )
177
+ print (command )
138
178
subprocess .run (command , check = True , shell = True , capture_output = True )
139
179
140
180
141
181
def nvme_disconnect_all ():
142
182
"""Disconnect from all connected nvme subsystems"""
143
183
command = "nix-sudo nvme disconnect-all"
184
+ print (command )
144
185
subprocess .run (command , check = True , shell = True , capture_output = True )
145
186
146
187
0 commit comments