@@ -170,6 +170,16 @@ class BSDNvmecli(Nvmecli):
170
170
# nvme0ns1 (1831420MB)
171
171
# nvme10ns12 (1831420MB)
172
172
_namespace_cli_pattern = re .compile (r"(?P<namespace>nvme\d+ns\d+)" )
173
+ # nvme0ns1 (10293847MB)
174
+ _devicename_from_namespace_pattern = re .compile (r"(?P<devicename>nvme\d+)ns\d+" )
175
+ # Total NVM Capacity: 1920383410176 bytes
176
+ _total_storage_pattern = re .compile (
177
+ r"Total NVM Capacity:\s+(?P<storage>\d+)\s+bytes"
178
+ )
179
+ # Format NVM: Supported
180
+ __format_device_support = "Format NVM: Supported"
181
+ # Namespace Management: Supported
182
+ __ns_management_attachement_support = "Namespace Management: Supported"
173
183
174
184
@property
175
185
def command (self ) -> str :
@@ -196,3 +206,55 @@ def get_namespaces(self, force_run: bool = False) -> List[str]:
196
206
namespaces_cli .append (f"/dev/{ namespace } " )
197
207
198
208
return namespaces_cli
209
+
210
+ def support_device_format (self , device_name : str ) -> bool :
211
+ name_without_dev = device_name .replace ("/dev/" , "" )
212
+ cmd_result = self .run (f"identify { name_without_dev } " , shell = True , sudo = True )
213
+ cmd_result .assert_exit_code (
214
+ message = f"Failed to identify settings for { device_name } ."
215
+ )
216
+ return self .__format_device_support in cmd_result .stdout
217
+
218
+ def support_ns_manage_attach (self , device_name : str ) -> bool :
219
+ name_without_dev = device_name .replace ("/dev/" , "" )
220
+ cmd_result = self .run (f"identify { name_without_dev } " , shell = True , sudo = True )
221
+ cmd_result .assert_exit_code ()
222
+ return self .__ns_management_attachement_support in cmd_result .stdout
223
+
224
+ def create_namespace (self , namespace : str ) -> ExecutableResult :
225
+ device_name = find_patterns_in_lines (
226
+ namespace , [self ._devicename_from_namespace_pattern ]
227
+ )[0 ][0 ]
228
+ cmd_result = self .run (f"identify { device_name } " , shell = True , sudo = True )
229
+ cmd_result .assert_exit_code (
230
+ message = "Failed to identify devicename and collect"
231
+ " requirements for namespace creation."
232
+ )
233
+ total_storage_space_in_bytes = find_patterns_in_lines (
234
+ cmd_result .stdout , [self ._total_storage_pattern ]
235
+ )[0 ][0 ]
236
+ # Using the same block size as linux tests of 4096 bytes
237
+ total_storage_space_in_blocks = int (total_storage_space_in_bytes ) // 4096
238
+ cmd_result = self .run (
239
+ f"ns create -s { total_storage_space_in_blocks } -c "
240
+ f"{ total_storage_space_in_blocks } { device_name } " ,
241
+ shell = True ,
242
+ sudo = True ,
243
+ )
244
+ return cmd_result
245
+
246
+ def delete_namespace (self , namespace : str , id_ : int ) -> ExecutableResult :
247
+ device_name = find_patterns_in_lines (
248
+ namespace , [self ._devicename_from_namespace_pattern ]
249
+ )[0 ][0 ]
250
+ return self .run (f"ns delete -n { id_ } { device_name } " , shell = True , sudo = True )
251
+
252
+ def detach_namespace (self , namespace : str , id_ : int ) -> ExecutableResult :
253
+ device_name = find_patterns_in_lines (
254
+ namespace , [self ._devicename_from_namespace_pattern ]
255
+ )[0 ][0 ]
256
+ return self .run (f"ns detach -n { id_ } -c 0 { device_name } " , shell = True , sudo = True )
257
+
258
+ def format_namespace (self , namespace : str ) -> ExecutableResult :
259
+ name_without_dev = namespace .replace ("/dev/" , "" )
260
+ return self .run (f"format { name_without_dev } " , shell = True , sudo = True )
0 commit comments