Browse Source

Use libblkid instead of CLI

This patch changes the get_block_uuid function around
to use libblkid instead of scraping the cli.

Closes-Bug: 1660240
Change-Id: Ieebf5714bceb9d41c70fc1dd0cfd17c903c3a107
changes/29/426929/1
Chris Holcombe 2 years ago
parent
commit
a7f29e52ef
1 changed files with 42 additions and 12 deletions
  1. 42
    12
      ceph/__init__.py

+ 42
- 12
ceph/__init__.py View File

@@ -11,6 +11,7 @@
11 11
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 12
 # See the License for the specific language governing permissions and
13 13
 # limitations under the License.
14
+from _ctypes import POINTER, byref
14 15
 import ctypes
15 16
 import collections
16 17
 import json
@@ -309,22 +310,52 @@ def set_hdd_read_ahead(dev_name, read_ahead_sectors=256):
309 310
 
310 311
 def get_block_uuid(block_dev):
311 312
     """
312
-    This queries blkid to get the uuid for a block device.
313
+    This queries blkid to get the uuid for a block device. Note: This function
314
+    needs to be called with root priv.  It will raise an error otherwise.
313 315
     :param block_dev: Name of the block device to query.
314
-    :return: The UUID of the device or None on Error.
316
+    :return: The UUID of the device or None on Error. Raises OSError
315 317
     """
316 318
     try:
317
-        block_info = subprocess.check_output(
318
-            ['blkid', '-o', 'export', block_dev])
319
-        for tag in block_info.split('\n'):
320
-            parts = tag.split('=')
321
-            if parts[0] == 'UUID':
322
-                return parts[1]
323
-        return None
324
-    except subprocess.CalledProcessError as err:
325
-        log('get_block_uuid failed with error: {}'.format(err.output),
319
+        blkid = ctypes.cdll.LoadLibrary("libblkid.so")
320
+        # Header signature
321
+        # extern int blkid_probe_lookup_value(blkid_probe pr, const char *name,
322
+        #                                     const char **data, size_t *len);
323
+        blkid.blkid_new_probe_from_filename.argtypes = [ctypes.c_char_p]
324
+        blkid.blkid_probe_lookup_value.argtypes = [ctypes.c_void_p,
325
+                                                   ctypes.c_char_p,
326
+                                                   POINTER(ctypes.c_char_p),
327
+                                                   POINTER(ctypes.c_ulong)]
328
+    except OSError as err:
329
+        log('get_block_uuid loading libblkid.so failed with error: {}'.format(
330
+            os.strerror(err.errno)),
326 331
             level=ERROR)
332
+        raise err
333
+    if not os.path.exists(block_dev):
327 334
         return None
335
+    probe = blkid.blkid_new_probe_from_filename(ctypes.c_char_p(block_dev))
336
+    if probe < 0:
337
+        log('get_block_uuid new_probe_from_filename failed: {}'.format(
338
+            os.strerror(probe)),
339
+            level=ERROR)
340
+        raise OSError(probe, os.strerror(probe))
341
+    result = blkid.blkid_do_probe(probe)
342
+    if result != 0:
343
+        log('get_block_uuid do_probe failed with error: {}'.format(
344
+            os.strerror(result)),
345
+            level=ERROR)
346
+        raise OSError(result, os.strerror(result))
347
+    uuid = ctypes.c_char_p()
348
+    result = blkid.blkid_probe_lookup_value(probe,
349
+                                            ctypes.c_char_p(
350
+                                                'UUID'.encode('ascii')),
351
+                                            byref(uuid), None)
352
+    if result < 0:
353
+        log('get_block_uuid lookup_value failed with error: {}'.format(
354
+            os.strerror(result)),
355
+            level=ERROR)
356
+        raise OSError(result, os.strerror(result))
357
+    blkid.blkid_free_probe(probe)
358
+    return ctypes.string_at(uuid).decode('ascii')
328 359
 
329 360
 
330 361
 def check_max_sectors(save_settings_dict,
@@ -1615,7 +1646,6 @@ def list_pools(service):
1615 1646
         log("rados lspools failed with error: {}".format(err.output))
1616 1647
         raise
1617 1648
 
1618
-
1619 1649
 # A dict of valid ceph upgrade paths.  Mapping is old -> new
1620 1650
 UPGRADE_PATHS = {
1621 1651
     'firefly': 'hammer',

Loading…
Cancel
Save