Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait AC Power #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions btrfs-balance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ fi
LOGIDENTIFIER='btrfs-balance'
. $(dirname $(realpath $0))/btrfsmaintenance-functions

if [ "$BTRFS_BALANCE_WAIT_AC_POWER" = "true" ]; then
if ! wait_ac_power $BTRFS_AC_POWER_TIMEOUT; then
if [ "$BTRFS_AC_POWER_ACTION_ON_NO_AC_POWER" == "abort" ]; then
echo "No AC Power. Abort"
exit 1
fi
fi
fi

{
evaluate_auto_mountpoint BTRFS_BALANCE_MOUNTPOINTS
OIFS="$IFS"
Expand Down
10 changes: 10 additions & 0 deletions btrfs-defrag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ if [ -f /etc/default/btrfsmaintenance ] ; then
fi

LOGIDENTIFIER='btrfs-defrag'
$(dirname $(realpath $0))/btrfsmaintenance-functions

if [ "$BTRFS_BALANCE_WAIT_AC_POWER" = "true" ]; then
if ! wait_ac_power $BTRFS_AC_POWER_TIMEOUT; then
if [ "$BTRFS_AC_POWER_ACTION_ON_NO_AC_POWER" == "abort" ]; then
echo "No AC Power. Abort"
exit 1
fi
fi
fi

{
OIFS="$IFS"
Expand Down
10 changes: 10 additions & 0 deletions btrfs-scrub.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@ if [ -f /etc/default/btrfsmaintenance ] ; then
. /etc/default/btrfsmaintenance
fi


LOGIDENTIFIER='btrfs-scrub'
. $(dirname $(realpath $0))/btrfsmaintenance-functions

if [ "$BTRFS_BALANCE_WAIT_AC_POWER" = "true" ]; then
if ! wait_ac_power $BTRFS_AC_POWER_TIMEOUT; then
if [ "$BTRFS_AC_POWER_ACTION_ON_NO_AC_POWER" == "abort" ]; then
echo "No AC Power. Abort"
exit 1
fi
fi
fi

readonly=
if [ "$BTRFS_SCRUB_READ_ONLY" = "true" ]; then
readonly=-r
Expand Down
9 changes: 9 additions & 0 deletions btrfs-trim.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ fi
LOGIDENTIFIER='btrfs-trim'
. $(dirname $(realpath $0))/btrfsmaintenance-functions

if [ "$BTRFS_BALANCE_WAIT_AC_POWER" = "true" ]; then
if ! wait_ac_power $BTRFS_AC_POWER_TIMEOUT; then
if [ "$BTRFS_AC_POWER_ACTION_ON_NO_AC_POWER" == "abort" ]; then
echo "No AC Power. Abort"
exit 1
fi
fi
fi

{
evaluate_auto_mountpoint BTRFS_TRIM_MOUNTPOINTS
OIFS="$IFS"
Expand Down
82 changes: 82 additions & 0 deletions btrfsmaintenance-functions
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,85 @@ is_btrfs() {
[ "$FS" == "btrfs" ] && return 0
return 1
}

# function: check_power_status
# parameter:
#
# Detect AC power or not in a portable way
# Exit 0 if on AC power, 1 if not and 255 if we don't know how to work it out
# Code from Openrc/scripts/on_ac_power
check_power_status() {
local state=255
if [ -f /proc/acpi/ac_adapter/*/state ]; then
cat /proc/acpi/ac_adapter/*/state | while read line; do
case "$line" in
"state:"*"off-line") state=128; break;;
esac
done
elif [ -f /sys/class/power_supply/*/online ]; then
cat /sys/class/power_supply/*/online | while read line; do
[ "${line}" = 0 ] && state=128 && break;
done
elif [ -f /proc/pmu/info ]; then
cat /proc/pmu/info | while read line; do
case "$line" in
"AC Power"*": 0") state=128; break;;
esac
done
elif command -v envstat >/dev/null 2>&1; then
# NetBSD has envstat
envstat -d acpiacad0 2>/dev/null | while read line; do
case "$line" in
"connected:"*"OFF") state=128; break;;
esac
done
elif sysctl -q hw.acpi.acline >/dev/null 2>/dev/null; then
case $(sysctl -n hw.acpi.acline) in
0) state=1;;
*) state=0;;
esac
else
return 255
fi
return [ $state != 128 ]
}

# function: wait_ac_power
# parameter: {timeout} {AC power online device}
#
# wait until ac power goes online
wait_ac_power() {
local timecount=0
local timeout=${1:-0}
local check=0

while true; do
check_power_status
case $? in
0)
return 0
;;
1)
;;
255)
case "$BTRFS_AC_POWER_ACTION_ON_UNKNOWN_STATUS" in
assume_no_ac_power)
return 1
;;
assume_ac_power)
return 0
;;
*)
# Continue
;;
esac
;;
esac
# AC is not online
[ $timeout -gt 0 ] && [ $timecount -ge $timeout ] && return 1
sleep 1s
timecount=$((timecount+1))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If my math is correct, this will wait the 30 times more than the config value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was 1s but i changed just before to commit ("1s is too short" I said).

done

return 0
}
50 changes: 50 additions & 0 deletions sysconfig.btrfsmaintenance
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@
# 'btrfs-scrub' etc.
BTRFS_LOG_OUTPUT="stdout"

## Path: System/File systems/btrfs
## Type: number
## Default: 3600
#
# Second to wait the AC Power before to continue anyway
BTRFS_AC_POWER_TIMEOUT=3600

## Path: System/File systems/btrfs
## Type: string(retry,assume_no_ac_power,assume_ac_power)
## Default: "retry"
#
# Default action when ac power status is unknown
BTRFS_AC_POWER_ACTION_ON_UNKNOWN_STATUS="retry"

## Path: System/File systems/btrfs
## Type: string(continue,abort)
## Default: "abort"
#
# Default action when ac power status is unknown
BTRFS_AC_POWER_ACTION_ON_NO_AC_POWER="abort"

## Path: System/File systems/btrfs
## Type: string
## Default: ""
Expand All @@ -31,6 +52,14 @@ BTRFS_DEFRAG_PERIOD="none"
# Minimal file size to consider for defragmentation
BTRFS_DEFRAG_MIN_SIZE="+1M"

## Path: System/File systems/btrfs
## Type: boolean
## Default: "false"
#
# Wait AC Power before defrag
BTRFS_DEFRAG_WAIT_AC_POWER="false"


## Path: System/File systems/btrfs
## Type: string
## Default: "/"
Expand Down Expand Up @@ -72,6 +101,13 @@ BTRFS_BALANCE_DUSAGE="1 5 10 20 30 40 50"
# this will increase IO load on the system.
BTRFS_BALANCE_MUSAGE="1 5 10 20 30"

## Path: System/File systems/btrfs
## Type: boolean
## Default: "false"
#
# Wait AC Power before balance
BTRFS_BALANCE_WAIT_AC_POWER="false"

## Path: System/File systems/btrfs
## Type: string
## Default: "/"
Expand Down Expand Up @@ -104,6 +140,13 @@ BTRFS_SCRUB_PRIORITY="idle"
# Do read-only scrub and don't try to repair anything.
BTRFS_SCRUB_READ_ONLY="false"

## Path: System/File systems/btrfs
## Type: boolean
## Default: "false"
#
# Wait AC Power before scrub
BTRFS_SCRUB_WAIT_AC_POWER="false"

## Path: System/File systems/btrfs
## Description: Configuration for periodic fstrim
## Type: string(none,daily,weekly,monthly)
Expand All @@ -124,3 +167,10 @@ BTRFS_TRIM_PERIOD="none"
# (Colon separated paths)
# The special word/mountpoint "auto" will evaluate all mounted btrfs filesystems at runtime
BTRFS_TRIM_MOUNTPOINTS="/"

## Path: System/File systems/btrfs
## Type: boolean
## Default: "false"
#
# Wait AC Power before trim
BTRFS_TRIM_WAIT_AC_POWER="false"