Skip to content

Instantly share code, notes, and snippets.

@devilholk
Last active September 25, 2020 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devilholk/4646a4b2253f6bb4b6ecc4a337d8891d to your computer and use it in GitHub Desktop.
Save devilholk/4646a4b2253f6bb4b6ecc4a337d8891d to your computer and use it in GitHub Desktop.
Get filesystem ID from device node
import ctypes
lib = ctypes.CDLL('libblkid.so')
lib.blkid_new_probe_from_filename.argtypes = (ctypes.c_char_p,)
lib.blkid_new_probe_from_filename.restype = ctypes.c_void_p
lib.blkid_free_probe.argtypes = (ctypes.c_void_p,)
lib.blkid_do_probe.argtypes = (ctypes.c_void_p,)
lib.blkid_do_probe.restype = ctypes.c_int
lib.blkid_probe_lookup_value.argtypes = (ctypes.c_void_p, ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p), ctypes.POINTER(ctypes.c_size_t))
lib.blkid_probe_lookup_value.restype = ctypes.c_int
lib.blkid_probe_numof_values.argtypes = (ctypes.c_void_p,)
lib.blkid_probe_numof_values.restype = ctypes.c_int
def get_fs_uuid(dev_name):
probe = lib.blkid_new_probe_from_filename(bytes(dev_name, 'ascii'))
assert probe
done = False
found_uuid = None
while True:
state = lib.blkid_do_probe(probe)
if state == 0:
UUID_len = ctypes.c_size_t()
UUID = ctypes.c_char_p()
res = lib.blkid_probe_lookup_value(probe, b'UUID', ctypes.byref(UUID), ctypes.byref(UUID_len))
if res == 0 and UUID_len.value:
found_uuid = UUID.value[:UUID_len.value]
break #We are done so no reason to keep probing
elif state == -1:
raise Exception('Failure happened!')
break
elif state == 1:
done = True
break
else:
raise Exception('Unexpected state')
break
lib.blkid_free_probe(probe)
return found_uuid
#print(get_fs_uuid('/dev/sdd1'))
#b'1234-ABCD'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment