From 5cb3b0d176fff9d2c2fb814530b29eca5819fe8e Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Wed, 17 Feb 2021 12:24:56 +0100 Subject: Implemented #106 in branch skip-partitioning. Also moving the disk_password from being a local variable to a BlockDevice setting/variable. --- archinstall/lib/user_interaction.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index fdbabe96..f92cd008 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -1,10 +1,20 @@ +import getpass from .exceptions import * from .profiles import Profile from .locale_helpers import search_keyboard_layout +from .output import log ## TODO: Some inconsistencies between the selection processes. ## Some return the keys from the options, some the values? +def get_password(prompt="Enter a password: "): + while (passwd := getpass.getpass(prompt)): + passwd_verification = getpass.getpass(prompt='And one more time for verification: ') + if passwd != passwd_verification: + log(' * Passwords did not match * ', bg='black', fg='red') + continue + return passwd + def generic_select(options, input_text="Select one of the above by index or absolute value: ", sort=True): """ A generic select function that does not output anything -- cgit v1.2.3-70-g09d2 From 572d59e5607811c4f7456d09ad1744b2da3ea394 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Wed, 17 Feb 2021 13:30:14 +0100 Subject: Cleaning up guided.py a bit to be less complex and convoluted, while still performing the same task. --- archinstall/lib/exceptions.py | 2 + archinstall/lib/user_interaction.py | 71 +++++++++++++++ examples/guided.py | 177 ++++++++++++------------------------ 3 files changed, 133 insertions(+), 117 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/exceptions.py b/archinstall/lib/exceptions.py index 5186bfd4..5a5d47c6 100644 --- a/archinstall/lib/exceptions.py +++ b/archinstall/lib/exceptions.py @@ -13,4 +13,6 @@ class ProfileNotFound(BaseException): class HardwareIncompatibilityError(BaseException): pass class PermissionError(BaseException): + pass +class UserError(BaseException): pass \ No newline at end of file diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index f92cd008..440e41a1 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -14,6 +14,77 @@ def get_password(prompt="Enter a password: "): log(' * Passwords did not match * ', bg='black', fg='red') continue return passwd + return None + +def ask_for_superuser_account(prompt='Create a required super-user with sudo privileges: ', forced=False): + while 1: + new_user = input(prompt).strip(' ') + + if not new_user and forced: + # TODO: make this text more generic? + # It's only used to create the first sudo user when root is disabled in guided.py + log(' * Since root is disabled, you need to create a least one (super) user!', bg='black', fg='red') + continue + elif not new_user and not forced: + raise UserError("No superuser was created.") + + password = get_password(prompt=f'Password for user {new_user}: ') + return {new_user: password} + +def ask_for_additional_users(prompt='Any additional users to install (leave blank for no users): '): + users = {} + super_users = {} + + while 1: + new_user = input(prompt).strip(' ') + if not new_user: + break + password = get_password(prompt=f'Password for user {new_user}: ') + + if input("Should this user be a sudo (super) user (y/N): ").strip(' ').lower() in ('y', 'yes'): + super_users[new_user] = password + else: + users[new_user] = password + + return users, super_users + +def ask_to_configure_network(): + # Optionally configure one network interface. + #while 1: + # {MAC: Ifname} + interfaces = {'ISO-CONFIG' : 'Copy ISO network configuration to installation', **archinstall.list_interfaces()} + archinstall.storage['_guided']['network'] = None + + nic = archinstall.generic_select(interfaces.values(), "Select one network interface to configure (leave blank to skip): ") + if nic and nic != 'Copy ISO network configuration to installation': + mode = archinstall.generic_select(['DHCP (auto detect)', 'IP (static)'], f"Select which mode to configure for {nic}: ") + if mode == 'IP (static)': + while 1: + ip = input(f"Enter the IP and subnet for {nic} (example: 192.168.0.5/24): ").strip() + if ip: + break + else: + ArchInstall.log( + "You need to enter a valid IP in IP-config mode.", + level=archinstall.LOG_LEVELS.Warning, + bg='black', + fg='red' + ) + + if not len(gateway := input('Enter your gateway (router) IP address or leave blank for none: ').strip()): + gateway = None + + dns = None + if len(dns_input := input('Enter your DNS servers (space separated, blank for none): ').strip()): + dns = dns_input.split(' ') + + return {'nic': nic, 'dhcp': False, 'ip': ip, 'gateway' : gateway, 'dns' : dns} + else: + return {'nic': nic} + elif nic: + return nic + + return None def generic_select(options, input_text="Select one of the above by index or absolute value: ", sort=True): """ diff --git a/examples/guided.py b/examples/guided.py index 84676284..119f22b3 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -1,14 +1,10 @@ import getpass, time, json, sys, signal, os import archinstall -# Create a storage structure for all our information. -# We'll print this right before the user gets informed about the formatting timer. -archinstall.storage['_guided'] = {} -archinstall.storage['_guided_hidden'] = {} # This will simply be hidden from printouts and things. - """ This signal-handler chain (and global variable) is used to trigger the "Are you sure you want to abort?" question further down. +It might look a bit odd, but have a look at the line: "if SIG_TRIGGER:" """ SIG_TRIGGER = False def kill_handler(sig, frame): @@ -23,13 +19,14 @@ def sig_handler(sig, frame): original_sigint_handler = signal.getsignal(signal.SIGINT) signal.signal(signal.SIGINT, sig_handler) + def perform_installation(device, boot_partition, language, mirrors): """ Performs the installation steps on a block device. Only requirement is that the block devices are formatted and setup prior to entering this function. """ - with archinstall.Installer(device, boot_partition=boot_partition, hostname=archinstall.storage['_guided']['hostname']) as installation: + with archinstall.Installer(device, boot_partition=boot_partition, hostname=archinstall.arguments.get('hostname', 'Archinstall')) as installation: ## if len(mirrors): # Certain services might be running that affects the system during installation. # Currently, only one such service is "reflector.service" which updates /etc/pacman.d/mirrorlist @@ -152,137 +149,83 @@ if archinstall.arguments['harddrive'].has_partitions(): else: archinstall.arguments['harddrive'].keep_partitions = False +# Get disk encryption password (or skip if blank) +if not archinstall.arguments.get('encryption-password', None): + archinstall.arguments['encryption-password'] = archinstall.get_password(prompt='Enter disk encryption password (leave blank for no encryption): ') +archinstall.arguments['harddrive'].encryption_password = archinstall.arguments['encryption-password'] -disk_password = archinstall.get_password(prompt='Enter disk encryption password (leave blank for no encryption): ') -archinstall.arguments['harddrive'].encryption_passwoed = disk_password - -exit(0) -# Ask for a hostname -hostname = input('Desired hostname for the installation: ') -if len(hostname) == 0: - hostname = 'ArchInstall' -archinstall.storage['_guided']['hostname'] = hostname +# Get the hostname for the machine +if not archinstall.arguments.get('hostname', None): + archinstall.arguments['hostname'] = input('Desired hostname for the installation: ').strip(' ') # Ask for a root password (optional, but triggers requirement for super-user if skipped) -while (root_pw := getpass.getpass(prompt='Enter root password (leave blank to leave root disabled): ')): - root_pw_verification = getpass.getpass(prompt='And one more time for verification: ') - if root_pw != root_pw_verification: - archinstall.log(' * Passwords did not match * ', bg='black', fg='red') - continue - - # Storing things in _guided_hidden helps us avoid printing it - # when echoing user configuration: archinstall.storage['_guided'] - archinstall.storage['_guided_hidden']['root_pw'] = root_pw - archinstall.storage['_guided']['root_unlocked'] = True - break +if not archinstall.arguments.get('!root-password', None): + archinstall.arguments['!root-password'] = archinstall.get_password(prompt='Enter root password (Recommended: leave blank to leave root disabled): ') + +# # Storing things in _guided_hidden helps us avoid printing it +# # when echoing user configuration: archinstall.storage['_guided'] +# archinstall.storage['_guided_hidden']['root_pw'] = root_pw +# archinstall.storage['_guided']['root_unlocked'] = True +# break # Ask for additional users (super-user if root pw was not set) -users = {} -new_user_text = 'Any additional users to install (leave blank for no users): ' -if len(root_pw.strip()) == 0: - new_user_text = 'Create a super-user with sudo privileges: ' - -archinstall.storage['_guided']['users'] = None -while 1: - new_user = input(new_user_text) - if len(new_user.strip()) == 0: - if len(root_pw.strip()) == 0: - archinstall.log(' * Since root is disabled, you need to create a least one (super) user!', bg='black', fg='red') - continue - break - - if not archinstall.storage['_guided']['users']: - archinstall.storage['_guided']['users'] = [] - archinstall.storage['_guided']['users'].append(new_user) - - new_user_passwd = getpass.getpass(prompt=f'Password for user {new_user}: ') - new_user_passwd_verify = getpass.getpass(prompt=f'Enter password again for verification: ') - if new_user_passwd != new_user_passwd_verify: - archinstall.log(' * Passwords did not match * ', bg='black', fg='red') - continue - - users[new_user] = new_user_passwd - break +archinstall.arguments['users'] = {} +archinstall.arguments['superusers'] = {} +if not archinstall.arguments.get('!root-password', None): + archinstall.arguments['superusers'] = archinstall.ask_for_superuser_account('Create a required super-user with sudo privileges: ', forced=True) + +users, superusers = archinstall.ask_for_additional_users('Any additional users to install (leave blank for no users): ') +archinstall.arguments['users'] = users +archinstall.arguments['superusers'] = {**archinstall.arguments['superusers'], **superusers} # Ask for archinstall-specific profiles (such as desktop environments etc) -while 1: - profile = archinstall.select_profile(archinstall.list_profiles()) - if profile: - archinstall.storage['_guided']['profile'] = profile - - if type(profile) != str: # Got a imported profile - archinstall.storage['_guided']['profile'] = profile[0] # The second return is a module, and not a handle/object. - if not profile[1]._prep_function(): - # TODO: See how we can incorporate this into - # the general log flow. As this is pre-installation - # session setup. Which creates the installation.log file. - archinstall.log( - ' * Profile\'s preparation requirements was not fulfilled.', - bg='black', - fg='red' - ) - continue +if not archinstall.arguments.get('profile', None): + while 1: + profile = archinstall.select_profile(archinstall.list_profiles()) + print(profile) + if profile: + archinstall.storage['_guided']['profile'] = profile + + if type(profile) != str: # Got a imported profile + archinstall.storage['_guided']['profile'] = profile[0] # The second return is a module, and not a handle/object. + if not profile[1]._prep_function(): + # TODO: See how we can incorporate this into + # the general log flow. As this is pre-installation + # session setup. Which creates the installation.log file. + archinstall.log( + ' * Profile\'s preparation requirements was not fulfilled.', + bg='black', + fg='red' + ) + continue + break + else: break - else: - break # Additional packages (with some light weight error handling for invalid package names) -archinstall.storage['_guided']['packages'] = None -while 1: - packages = [package for package in input('Additional packages aside from base (space separated): ').split(' ') if len(package)] +if not archinstall.arguments.get('packages', None): + archinstall.arguments['packages'] = [package for package in input('Additional packages aside from base (space separated): ').split(' ') if len(package)] - if not packages: - break +# Verify packages that were given +try: + archinstall.validate_package_list(archinstall.arguments['packages']) +except archinstall.RequirementError as e: + archinstall.log(e, fg='red') + exit(1) - try: - if archinstall.validate_package_list(packages): - archinstall.storage['_guided']['packages'] = packages - break - except archinstall.RequirementError as e: - print(e) - -# Optionally configure one network interface. -#while 1: -# {MAC: Ifname} -interfaces = {'ISO-CONFIG' : 'Copy ISO network configuration to installation', **archinstall.list_interfaces()} -archinstall.storage['_guided']['network'] = None - -nic = archinstall.generic_select(interfaces.values(), "Select one network interface to configure (leave blank to skip): ") -if nic and nic != 'Copy ISO network configuration to installation': - mode = archinstall.generic_select(['DHCP (auto detect)', 'IP (static)'], f"Select which mode to configure for {nic}: ") - if mode == 'IP (static)': - while 1: - ip = input(f"Enter the IP and subnet for {nic} (example: 192.168.0.5/24): ").strip() - if ip: - break - else: - ArchInstall.log( - "You need to enter a valid IP in IP-config mode.", - level=archinstall.LOG_LEVELS.Warning, - bg='black', - fg='red' - ) - - if not len(gateway := input('Enter your gateway (router) IP address or leave blank for none: ').strip()): - gateway = None - - dns = None - if len(dns_input := input('Enter your DNS servers (space separated, blank for none): ').strip()): - dns = dns_input.split(' ') - - archinstall.storage['_guided']['network'] = {'nic': nic, 'dhcp': False, 'ip': ip, 'gateway' : gateway, 'dns' : dns} - else: - archinstall.storage['_guided']['network'] = {'nic': nic} -elif nic: - archinstall.storage['_guided']['network'] = nic +# Ask or Call the helper function that asks the user to optionally configure a network. +if not archinstall.arguments.get('nic', None): + archinstall.arguments['nic'] = archinstall.ask_to_configure_network() print() print('This is your chosen configuration:') archinstall.log("-- Guided template chosen (with below config) --", level=archinstall.LOG_LEVELS.Debug) archinstall.log(json.dumps(archinstall.storage['_guided'], indent=4, sort_keys=True, cls=archinstall.JSON), level=archinstall.LOG_LEVELS.Info) +archinstall.log(json.dumps(archinstall.arguments, indent=4, sort_keys=True, cls=archinstall.JSON), level=archinstall.LOG_LEVELS.Info) print() input('Press Enter to continue.') +exit(0) """ Issue a final warning before we continue with something un-revertable. -- cgit v1.2.3-70-g09d2 From 758b12e6746ac76c57e7725d4e35abbb4805ad23 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Wed, 17 Feb 2021 13:59:44 +0100 Subject: Simplifying the profile loading a bit, and adding some debugging for it. --- archinstall/lib/user_interaction.py | 4 ++-- examples/guided.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 440e41a1..bdf8acaf 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -188,9 +188,9 @@ def select_profile(options): if '__name__' in source_data and '_prep_function' in source_data: with profile.load_instructions(namespace=f"{selected_profile}.py") as imported: if hasattr(imported, '_prep_function'): - return profile, imported + return imported - return selected_profile + return profile raise RequirementError("Selecting profiles require a least one profile to be given as an option.") diff --git a/examples/guided.py b/examples/guided.py index 3226c69b..1758a397 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -185,6 +185,7 @@ else: archinstall.arguments['profile'] = archinstall.list_profiles()[archinstall.arguments['profile']] # Check the potentially selected profiles preperations to get early checks if some additional questions are needed. +print(archinstall.arguments['profile']) if archinstall.arguments['profile']: if not archinstall.arguments['profile']._prep_function(): archinstall.log( -- cgit v1.2.3-70-g09d2 From ad4733bbd0b0e889ad902a7d954ec985fc7a24fe Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Wed, 17 Feb 2021 14:21:46 +0100 Subject: Simplified profile prep-execution slightly in guided.py. The code can be improved further but it's now more easily read what's going on. --- archinstall/lib/profiles.py | 17 +++++++++++++++++ archinstall/lib/user_interaction.py | 18 +----------------- examples/guided.py | 18 +++++++++--------- 3 files changed, 27 insertions(+), 26 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/profiles.py b/archinstall/lib/profiles.py index f9aa206c..7b0e78e4 100644 --- a/archinstall/lib/profiles.py +++ b/archinstall/lib/profiles.py @@ -157,6 +157,23 @@ class Profile(Script): def install(self): return self.execute() + def has_prep_function(self): + with open(self.path, 'r') as source: + source_data = source.read() + + # Some crude safety checks, make sure the imported profile has + # a __name__ check and if so, check if it's got a _prep_function() + # we can call to ask for more user input. + # + # If the requirements are met, import with .py in the namespace to not + # trigger a traditional: + # if __name__ == 'moduleName' + if '__name__' in source_data and '_prep_function' in source_data: + with profile.load_instructions(namespace=f"{selected_profile}.py") as imported: + if hasattr(imported, '_prep_function'): + return True + return False + class Application(Profile): def __repr__(self, *args, **kwargs): return f'Application({os.path.basename(self.profile)})' diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index bdf8acaf..5941903d 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -174,23 +174,7 @@ def select_profile(options): else: RequirementError("Selected profile does not exist.") - profile = Profile(None, selected_profile) - with open(profile.path, 'r') as source: - source_data = source.read() - - # Some crude safety checks, make sure the imported profile has - # a __name__ check and if so, check if it's got a _prep_function() - # we can call to ask for more user input. - # - # If the requirements are met, import with .py in the namespace to not - # trigger a traditional: - # if __name__ == 'moduleName' - if '__name__' in source_data and '_prep_function' in source_data: - with profile.load_instructions(namespace=f"{selected_profile}.py") as imported: - if hasattr(imported, '_prep_function'): - return imported - - return profile + return Profile(None, selected_profile) raise RequirementError("Selecting profiles require a least one profile to be given as an option.") diff --git a/examples/guided.py b/examples/guided.py index 1758a397..4cd37972 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -185,15 +185,15 @@ else: archinstall.arguments['profile'] = archinstall.list_profiles()[archinstall.arguments['profile']] # Check the potentially selected profiles preperations to get early checks if some additional questions are needed. -print(archinstall.arguments['profile']) -if archinstall.arguments['profile']: - if not archinstall.arguments['profile']._prep_function(): - archinstall.log( - ' * Profile\'s preparation requirements was not fulfilled.', - bg='black', - fg='red' - ) - exit(1) +if archinstall.arguments['profile'] and archinstall.arguments['profile'].has_prep_function(): + with archinstall.arguments['profile'].load_instructions(namespace=f"{archinstall.arguments['profile'].namespace}.py") as imported: + if not imported._prep_function(): + archinstall.log( + ' * Profile\'s preparation requirements was not fulfilled.', + bg='black', + fg='red' + ) + exit(1) # Additional packages (with some light weight error handling for invalid package names) if not archinstall.arguments.get('packages', None): -- cgit v1.2.3-70-g09d2 From 20b343c99376f4132c41e6c3085c33248e7b15b6 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Wed, 17 Feb 2021 14:24:48 +0100 Subject: Added some forgotten imports --- archinstall/lib/user_interaction.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 5941903d..0ca4a636 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -2,7 +2,9 @@ import getpass from .exceptions import * from .profiles import Profile from .locale_helpers import search_keyboard_layout -from .output import log +from .output import log, LOG_LEVELS +from .storage import storage +from .networking import list_interfaces ## TODO: Some inconsistencies between the selection processes. ## Some return the keys from the options, some the values? @@ -52,21 +54,21 @@ def ask_to_configure_network(): # Optionally configure one network interface. #while 1: # {MAC: Ifname} - interfaces = {'ISO-CONFIG' : 'Copy ISO network configuration to installation', **archinstall.list_interfaces()} - archinstall.storage['_guided']['network'] = None + interfaces = {'ISO-CONFIG' : 'Copy ISO network configuration to installation', **list_interfaces()} + storage['_guided']['network'] = None - nic = archinstall.generic_select(interfaces.values(), "Select one network interface to configure (leave blank to skip): ") + nic = generic_select(interfaces.values(), "Select one network interface to configure (leave blank to skip): ") if nic and nic != 'Copy ISO network configuration to installation': - mode = archinstall.generic_select(['DHCP (auto detect)', 'IP (static)'], f"Select which mode to configure for {nic}: ") + mode = generic_select(['DHCP (auto detect)', 'IP (static)'], f"Select which mode to configure for {nic}: ") if mode == 'IP (static)': while 1: ip = input(f"Enter the IP and subnet for {nic} (example: 192.168.0.5/24): ").strip() if ip: break else: - ArchInstall.log( + log( "You need to enter a valid IP in IP-config mode.", - level=archinstall.LOG_LEVELS.Warning, + level=LOG_LEVELS.Warning, bg='black', fg='red' ) -- cgit v1.2.3-70-g09d2 From ad8389ccaf403f18f1ed35b51c5f03bc3f86166a Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Wed, 17 Feb 2021 14:29:13 +0100 Subject: Removed redundant variable --- archinstall/lib/user_interaction.py | 1 - 1 file changed, 1 deletion(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 0ca4a636..6a55df9c 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -55,7 +55,6 @@ def ask_to_configure_network(): #while 1: # {MAC: Ifname} interfaces = {'ISO-CONFIG' : 'Copy ISO network configuration to installation', **list_interfaces()} - storage['_guided']['network'] = None nic = generic_select(interfaces.values(), "Select one network interface to configure (leave blank to skip): ") if nic and nic != 'Copy ISO network configuration to installation': -- cgit v1.2.3-70-g09d2 From 8da8608e220a62c993f7bdf49c2c3d7c9b4a43aa Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Mon, 8 Mar 2021 14:42:43 +0100 Subject: Added a small menu instead of a one-liner to select what to do with the disk if it has partitions. --- archinstall/lib/user_interaction.py | 8 +++++ examples/guided.py | 59 +++++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 22 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 6a55df9c..e20e98b1 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -87,6 +87,14 @@ def ask_to_configure_network(): return None +def ask_for_disk_layout(): + options = { + 'keep-existing' : 'Keep existing partition layout and select which ones to use where.', + 'format-all' : 'Format entire drive and setup a basic partition scheme.' + } + + return generic_select(options.values(), "Found partitions on the selected drive, (select by number) what do you want to do: ") + def generic_select(options, input_text="Select one of the above by index or absolute value: ", sort=True): """ A generic select function that does not output anything diff --git a/examples/guided.py b/examples/guided.py index bccedb0a..27c005d6 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -101,6 +101,9 @@ else: # 2. If so, ask if we should keep them or wipe everything if archinstall.arguments['harddrive'].has_partitions(): archinstall.log(f" ! {archinstall.arguments['harddrive']} contains existing partitions", fg='red') + + # We curate a list pf supported paritions + # and print those that we don't support. partition_mountpoints = {} for partition in archinstall.arguments['harddrive']: try: @@ -109,44 +112,56 @@ if archinstall.arguments['harddrive'].has_partitions(): partition_mountpoints[partition] = None except archinstall.UnknownFilesystemFormat as err: archinstall.log(f" {partition} (Filesystem not supported)", fg='red') - #archinstall.log(f"Current filesystem is not supported: {err}", fg='red') - #input(f"Do you wish to erase all data? (y/n):") - if (option := input('Do you wish to keep one/more existing partitions or format entire drive? (k/f): ')).lower() in ('k', 'keep'): + # We then ask what to do with the paritions. + if (option := archinstall.ask_for_disk_layout()) == 'keep-existing': archinstall.arguments['harddrive'].keep_partitions = True - archinstall.log(f" ** You will now select where (inside the installation) to mount partitions. **") - archinstall.log(f" ** The root would be a simple / and the boot partition /boot as it's relative paths. **") + archinstall.log(f" ** You will now select which partitions to use by selecting mount points (inside the installation). **") + archinstall.log(f" ** The root would be a simple / and the boot partition /boot (as all paths are relative inside the installation). **") while True: - partition = archinstall.generic_select(partition_mountpoints.keys(), "Select a partition to assign mount-point to (leave blank when done): ") + # Select a partition + partition = archinstall.generic_select(partition_mountpoints.keys(), + "Select a partition by number that you want to set a mount-point for (leave blank when done): ") if not partition: break + # Select a mount-point mountpoint = input(f"Enter a mount-point for {partition}: ").strip(' ') + if len(mountpoint): - while 1: - new_filesystem = input(f"Enter a valid filesystem for {partition} (leave blank for {partition.filesystem}): ").strip(' ') - if len(new_filesystem) <= 0: + # Get a valid & supported filesystem for the parition: + while 1: + new_filesystem = input(f"Enter a valid filesystem for {partition} (leave blank for {partition.filesystem}): ").strip(' ') + if len(new_filesystem) <= 0: + break + + # Since the potentially new filesystem is new + # we have to check if we support it. We can do this by formatting /dev/null with the partitions filesystem. + # There's a nice wrapper for this on the partition object itself that supports a path-override during .format() + try: + partition.format(new_filesystem, path='/dev/null', log_formating=False, allow_formatting=True) + except archinstall.UnknownFilesystemFormat: + archinstall.log(f"Selected filesystem is not supported yet. If you want archinstall to support '{new_filesystem}', please create a issue-ticket suggesting it on github at https://github.com/Torxed/archinstall/issues.") + archinstall.log(f"Until then, please enter another supported filesystem.") + continue + except archinstall.SysCallError: + pass # Expected exception since mkfs. can not format /dev/null. + # But that means our .format() function supported it. break - try: - partition.format(new_filesystem, path='/dev/null', log_formating=False, allow_formatting=True) - except archinstall.UnknownFilesystemFormat: - archinstall.log(f"Selected filesystem is not supported yet, if you wish archinstall should support '{new_filesystem}' please create a issue-ticket suggesting it on github at https://github.com/Torxed/archinstall/issues.") - archinstall.log(f"Until then, please enter another supported filesystem.") - continue - except archinstall.SysCallError: - pass # Supported, but mkfs could not format /dev/null which is the whole point of /dev/null in path :) - break - - if len(mountpoint): + # When we've selected all three criterias, + # We can safely mark the partition for formatting and where to mount it. + # TODO: allow_formatting might be redundant since target_mountpoint should only be + # set if we actually want to format it anyway. partition.allow_formatting = True partition.target_mountpoint = mountpoint + # Only overwrite the filesystem definition if we selected one: if len(new_filesystem): partition.filesystem = new_filesystem archinstall.log('Using existing partition table reported above.') - else: + elif option == 'format-all': archinstall.arguments['harddrive'].keep_partitions = False # Get disk encryption password (or skip if blank) @@ -217,7 +232,6 @@ archinstall.log(json.dumps(archinstall.arguments, indent=4, sort_keys=True, cls= print() input('Press Enter to continue.') -exit(0) """ Issue a final warning before we continue with something un-revertable. @@ -249,6 +263,7 @@ for i in range(5, 0, -1): print() signal.signal(signal.SIGINT, original_sigint_handler) +exit(0) """ Setup the blockdevice, filesystem (and optionally encryption). Once that's done, we'll hand over to perform_installation() -- cgit v1.2.3-70-g09d2 From 775a26f738002ad124200716b68f92ebc9e91a5c Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Mon, 8 Mar 2021 14:47:24 +0100 Subject: Added a abort message --- archinstall/lib/user_interaction.py | 5 +++-- examples/guided.py | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index e20e98b1..01e3372c 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -90,10 +90,11 @@ def ask_to_configure_network(): def ask_for_disk_layout(): options = { 'keep-existing' : 'Keep existing partition layout and select which ones to use where.', - 'format-all' : 'Format entire drive and setup a basic partition scheme.' + 'format-all' : 'Format entire drive and setup a basic partition scheme.', + 'abort' : 'abort' } - return generic_select(options.values(), "Found partitions on the selected drive, (select by number) what do you want to do: ") + return generic_select(options.values(), "Found partitions on the selected drive, (select by number) what you want to do: ") def generic_select(options, input_text="Select one of the above by index or absolute value: ", sort=True): """ diff --git a/examples/guided.py b/examples/guided.py index 2b49d88a..0077b505 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -114,7 +114,10 @@ if archinstall.arguments['harddrive'].has_partitions(): archinstall.log(f" {partition} (Filesystem not supported)", fg='red') # We then ask what to do with the paritions. - if (option := archinstall.ask_for_disk_layout()) == 'keep-existing': + if (option := archinstall.ask_for_disk_layout()) == 'abort': + archinstall.log(f"Safely aborting the installation. No changes to the disk or system has been made.") + exit(1) + elif option == 'keep-existing': archinstall.arguments['harddrive'].keep_partitions = True archinstall.log(f" ** You will now select which partitions to use by selecting mount points (inside the installation). **") -- cgit v1.2.3-70-g09d2 From 94daa8b98b8dba328e45b11ba51b25963c5dcf76 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Mon, 8 Mar 2021 14:47:41 +0100 Subject: Added a abort message --- archinstall/lib/user_interaction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 01e3372c..2c8c30f8 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -91,7 +91,7 @@ def ask_for_disk_layout(): options = { 'keep-existing' : 'Keep existing partition layout and select which ones to use where.', 'format-all' : 'Format entire drive and setup a basic partition scheme.', - 'abort' : 'abort' + 'abort' : 'Abort the installation.' } return generic_select(options.values(), "Found partitions on the selected drive, (select by number) what you want to do: ") -- cgit v1.2.3-70-g09d2 From 476006abe8060235299fd3bddbfe69e597f7f988 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Mon, 8 Mar 2021 14:51:18 +0100 Subject: Fixed expected return value from ask_for_disk_layout(). I think I have to throw an eye on generic_select() and it's expected return value in general.. But that's later. --- archinstall/lib/user_interaction.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 2c8c30f8..5861fff3 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -94,7 +94,8 @@ def ask_for_disk_layout(): 'abort' : 'Abort the installation.' } - return generic_select(options.values(), "Found partitions on the selected drive, (select by number) what you want to do: ") + value = generic_select(options.values(), "Found partitions on the selected drive, (select by number) what you want to do: ") + return next((key for key, val in options.items() if val == value), None) def generic_select(options, input_text="Select one of the above by index or absolute value: ", sort=True): """ -- cgit v1.2.3-70-g09d2 From 6306de4bfee2d44ab1f362078a47d6d9a05835ef Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Mon, 8 Mar 2021 16:52:06 +0100 Subject: Reworked the guided partitioning logic to better match new expectations of flexability. Still some work to be done and features to be implemented, but the structure is taking place --- archinstall/lib/disk.py | 8 +++++++- archinstall/lib/luks.py | 28 +++++++++++++++++++++++----- archinstall/lib/user_interaction.py | 12 ++++++++++++ examples/guided.py | 16 +++++++++------- 4 files changed, 51 insertions(+), 13 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/disk.py b/archinstall/lib/disk.py index 2be26585..2f3d8233 100644 --- a/archinstall/lib/disk.py +++ b/archinstall/lib/disk.py @@ -196,28 +196,34 @@ class Partition(): if b'UUID' not in o: raise DiskError(f'Could not format {path} with {filesystem} because: {o}') self.filesystem = 'btrfs' + elif filesystem == 'vfat': o = b''.join(sys_command(f'/usr/bin/mkfs.vfat -F32 {path}')) if (b'mkfs.fat' not in o and b'mkfs.vfat' not in o) or b'command not found' in o: raise DiskError(f'Could not format {path} with {filesystem} because: {o}') self.filesystem = 'vfat' + elif filesystem == 'ext4': if (handle := sys_command(f'/usr/bin/mkfs.ext4 -F {path}')).exit_code != 0: raise DiskError(f'Could not format {path} with {filesystem} because: {b"".join(handle)}') self.filesystem = 'ext4' + elif filesystem == 'xfs': if (handle := sys_command(f'/usr/bin/mkfs.xfs -f {path}')).exit_code != 0: raise DiskError(f'Could not format {path} with {filesystem} because: {b"".join(handle)}') self.filesystem = 'xfs' + elif filesystem == 'f2fs': if (handle := sys_command(f'/usr/bin/mkfs.f2fs -f {path}')).exit_code != 0: raise DiskError(f'Could not format {path} with {filesystem} because: {b"".join(handle)}') self.filesystem = 'f2fs' + elif filesystem == 'crypto_LUKS': from .luks import luks2 encrypted_partition = luks2(self, None, None) encrypted_partition.format(path) self.filesystem = 'crypto_LUKS' + else: raise UnknownFilesystemFormat(f"Fileformat '{filesystem}' is not yet implemented.") return True @@ -327,7 +333,7 @@ class Filesystem(): if prep_mode == 'luks2': self.add_partition('primary', start='513MiB', end='100%') else: - self.add_partition('primary', start='513MiB', end='100%', format='ext4') + self.add_partition('primary', start='513MiB', end='100%', format=prep_mode) def add_partition(self, type, start, end, format=None): log(f'Adding partition to {self.blockdevice}', level=LOG_LEVELS.Info) diff --git a/archinstall/lib/luks.py b/archinstall/lib/luks.py index d62c2d4b..b98994ef 100644 --- a/archinstall/lib/luks.py +++ b/archinstall/lib/luks.py @@ -6,17 +6,28 @@ from .output import log, LOG_LEVELS from .storage import storage class luks2(): - def __init__(self, partition, mountpoint, password, *args, **kwargs): + def __init__(self, partition, mountpoint, password, key_file=None, *args, **kwargs): self.password = password self.partition = partition self.mountpoint = mountpoint self.args = args self.kwargs = kwargs + self.key_file = key_file self.filesystem = 'crypto_LUKS' def __enter__(self): - key_file = self.encrypt(self.partition, self.password, *self.args, **self.kwargs) - return self.unlock(self.partition, self.mountpoint, key_file) + if self.partition.allow_formatting: + self.key_file = self.encrypt(self.partition, *self.args, **self.kwargs) + else: + if not self.key_file: + self.key_file = f"/tmp/{os.path.basename(self.partition.path)}.disk_pw" # TODO: Make disk-pw-file randomly unique? + + if type(self.password) != bytes: self.password = bytes(self.password, 'UTF-8') + + with open(self.key_file, 'wb') as fh: + fh.write(self.password) + + return self.unlock(self.partition, self.mountpoint, self.key_file) def __exit__(self, *args, **kwargs): # TODO: https://stackoverflow.com/questions/28157929/how-to-safely-handle-an-exception-inside-a-context-manager @@ -24,13 +35,20 @@ class luks2(): raise args[1] return True - def encrypt(self, partition, password, key_size=512, hash_type='sha512', iter_time=10000, key_file=None): + def encrypt(self, partition, password=None, key_size=512, hash_type='sha512', iter_time=10000, key_file=None): # TODO: We should be able to integrate this into the main log some how. # Perhaps post-mortem? log(f'Encrypting {partition} (This might take a while)', level=LOG_LEVELS.Info) if not key_file: - key_file = f"/tmp/{os.path.basename(self.partition.path)}.disk_pw" # TODO: Make disk-pw-file randomly unique? + if self.key_file: + key_file = self.key_file + else: + key_file = f"/tmp/{os.path.basename(self.partition.path)}.disk_pw" # TODO: Make disk-pw-file randomly unique? + + if not password: + password = self.password + if type(password) != bytes: password = bytes(password, 'UTF-8') with open(key_file, 'wb') as fh: diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 5861fff3..7e7f5873 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -97,6 +97,18 @@ def ask_for_disk_layout(): value = generic_select(options.values(), "Found partitions on the selected drive, (select by number) what you want to do: ") return next((key for key, val in options.items() if val == value), None) +def ask_for_main_filesystem_format(): + options = { + 'btrfs' : 'btrfs', + 'ext4' : 'ext4', + 'xfs' : 'xfs', + 'f2fs' : 'f2fs', + 'vfat' : 'vfat' + } + + value = generic_select(options.values(), "Select your main partitions filesystem by number or free-text: ") + return next((key for key, val in options.items() if val == value), None) + def generic_select(options, input_text="Select one of the above by index or absolute value: ", sort=True): """ A generic select function that does not output anything diff --git a/examples/guided.py b/examples/guided.py index 7aec0611..b289016b 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -165,6 +165,7 @@ if archinstall.arguments['harddrive'].has_partitions(): archinstall.log('Using existing partition table reported above.') elif option == 'format-all': + archinstall.arguments['filesystem'] = archinstall.ask_for_main_filesystem_format() archinstall.arguments['harddrive'].keep_partitions = False # Get disk encryption password (or skip if blank) @@ -272,22 +273,23 @@ signal.signal(signal.SIGINT, original_sigint_handler) """ with archinstall.Filesystem(archinstall.arguments['harddrive'], archinstall.GPT) as fs: if archinstall.arguments['harddrive'].keep_partitions is False: - if disk_password: + if archinstall.arguments.get('!encryption-password', None): + # Set a temporary partition format to indicate that the partitions is encrypted. + # Later on, we'll mount it and put an actual filesystem inside this encrypted container. fs.use_entire_disk('luks2') else: - fs.use_entire_disk('ext4') + fs.use_entire_disk(archinstall.arguments.get('filesystem', 'ext4')) else: for partition in archinstall.arguments['harddrive']: if partition.allow_formatting: partition.format() - - exit(0) - if disk_password: + + if archinstall.arguments.get('!encryption-password', None): # First encrypt and unlock, then format the desired partition inside the encrypted part. # archinstall.luks2() encrypts the partition when entering the with context manager, and # unlocks the drive so that it can be used as a normal block-device within archinstall. - with archinstall.luks2(harddrive.partition[1], 'luksloop', disk_password) as unlocked_device: - unlocked_device.format('btrfs') + with archinstall.luks2(harddrive.partition[1], 'luksloop', archinstall.arguments.get('!encryption-password', None)) as unlocked_device: + unlocked_device.format(archinstall.arguments.get('filesystem', 'btrfs')) perform_installation(unlocked_device, harddrive.partition[0], -- cgit v1.2.3-70-g09d2