Last active
September 25, 2020 12:42
-
-
Save devilholk/4646a4b2253f6bb4b6ecc4a337d8891d to your computer and use it in GitHub Desktop.
Get filesystem ID from device node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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