spit and polish

This commit is contained in:
nagumii 2025-02-14 13:20:24 +02:00
parent d7b822c7b1
commit 104c8cf656
2 changed files with 62 additions and 47 deletions

View file

@ -242,7 +242,7 @@ utilities() {
utilities_choice=$(dialog --no-collapse --cancel-label "Back" --default-item "$utilities_choice" --item-help --menu "$title" 21 50 4 \
1 "View system info" "" \
2 "Set system time" "" \
3 "Kernel modules" "" \
3 "Kernel module manager" "" \
4 "Re-run first-boot script" "" \
5 "Run USB configuration tool" "" \
6 "Run OEM luckfox-config" "" \

View file

@ -14,9 +14,13 @@ Options are:
-a List active kernel modules
-x "kernelmodule" "enable" Set kernel module status (enable/disable)
-z "kernelmodule" "blacklist" Set kernel module blacklist status (blacklist/un-blacklist)
-y Get list of blacklisted kernel modules
EOF
)
module_dir="/lib/modules/5.10.160"
title="Kernel Module Manager"
module_switch() {
if [ $2 = "enable" ]; then
modprobe $1 # Load the selected module
@ -39,7 +43,9 @@ module_switch() {
}
module_blacklist() {
if [ $2 = "blacklist" ]; then
if [ $1 = "list" ]; then
echo -e "$(ls "$module_dir"/*.blacklisted 2>/dev/null | sed -E 's|.*/([^.]*)\..*|\1|' | awk '{for(i=1;i<=NF;i++) {printf "\033[4m%s\033[0m", $i; if(i<NF) printf " ";} printf " ";} END {if (NR == 0) print "\033[4mnone\033[0m"}')"
elif [ $2 = "blacklist" ]; then
mv "$module_dir/$1.ko" "$module_dir/$1.ko.blacklisted" > /dev/null 2>&1
module_switch $1 disable > /dev/null 2>&1 # unload and disable the module
echo -e "$1 is blacklisted. A reboot is sometimes required for blacklisting to fully take effect."
@ -47,12 +53,12 @@ module_blacklist() {
mv "$module_dir/$1.ko.blacklisted" "$module_dir/$1.ko" > /dev/null 2>&1
echo -e "$1 is un-blacklisted. A reboot is sometimes required for un-blacklisting to fully take effect."
else
echo "Invalid argument \"$2\".\n$help"
echo "Invalid argument \"$1 $2\".\n$help"
fi
}
# Parse options
while getopts ":hbax:z:" opt; do
while getopts ":hbax:z:y" opt; do
case ${opt} in
h) # Option -l (set lora radio)
echo "$help"
@ -70,21 +76,23 @@ while getopts ":hbax:z:" opt; do
z) # Option -z (Set kernel module blacklist status (blacklist/un-blacklist))
echo "$(module_blacklist $2 $3)"
;;
y) # Option -y (Get list of blacklisted kernel modules)
echo "$(module_blacklist "list")"
;;
esac
done
if [ -n "$1" ]; then # if there are arguments, don't proceed to menu
exit
fi
module_dir="/lib/modules/5.10.160"
dialog --no-collapse --colors --title "$title" --yesno "\
Kernel modules are loadable pieces of code that extend a Linux kernel's functionality without requiring a reboot. Common examples include device drivers, file systems, or system calls.\n\
\n\
This tool will allow you to manage kernel modules and add pre-compiled modules to Foxbuntu.\n\
\n\
Boot modules: \Zu$(femto-utils.sh -R "$(femto-kernel-modules.sh -b)")\Zu\Zn\n\
Active modules: \Zu$(femto-utils.sh -R "$(femto-kernel-modules.sh -a)")\Zu\Zn\n\
Boot modules: $(femto-utils.sh -R "$(femto-kernel-modules.sh -b)")\n\
Blacklisted modules: $(femto-utils.sh -R "$(femto-kernel-modules.sh -y)")\n\
Active modules: $(femto-utils.sh -R "$(femto-kernel-modules.sh -a)")\n\
\n\
Continue?
" 0 0
@ -92,7 +100,7 @@ Continue?
load_modules() {
dialog --infobox "Loading kernel module menu...\n\nExpected load time: 35 seconds." 6 45
modules=()
modules=("Module name" "L/D Description" "" "")
# Create a list of modules (filename minus the .ko)
for module in $(ls $module_dir/*.ko*); do
@ -108,49 +116,56 @@ modules_changed="false"
while true; do
[ $modules_changed = "true" ] && load_modules
# Create the menu options
selected_module=$(dialog --no-collapse --cancel-label "Return" --ok-label "Open" --title "Kernel Modules" --no-shadow --default-item "$selected_module" --menu "Module name Loaded/Boot? Description" 42 103 8 "${modules[@]}" 3>&1 1>&2 2>&3)
[ $? -eq 1 ] && break # Exit the loop if the user selects "Cancel" or closes the dialog
selected_module=$(dialog --no-collapse --cancel-label "Return" --ok-label "Open" --title "$title" --no-shadow --default-item "$selected_module" --help-button --menu "" 42 103 8 "${modules[@]}" 3>&1 1>&2 2>&3)
exit_status=$?
if [ $exit_status -eq 1 ]; then # "Return" button
break
elif [ $exit_status -eq 2 ]; then # "Help" button
dialog --no-collapse --colors --title "Kernel Module Manager Help" --msgbox "In the kernel module manager menu,\nL=Module is loaded\nB=Module is set to start at boot.\n\nIf module is blacklisted, \`BLACKLISTED\` will show in the description." 0 0
else
[ "$selected_module" = "" ] || [ "$selected_module" = "Module name" ] && continue # do nothing if no valid module name is selected
modules_changed="false"
# Get the full modinfo for the module and process it for dialog
while true; do
modinfo_output=$(modinfo $selected_module 2>/dev/null | sed ':a;N;$!ba;s/\n/\\n/g') # add \n to module info
modules_changed="false"
# Get the full modinfo for the module and process it for dialog
while true; do
modinfo_output=$(modinfo $selected_module 2>/dev/null | sed ':a;N;$!ba;s/\n/\\n/g') # add \n to module info
if [ ! -e "$module_dir/$selected_module.ko" ]; then
module_info="\Z1\ZuMODULE BLACKLISTED!\Zn\n\n$selected_module has been blacklisted and will not load on boot.\
$(lsmod | grep -q "^$selected_module " && echo "\n\n$selected_module is currently \Z4\Zuloaded\Zn!\n\
if [ ! -e "$module_dir/$selected_module.ko" ]; then
module_info="\Z1\ZuMODULE BLACKLISTED!\Zn\n\n$selected_module has been blacklisted and will not load on boot.\
$(lsmod | grep -q "^$selected_module " && echo "\n\n$selected_module is currently \Z4\Zuloaded\Zn!\n\
Some modules require reboot for blacklist to take effect if they're in use when blacklisted.")"
else
module_info="Module is currently $(lsmod | grep -q "^$selected_module " && echo "\Z4loaded\Zn" || echo "\Z1unloaded\Zn") and is set $(femto-utils.sh -R "$(femto-kernel-modules.sh -b | sed 's/\x1b\[[0-9;]*m//g' | grep -qw "$selected_module" && echo "\Z4to load at boot\Zn" || echo "\Z1not to load at boot\Zn. It may load automatically if needed")").\n\
else
module_info="Module is currently $(lsmod | grep -q "^$selected_module " && echo "\Z4loaded\Zn" || echo "\Z1unloaded\Zn") and is set $(femto-utils.sh -R "$(femto-kernel-modules.sh -b | sed 's/\x1b\[[0-9;]*m//g' | grep -qw "$selected_module" && echo "\Z4to load at boot\Zn" || echo "\Z1not to load at boot\Zn. It may load automatically if needed")").\n\
\n\
Full module info:\n$modinfo_output\n\nNote: Dependencies are loaded/unloaded automatically."
fi
dialog --colors --yes-label "Return" --no-label "Disable" --extra-button --extra-label "Enable" --help-button --help-label "Blacklist" --title "$selected_module" --yesno "$module_info" 0 0
exit_status=$? # This line checks the exit status of the dialog command
if [ $exit_status -eq 0 ]; then # "Back" button
break
elif [ $exit_status -eq 3 ]; then # "Enable" button
dialog --colors --title "$selected_module" --msgbox "$(femto-utils.sh -R "$(module_switch $selected_module enable)")" 0 0
modules_changed="true"
elif [ $exit_status -eq 1 ]; then # "Disable" button
dialog --colors --title "$selected_module" --msgbox "$(femto-utils.sh -R "$(module_switch $selected_module disable)")" 0 0
modules_changed="true"
elif [ $exit_status -eq 2 ]; then # "Blacklist" button
dialog --no-collapse --colors --title "$selected_module" --yes-label "Return" --no-label "Un-blacklist" --extra-button --extra-label "Blacklist" --yesno "$selected_module is currently $([ -e "$module_dir/$selected_module.ko" ] && echo "\Z4\Zunot blacklisted\Zn" || echo "\Z1\Zublacklisted\Zn").\n\nBlacklisting prevents a kernel module from loading.\n\nWould you like to blacklist $selected_module?" 0 0
exit_status=$?
if [ $exit_status -eq 0 ]; then
continue
elif [ $exit_status -eq 3 ]; then # blacklist
dialog --colors --title "$selected_module" --msgbox "$(femto-utils.sh -R "$(module_blacklist $selected_module blacklist)")" 8 50
modules_changed="true"
else # blacklist
dialog --colors --title "$selected_module" --msgbox "$(femto-utils.sh -R "$(module_blacklist $selected_module un-blacklist)")" 8 50
modules_changed="true"
fi
fi
done
dialog --colors --yes-label "Return" --no-label "Disable" --extra-button --extra-label "Enable" --help-button --help-label "Blacklist" --title "$selected_module" --yesno "$module_info" 0 0
exit_status=$? # This line checks the exit status of the dialog command
if [ $exit_status -eq 0 ]; then # "Back" button
break
elif [ $exit_status -eq 3 ]; then # "Enable" button
dialog --colors --title "$selected_module" --msgbox "$(femto-utils.sh -R "$(module_switch $selected_module enable)")" 0 0
modules_changed="true"
elif [ $exit_status -eq 1 ]; then # "Disable" button
dialog --colors --title "$selected_module" --msgbox "$(femto-utils.sh -R "$(module_switch $selected_module disable)")" 0 0
modules_changed="true"
elif [ $exit_status -eq 2 ]; then # "Blacklist" button
dialog --no-collapse --colors --title "$selected_module" --yes-label "Return" --no-label "Un-blacklist" --extra-button --extra-label "Blacklist" --yesno "$selected_module is currently $([ -e "$module_dir/$selected_module.ko" ] && echo "\Z4\Zunot blacklisted\Zn" || echo "\Z1\Zublacklisted\Zn").\n\nBlacklisting prevents a kernel module from loading.\n\nWould you like to blacklist $selected_module?" 0 0
exit_status=$?
if [ $exit_status -eq 0 ]; then
continue
elif [ $exit_status -eq 3 ]; then # blacklist
dialog --colors --title "$selected_module" --msgbox "$(femto-utils.sh -R "$(module_blacklist $selected_module blacklist)")" 8 50
modules_changed="true"
else # blacklist
dialog --colors --title "$selected_module" --msgbox "$(femto-utils.sh -R "$(module_blacklist $selected_module un-blacklist)")" 8 50
modules_changed="true"
fi
fi
done
fi
done
exit 0