Created
November 27, 2018 02:26
-
-
Save TripleDogDare/3695490db45e03f0154d25597092a6e2 to your computer and use it in GitHub Desktop.
Generates and writes a new UUID for an NTFS partition. Used for disks from the same manufacturer that have the same UUID. This allows you to mount via UUID in /etc/fstab.
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
#!/usr/bin/env python3 | |
# file: rewrite_ntfs_uuid.py | |
import sys | |
import random | |
import os | |
# partition file | |
fileName = sys.argv[1] # e.g. /dev/sdb1 | |
UUID_OFFSET = 72 # location of NTFS UUID | |
UUID_LENGTH = 8 | |
def print_hex(*args, **kwargs): | |
if len(args) == 1: | |
data = args[0] | |
fmt = None | |
if len(args) > 1: | |
fmt = args[0] | |
data = args[1] | |
args = args[2:] | |
else: | |
raise ArgumentError('must have a byte array') | |
hex_str = ''.join('{:02x} '.format(x) for x in data) | |
if fmt: | |
print(fmt.format(hex_str, *args, **kwargs)) | |
else: | |
print(hex_str) | |
def read_uuid(fh): | |
fh.seek(UUID_OFFSET) | |
data = fh.read(UUID_LENGTH) | |
return data | |
def write_uuid(fh, uuid): | |
fh.seek(UUID_OFFSET) | |
fh.write(uuid) | |
fh.flush() | |
os.fsync(fh.fileno()) | |
def _main(): | |
new_uuid = bytes(random.randint(0, 255) for _ in range(UUID_LENGTH)) | |
print_hex('generated uuid: {}', new_uuid) | |
with open(fileName, "r+b") as fh: | |
data = read_uuid(fh) | |
print_hex('current uuid: {}', data) | |
write_uuid(fh, new_uuid) | |
data = read_uuid(fh) | |
print_hex('new uuid: {}', data) | |
if __name__ == "__main__": | |
_main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment