From c03598a2e2cee38eaf6e098c470e99bd65c104ee Mon Sep 17 00:00:00 2001 From: Stanislaw Adaszewski Date: Thu, 21 May 2020 19:34:10 +0200 Subject: [PATCH] Slightly ugly workaround for JID issues when numeric-only characters are used in jail name. --- focker/jail.py | 7 ++++--- focker/misc.py | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/focker/jail.py b/focker/jail.py index 4300fd7..1512891 100644 --- a/focker/jail.py +++ b/focker/jail.py @@ -17,7 +17,8 @@ from .mount import getmntinfo import shlex import stat from .misc import focker_lock, \ - focker_unlock + focker_unlock, \ + random_sha256_hexdigest def backup_file(fname, nbackups=10, chmod=0o600): @@ -44,7 +45,7 @@ def jail_conf_write(conf): def jail_fs_create(image=None): - sha256 = bytes([ random.randint(0, 255) for _ in range(32) ]).hex() + sha256 = random_sha256_hexdigest() lst = zfs_list(fields=['focker:sha256'], focker_type='image') lst = list(filter(lambda a: a[0] == sha256, lst)) if lst: @@ -273,7 +274,7 @@ def command_jail_oneshot_old(): base, _ = zfs_snapshot_by_tag_or_sha256(args.image) # root = '/'.join(base.split('/')[:-1]) for _ in range(10**6): - sha256 = bytes([ random.randint(0, 255) for _ in range(32) ]).hex() + sha256 = random_sha256_hexdigest() name = sha256[:7] name = base.split('/')[0] + '/focker/jails/' + name if not zfs_exists(name): diff --git a/focker/misc.py b/focker/misc.py index dee07d7..8122106 100644 --- a/focker/misc.py +++ b/focker/misc.py @@ -12,7 +12,11 @@ import fcntl def random_sha256_hexdigest(): - return bytes([ random.randint(0, 255) for _ in range(32) ]).hex() + for _ in range(10**6): + res = bytes([ random.randint(0, 255) for _ in range(32) ]).hex() + if not res[:7].isnumeric(): + return res + raise ValueError('Couldn\'t find random SHA256 hash with non-numeric 7-character prefix in 10^6 trials o_O') def find_prefix(head, tail):