Improve preflight check

Run all actions at once to avoid multiple password prompts.
Move followups and results to the preflight_check function.
This commit is contained in:
the-sane 2021-01-24 12:44:49 -05:00 committed by GitHub
parent 693edd56fa
commit 1840983431
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -517,26 +517,31 @@ sanitize() {
# Check if setting vm.max_map_count was successful # Check if setting vm.max_map_count was successful
mapcount_confirm() { mapcount_confirm() {
if [ "$(cat /proc/sys/vm/max_map_count)" -lt 16777216 ]; then if [ "$(cat /proc/sys/vm/max_map_count)" -lt 16777216 ]; then
message warning "As far as this Helper can detect, vm.max_map_count\nwas not successfully configured on your system.\n\nYou will most likely experience crashes." preflight_results+=("WARNING: As far as this Helper can detect, vm.max_map_count\nwas not successfully configured on your system.\nYou will most likely experience crashes.")
fi fi
} }
# Sets vm.max_map_count for the current session only # Sets vm.max_map_count for the current session only
mapcount_once() { mapcount_once() {
pkexec sh -c 'sysctl -w vm.max_map_count=16777216' preflight_actions+=('sysctl -w vm.max_map_count=16777216')
mapcount_confirm preflight_results+=("vm.max_map_count was changed until the next boot.")
preflight_followup+=("mapcount_confirm")
} }
# Set vm.max_map_count # Set vm.max_map_count
mapcount_set() { mapcount_set() {
if [ -d "/etc/sysctl.d" ]; then if [ -d "/etc/sysctl.d" ]; then
pkexec sh -c 'echo "vm.max_map_count = 16777216" >> /etc/sysctl.d/20-max_map_count.conf && sysctl --system' # Newer versions of sysctl
message info "The vm.max_map_count configuration has been appended to:\n/etc/sysctl.d/20-max_map_count.conf" preflight_actions+=('printf "\n# Added by LUG-Helper:\nvm.max_map_count = 16777216\n" >> /etc/sysctl.d/20-max_map_count.conf && sysctl --system')
preflight_results+=("The vm.max_map_count configuration has been appended to:\n/etc/sysctl.d/20-max_map_count.conf")
else else
pkexec sh -c 'echo "vm.max_map_count = 16777216" >> /etc/sysctl.conf && sysctl -p' # Older versions of sysctl
message info "The vm.max_map_count configuration has been appended to:\n/etc/sysctl.conf" preflight_actions+=('printf "\n# Added by LUG-Helper:\nvm.max_map_count = 16777216" >> /etc/sysctl.conf && sysctl -p')
preflight_results+=("The vm.max_map_count configuration has been appended to:\n/etc/sysctl.conf")
fi fi
mapcount_confirm
# Verify that the setting took effect
preflight_followup+=("mapcount_confirm")
} }
# Check vm.max_map_count for the correct setting # Check vm.max_map_count for the correct setting
@ -549,7 +554,7 @@ mapcount_check() {
# Was it supposed to have been set by sysctl? # Was it supposed to have been set by sysctl?
preflight_fail+=("vm.max_map_count is configured to at least 16777216 but the setting has not been loaded by your system.") preflight_fail+=("vm.max_map_count is configured to at least 16777216 but the setting has not been loaded by your system.")
# Add the function that will be called to change the configuration # Add the function that will be called to change the configuration
preflight_actions+=("mapcount_once") preflight_action_funcs+=("mapcount_once")
# Add info for manually changing the setting # Add info for manually changing the setting
preflight_manual+=("To change vm.max_map_count until the next reboot, run:\nsudo sysctl -w vm.max_map_count=16777216") preflight_manual+=("To change vm.max_map_count until the next reboot, run:\nsudo sysctl -w vm.max_map_count=16777216")
@ -557,7 +562,7 @@ mapcount_check() {
# The setting should be changed # The setting should be changed
preflight_fail+=("vm.max_map_count should be set to at least 16777216\nto give the game access to more than 8GB of memory\nand avoid crashes in areas with lots of geometry.") preflight_fail+=("vm.max_map_count should be set to at least 16777216\nto give the game access to more than 8GB of memory\nand avoid crashes in areas with lots of geometry.")
# Add the function that will be called to change the configuration # Add the function that will be called to change the configuration
preflight_actions+=("mapcount_set") preflight_action_funcs+=("mapcount_set")
# Add info for manually changing the setting # Add info for manually changing the setting
if [ -d "/etc/sysctl.d" ]; then if [ -d "/etc/sysctl.d" ]; then
@ -577,7 +582,7 @@ mapcount_check() {
# Check if setting the open file descriptors limit was successful # Check if setting the open file descriptors limit was successful
filelimit_confirm() { filelimit_confirm() {
if [ "$(ulimit -Hn)" -lt 524288 ]; then if [ "$(ulimit -Hn)" -lt 524288 ]; then
message warning "As far as this Helper can detect, the open files limit\nwas not successfully configured on your system.\nYou may experience crashes.\n\nWe recommend manually configuring this limit to at least 524288." preflight_results+=("WARNING: As far as this Helper can detect, the open files limit\nwas not successfully configured on your system.\nYou may experience crashes.")
fi fi
} }
@ -586,21 +591,20 @@ filelimit_set() {
if [ -f "/etc/systemd/system.conf" ]; then if [ -f "/etc/systemd/system.conf" ]; then
# Using systemd # Using systemd
# Append to the file # Append to the file
pkexec sh -c 'echo "DefaultLimitNOFILE=524288" >> /etc/systemd/system.conf && systemctl daemon-reexec' preflight_actions+=('printf "\n# Added by LUG-Helper:\nDefaultLimitNOFILE=524288\n" >> /etc/systemd/system.conf && systemctl daemon-reexec')
message info "The open files limit configuration has been appended to:\n/etc/systemd/system.conf" preflight_results+=("The open files limit configuration has been appended to:\n/etc/systemd/system.conf")
elif [ -f "/etc/security/limits.conf" ]; then elif [ -f "/etc/security/limits.conf" ]; then
# Using limits.conf # Using limits.conf
# Insert before the last line in the file # Insert before the last line in the file
pkexec sh -c 'sed -i "\$i* hard nofile 524288" /etc/security/limits.conf' preflight_actions+=('sed -i "\$i#Added by LUG-Helper:" /etc/security/limits.conf; sed -i "\$i* hard nofile 524288" /etc/security/limits.conf')
message info "The open files limit configuration has been appended to:\n/etc/security/limits.conf" preflight_results+=("The open files limit configuration has been appended to:\n/etc/security/limits.conf")
else else
# Don't know what method to use # Don't know what method to use
message warning "This Helper is unable to detect the correct method of setting\nthe open file descriptors limit on your system.\n\nWe recommend manually configuring this limit to at least 524288." preflight_results+=("This Helper is unable to detect the correct method of setting\nthe open file descriptors limit on your system.\n\nWe recommend manually configuring this limit to at least 524288.")
return 0
fi fi
# Verify that setting the limit was successful # Verify that setting the limit was successful
filelimit_confirm preflight_followup+=("filelimit_confirm")
} }
# Check the open file descriptors limit # Check the open file descriptors limit
@ -615,7 +619,7 @@ filelimit_check() {
# The file limit should be changed # The file limit should be changed
preflight_fail+=("Your hard open file descriptors limit should be set\nto at least 524288.") preflight_fail+=("Your hard open file descriptors limit should be set\nto at least 524288.")
# Add the function that will be called to change the configuration # Add the function that will be called to change the configuration
preflight_actions+=("filelimit_set") preflight_action_funcs+=("filelimit_set")
# Add info for manually changing the settings # Add info for manually changing the settings
if [ -f "/etc/systemd/system.conf" ]; then if [ -f "/etc/systemd/system.conf" ]; then
@ -1016,8 +1020,11 @@ preflight_check() {
# Initialize variables # Initialize variables
unset preflight_pass unset preflight_pass
unset preflight_fail unset preflight_fail
unset preflight_action_funcs
unset preflight_actions unset preflight_actions
unset preflight_results
unset preflight_manual unset preflight_manual
unset preflight_followup
# Call the optimization functions to perform the checks # Call the optimization functions to perform the checks
mapcount_check mapcount_check
@ -1037,20 +1044,19 @@ preflight_check() {
if [ "${#preflight_fail[@]}" -gt 0 ]; then if [ "${#preflight_fail[@]}" -gt 0 ]; then
preflight_fail_string="Failed Checks:" preflight_fail_string="Failed Checks:"
for (( i=0; i<"${#preflight_fail[@]}"; i++ )); do for (( i=0; i<"${#preflight_fail[@]}"; i++ )); do
if [ "$i" -gt 0 ]; then if [ "$i" -eq 0 ]; then
# Add extra newlines between sections but not for the first item
preflight_fail_string="$preflight_fail_string\n"
fi
preflight_fail_string="$preflight_fail_string\n- ${preflight_fail[i]//\\n/\\n }" preflight_fail_string="$preflight_fail_string\n- ${preflight_fail[i]//\\n/\\n }"
else
preflight_fail_string="$preflight_fail_string\n\n- ${preflight_fail[i]//\\n/\\n }"
fi
done done
fi fi
unset preflight_manual_string
for (( i=0; i<"${#preflight_manual[@]}"; i++ )); do for (( i=0; i<"${#preflight_manual[@]}"; i++ )); do
if [ "$i" -gt 0 ]; then if [ "$i" -eq 0 ]; then
# Add extra newlines between sections but not for the first item preflight_manual_string="${preflight_manual[i]}"
preflight_manual_string="$preflight_manual_string\n\n" else
preflight_manual_string="$preflight_manual_string\n\n${preflight_manual[i]}"
fi fi
preflight_manual_string="$preflight_manual_string${preflight_manual[i]}"
done done
# Display the results of the preflight check # Display the results of the preflight check
@ -1058,10 +1064,40 @@ preflight_check() {
message info "Preflight Check Complete\n\nYour system is optimized for Star Citizen!\n\n$preflight_pass_string" message info "Preflight Check Complete\n\nYour system is optimized for Star Citizen!\n\n$preflight_pass_string"
else else
if message question "$preflight_pass_string$preflight_fail_string\n\nWould you like this Helper to fix the issues for you?"; then if message question "$preflight_pass_string$preflight_fail_string\n\nWould you like this Helper to fix the issues for you?"; then
# Call functions to fix any issues found # Call functions to build fixes for any issues found
for (( i=0; i<"${#preflight_actions[@]}"; i++ )); do for (( i=0; i<"${#preflight_action_funcs[@]}"; i++ )); do
${preflight_actions[i]} ${preflight_action_funcs[i]}
done done
# Populate a string of actions to be executed
for (( i=0; i<"${#preflight_actions[@]}"; i++ )); do
if [ "$i" -eq 0 ]; then
preflight_actions_string="${preflight_actions[i]}"
else
preflight_actions_string="$preflight_actions_string; ${preflight_actions[i]}"
fi
done
# Execute the actions set by the functions
if [ ! -z "$preflight_actions_string" ]; then
pkexec sh -c "$preflight_actions_string"
fi
# Call any followup functions
for (( i=0; i<"${#preflight_followup[@]}"; i++ )); do
${preflight_followup[i]}
done
# Populate the results string
for (( i=0; i<"${#preflight_results[@]}"; i++ )); do
if [ "$i" -eq 0 ]; then
preflight_results_string="${preflight_results[i]}"
else
preflight_results_string="$preflight_results_string\n\n${preflight_results[i]}"
fi
done
# Display the results
message info "$preflight_results_string"
else else
# Show the user the manual configuration options # Show the user the manual configuration options
message info "$preflight_manual_string" message info "$preflight_manual_string"