disk: improve disk setup command validation

Checks:
--type is gpt or dos.
--num must have numeric argument.
--part defines 4 coma-separated values.
--part number must be an integer.
--part number is not repeated between partitions.
--part partition type is a known type.
--part filesystem type is a known type.
--part size has a valid format.
master v0.3.3-6
Alejandro Sirgo Rica 2024-06-03 15:47:17 +02:00
parent 7ff45e5fcc
commit 29ab6cc0ed
1 changed files with 30 additions and 7 deletions

View File

@ -37,8 +37,7 @@ class OgDisk():
if len(match.groups()) == 2:
number, unit = match.groups()
return str(int(float(number)*units[unit]))
print(f'Error parsing size {size}. Aborting.')
return None
raise ValueError(f'error parsing size {size}. Examples of valid sizes: 200M, 2G, 1T')
disk_type_map = {'dos': 'MSDOS', 'gpt': 'GPT'}
part_types = ['LINUX', 'EFI', 'WINDOWS', 'CACHE']
@ -96,21 +95,45 @@ class OgDisk():
print("Missing --client-ip, or --room-id/--center-id. No clients provided.")
return None
if not parsed_args.num.isdigit():
print(f'Invalid disk number: must be an integer value')
return
payload = {'clients': parsed_args.client_ip, 'type': disk_type_map[parsed_args.type], 'disk': str(parsed_args.num),
'cache': '0', 'cache_size': '0', 'partition_setup': []}
defined_part_indices = set()
for i, p in enumerate(parsed_args.part, start=1):
p = p[0]
if len(p) != 4:
print(f'Invalid partition: requires "num,part_scheme,fs,size", "{','.join(p)}" provided')
return
part_num, code, fs, size = p[0], p[1].upper(), p[2].upper(), p[3]
if not part_num.isdigit():
print(f'Invalid partition: the first parameter must be a number, "{part_num}" provided')
return
if part_num in defined_part_indices:
print(f'Invalid partition: partition number "{part_num}" has multiple definitions')
return
defined_part_indices.add(part_num)
if code not in part_types:
print(
f'Specified partition type {code} is not supported. Aborting...')
print(f'Invalid partition {i}: specified partition type {code} is not supported. The supported formats are {part_types}')
return
if fs not in fs_types:
print(
f'Specified filesystem {code} is not supported. Aborting...')
print(f'Invalid partition {i}: specified filesystem {fs} is not supported. The supported formats are {fs_types}')
return
try:
size = parse_size(size)
except ValueError as error:
print(f'Invalid partition {i}: {str(error)}')
return
size = parse_size(size)
for j in range(i, int(part_num)):
part = {'partition': str(j), 'code': 'EMPTY',