New function invocation mechanism (README!)

The new mechanism is clearly explained in the comment. The rationale is the next implementation of hot swapping.

WATCH OUT! The semantics have changed. Previously, it was evident by looking at the invocation when a function had distro-based overrides; now, that needs to be looked into. Since this behavior is not very impactful, it's not been ported, for simplicity purposes.
This commit is contained in:
Saverio Miroddi 2021-04-27 13:10:03 +02:00
parent c7c0f530f8
commit ad7f09fbb4

View File

@ -107,43 +107,47 @@ c_udevadm_settle_timeout=10 # seconds
# HELPER FUNCTIONS ############################################################# # HELPER FUNCTIONS #############################################################
# Chooses a function and invokes it depending on the O/S distribution. # Invoke a function, with a primitive dynamic dispatch based on the distribution.
# #
# Example: # Format: `invoke "function" [--optional]`.
#
# A target function must exist, otherwise a error is raised, unless `--optional` is specified.
# `--optional` is useful when a step is specific to a single distribution, e.g. Debian's root password.
#
# Examples:
# #
# $ function install_jail_zfs_packages { :; } # $ function install_jail_zfs_packages { :; }
# $ function install_jail_zfs_packages_Debian { :; } # $ function install_jail_zfs_packages_Debian { :; }
# $ distro_dependent_invoke "install_jail_zfs_packages" # $ distro_dependent_invoke "install_jail_zfs_packages"
# #
# If the distribution is `Debian`, the second will be invoked, otherwise, the # If the distribution is `Debian`, the second will be invoked, otherwise, the first.
# first.
#
# If the function is invoked with `--noforce` as second parameter, and there is
# no matching function:
# #
# $ function update_zed_cache_Ubuntu { :; } # $ function update_zed_cache_Ubuntu { :; }
# $ distro_dependent_invoke "install_jail_zfs_packages" --noforce # $ distro_dependent_invoke "update_zed_cache" --optional
# #
# then nothing happens. Without `--noforce`, this invocation will cause an # If the distribution is `Debian`, nothing will happen.
# error.
# #
function distro_dependent_invoke { # $ function update_zed_cache_Ubuntu { :; }
local distro_specific_fx_name="$1_$v_linux_distribution" # $ distro_dependent_invoke "update_zed_cache"
#
# If the distribution is `Debian`, an error will be raised.
#
function invoke {
local base_fx_name=$1
local distro_specific_fx_name=$1_$v_linux_distribution
local invoke_option=${2:-} local invoke_option=${2:-}
if [[ ! $invoke_option =~ ^(|--noforce)$ ]]; then if [[ ! $invoke_option =~ ^(|--optional)$ ]]; then
>&2 echo "Invalid distro_dependent_invoke() option: $invoke_option" >&2 echo "Invalid invoke() option: $invoke_option"
exit 1 exit 1
fi fi
# Invoke it regardless when it's not optional.
if declare -f "$distro_specific_fx_name" > /dev/null; then if declare -f "$distro_specific_fx_name" > /dev/null; then
"$distro_specific_fx_name" "$distro_specific_fx_name"
else elif declare -f "$base_fx_name" > /dev/null || [[ ! $invoke_option == "--optional" ]]; then
if ! declare -f "$1" > /dev/null && [[ $invoke_option == "--noforce" ]]; then "$base_fx_name"
: # do nothing
else
"$1"
fi
fi fi
} }
@ -1502,59 +1506,59 @@ if [[ $# -ne 0 ]]; then
display_help_and_exit display_help_and_exit
fi fi
activate_debug invoke "activate_debug"
set_distribution_data invoke "set_distribution_data"
distro_dependent_invoke "store_os_distro_information" invoke "store_os_distro_information"
store_running_processes invoke "store_running_processes"
check_prerequisites invoke "check_prerequisites"
display_intro_banner invoke "display_intro_banner"
check_system_memory invoke "check_system_memory"
save_disks_log invoke "save_disks_log"
find_suitable_disks invoke "find_suitable_disks"
distro_dependent_invoke "set_zfs_ppa_requirement" invoke "set_zfs_ppa_requirement"
register_exit_hook invoke "register_exit_hook"
create_passphrase_named_pipe invoke "create_passphrase_named_pipe"
select_disks invoke "select_disks"
select_pools_raid_type invoke "select_pools_raid_type"
distro_dependent_invoke "ask_root_password" --noforce invoke "ask_root_password" --optional
ask_encryption invoke "ask_encryption"
ask_boot_partition_size invoke "ask_boot_partition_size"
ask_swap_size invoke "ask_swap_size"
ask_free_tail_space invoke "ask_free_tail_space"
ask_rpool_name invoke "ask_rpool_name"
ask_pool_create_options invoke "ask_pool_create_options"
distro_dependent_invoke "install_host_packages" invoke "install_host_packages"
setup_partitions invoke "setup_partitions"
if [[ "${ZFS_OS_INSTALLATION_SCRIPT:-}" == "" ]]; then if [[ "${ZFS_OS_INSTALLATION_SCRIPT:-}" == "" ]]; then
# Includes the O/S extra configuration, if necessary (network, root pwd, etc.) # Includes the O/S extra configuration, if necessary (network, root pwd, etc.)
distro_dependent_invoke "install_operating_system" invoke "install_operating_system"
create_pools invoke "create_pools"
create_swap_volume invoke "create_swap_volume"
copy_zpool_cache invoke "copy_zpool_cache"
sync_os_temp_installation_dir_to_rpool invoke "sync_os_temp_installation_dir_to_rpool"
remove_temp_partition_and_expand_rpool invoke "remove_temp_partition_and_expand_rpool"
else else
create_pools invoke "create_pools"
create_swap_volume invoke "create_swap_volume"
copy_zpool_cache invoke "copy_zpool_cache"
remove_temp_partition_and_expand_rpool invoke "remove_temp_partition_and_expand_rpool"
custom_install_operating_system invoke "custom_install_operating_system"
fi fi
prepare_jail invoke "prepare_jail"
distro_dependent_invoke "install_jail_zfs_packages" invoke "install_jail_zfs_packages"
prepare_efi_partition invoke "prepare_efi_partition"
configure_and_update_grub invoke "configure_and_update_grub"
sync_efi_partitions invoke "sync_efi_partitions"
update_initramfs invoke "update_initramfs"
fix_filesystem_mount_ordering invoke "fix_filesystem_mount_ordering"
configure_pools_trimming invoke "configure_pools_trimming"
configure_remaining_settings invoke "configure_remaining_settings"
prepare_for_system_exit invoke "prepare_for_system_exit"
display_exit_banner invoke "display_exit_banner"