Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/modules/network/config
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# intentionally left blank
# Network module setup
# shellcheck=disable

# Use disable power save for wifi module
[ -n "$BASE_NETWORK_DISABLE_PWRSAVE" ] || BASE_NETWORK_DISABLE_PWRSAVE=yes

# Type of power save rclocal/service/udev
# rclocal - backwards compatibility, runs via rc.local
# service - will add an systemd.service to enable or disable behavior
# on reboots
# udev - creates a udev rules that should affect all wifi devices.

[ -n "$BASE_NETWORK_PWRSAVE_TYPE" ] || BASE_NETWORK_PWRSAVE_TYPE=udev
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#### Disable wifi power_save
####
#### Written by Stephan Wendel aka KwadFan <me@stephanwe.de>
#### Copyright 2022
#### https://github.com/mainsail-crew/MainsailOS
####
#### This File is distributed under GPLv3
####
#### Note: This is based on:
#### https://www.intel.com/content/www/us/en/support/articles/000006168/boards-and-kits.html

[Unit]
Description=Disable power management for wlan0
After=network.target

[Service]
Type=oneshot
StandardOutput=tty
ExecStart=/usr/local/bin/pwrsave off

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ACTION=="add", \
SUBSYSTEM=="net", \
KERNEL=="wlan*" \
RUN+="/usr/sbin/iw %k set power_save off"
90 changes: 90 additions & 0 deletions src/modules/network/filesystem/usr/local/bin/pwrsave
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/bash
#### Disable wifi power_save
####
#### Written by Stephan Wendel aka KwadFan <me@stephanwe.de>
#### Copyright 2022
#### https://github.com/mainsail-crew/MainsailOS
####
#### This File is distributed under GPLv3
####
#### Note: This is based on:
#### https://www.intel.com/content/www/us/en/support/articles/000006168/boards-and-kits.html


## Error handling
set -eou pipefail

## Debug Mode
#set -x

### Message func
function help_msg {
echo -e "Usage:\n"
echo -e "\tpwrsave [ on | off ]"
echo -e "\t\ton\tEnables Power Management of 'wlan0'"
echo -e "\t\toff\tDisables Power Management of 'wlan0'\n"
exit 1
}

function has_wifi {
LC_ALL=C iwconfig wlan0 &> /dev/null && echo "0" || echo "1"
}

function check_wifi_present {
# make sure to exit if command missing
if [ -z "$(command -v iwconfig)" ]; then
echo -e "Command 'iwconfig' not found ... [EXITING]"
exit 1
fi
if [ "$(has_wifi)" != "0" ]; then
echo -e "[ \e[33mWARN\e[0m ] No WiFi hardware present ... [SKIPPED]"
exit 0
fi
}

function disable_pwr_save {
iwconfig wlan0 power off
echo -e "[ \e[32mOK\e[0m ] Disabled Power Management for wlan0"
}


function enable_pwr_save {
iwconfig wlan0 power on
echo -e "[ \e[32mOK\e[0m ] Enabled Power Management for wlan0"
}


### MAIN
function main {
local arg
if [ "$(id -u)" != "0" ]; then
echo -e "\n$(basename "${0}"): This script needs root priviledges!\n"
exit 1
fi
if [ "${#}" == "0" ]; then
echo -e "$(basename "${0}"): No argument set!"
help_msg
fi
if [ "${#}" -gt 1 ]; then
echo -e "$(basename "${0}"): Too many arguments set!"
help_msg
fi
arg="${1}"
case "${arg}" in
"on")
check_wifi_present
enable_pwr_save
;;
"off")
check_wifi_present
disable_pwr_save
;;
?|*)
echo -e "$(basename "${0}"): Unknown argument '${arg}' !"
help_msg
;;
esac
}

main "${@}"
exit 0
105 changes: 105 additions & 0 deletions src/modules/network/filesystem/usr/local/bin/pwrsave-udev
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/bin/bash
#### Disable wifi power_save
####
#### Written by Stephan Wendel aka KwadFan <me@stephanwe.de>
#### Copyright 2022
#### https://github.com/mainsail-crew/MainsailOS
####
#### This File is distributed under GPLv3
####
#### Note: This is based on:
#### https://www.intel.com/content/www/us/en/support/articles/000006168/boards-and-kits.html


## Error handling
set -eou pipefail

## Debug Mode
#set -x

### Message func
function help_msg {
echo -e "Usage:\n"
echo -e "\tpwrsave-udev [ on | off | create ]"
echo -e "\t\ton\tEnables Power Management via udev rule"
echo -e "\t\toff\tDisables Power Management via udev rule"
echo -e "\t\tcreate\tCreate Power Management udev rule\n"
exit 1
}


### Check rule exist
function check_rule {
if [ ! -f /etc/udev/rules.d/070-wifi-powersave.rules ]; then
echo -e "[ \e[31mERROR\e[0m ] Udev Rule for WiFi Powermanagement not found!"
help_msg
exit 1
fi
}

function disable_pwr_save {
sed -i 's/on/off/' /etc/udev/rules.d/070-wifi-powersave.rules
echo -e "[ \e[32mOK\e[0m ] Disabled Power Management"
}


function enable_pwr_save {
sed -i 's/off/on/' /etc/udev/rules.d/070-wifi-powersave.rules
echo -e "[ \e[32mOK\e[0m ] Enabled Power Management"
}

function create_rule {
if [ -f /etc/udev/rules.d/070-wifi-powersave.rules ]; then
echo -e "[ \e[33mSKIPPED\e[0m ] Udev rule already exists!"
exit 0
fi

cat << EOF > /etc/udev/rules.d/070-wifi-powersave.rules
ACTION=="add", \
SUBSYSTEM=="net", \
KERNEL=="wlan*" \
RUN+="/usr/sbin/iw %k set power_save off"
EOF
echo -e "[ \e[32mOK\e[0m ] Created Udev rule ... \n"
echo -e "Please 'reboot' to take changes effect.\n"
}



### MAIN
function main {
local arg
if [ "$(id -u)" != "0" ]; then
echo -e "\n$(basename "${0}"): This script needs root priviledges!\n"
exit 1
fi
if [ "${#}" == "0" ]; then
echo -e "$(basename "${0}"): No argument set!"
help_msg
fi
if [ "${#}" -gt 1 ]; then
echo -e "$(basename "${0}"): Too many arguments set!"
help_msg
fi
arg="${1}"
if [ "${arg}" == "create" ]; then
create_rule
exit 0
fi
check_rule
case "${arg}" in
"on")
enable_pwr_save
;;
"off")
disable_pwr_save
;;
?|*)
echo -e "$(basename "${0}"): Unknown argument '${arg}' !"
help_msg
;;
esac
}

main "${@}"
exit 0
36 changes: 30 additions & 6 deletions src/modules/network/start_chroot_script
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,35 @@ sed -i 's@exit 0@@' /etc/rc.local
echo '/sbin/iptables -t mangle -I POSTROUTING 1 -o wlan0 -p udp --dport 123 -j TOS --set-tos 0x00' >> /etc/rc.local
echo 'exit 0' >> /etc/rc.local

# Turn off wlan power management
sed -i 's@exit 0@@' /etc/rc.local
cat <<'EOT' >> /etc/rc.local
echo "Disabling power management for wlan0"
iw dev wlan0 set power_save off
EOT
# Install powersave option
if [ "$BASE_NETWORK_DISABLE_PWRSAVE" == "yes" ]; then

# Copy pwrsave script
unpack filesystem/usr/local/bin /usr/local/bin root

# Use rc.local
if [ "$BASE_NETWORK_PWRSAVE_TYPE" == "rclocal" ]; then
echo_green "Modifying /etc/rc.local ..."
sed -i 's@exit 0@@' /etc/rc.local
(echo "# Disable WiFi Power Management"; \
echo 'echo "Disabling power management for wlan0 ..."' ; \
echo "/usr/local/bin/pwrsave off"; echo "exit 0") >> /etc/rc.local
fi
# Use service
if [ "$BASE_NETWORK_PWRSAVE_TYPE" == "service" ]; then
echo_green "Installing disable-wifi-pwr-mgmt service ..."
unpack filesystem/etc/systemd/system /etc/systemd/system root
systemctl_if_exists enable disable-wifi-pwr-mgmt.service
fi
# Use udev rule
if [ "$BASE_NETWORK_PWRSAVE_TYPE" == "udev" ]; then
echo_green "Installing WiFi Power Management udev rule ..."
unpack filesystem/etc/udev/rules.d /etc/udev/rules.d root
fi
# strip out unneeded script, depending on choose
if [ "$BASE_NETWORK_PWRSAVE_TYPE" != "udev" ]; then
rm -f /usr/local/bin/pwrsave-udev
else
rm -f /usr/local/bin/pwrsave
fi
fi