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 1 commit
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
4 changes: 4 additions & 0 deletions btrfs-balance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ if [ -f /etc/default/btrfsmaintenance ] ; then
. /etc/default/btrfsmaintenance
fi

if [ "$BTRFS_BALANCE_WAIT_AC_POWER" = "true" ]; then
wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE
fi

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

Expand Down
4 changes: 4 additions & 0 deletions btrfs-defrag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ if [ -f /etc/default/btrfsmaintenance ] ; then
. /etc/default/btrfsmaintenance
fi

if [ "$BTRFS_DEFRAG_WAIT_AC_POWER" = "true" ]; then
wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE
fi

LOGIDENTIFIER='btrfs-defrag'

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

if [ "$BTRFS_SCRUB_WAIT_AC_POWER" = "true" ]; then
wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE
fi

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

Expand Down
4 changes: 4 additions & 0 deletions btrfs-trim.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ if [ -f /etc/default/btrfsmaintenance ] ; then
. /etc/default/btrfsmaintenance
fi

if [ "$BTRFS_TRIM__WAIT_AC_POWER" = "true" ]; then
wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE
fi

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

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

# function: check_power_status
# parameter: paths to check the status
#
# Return true if at least one path is online
check_power_status() {
local status=$(cat "$@" 2>/dev/null)
[ -z "$status" ] && return 0
case "$status" in
*1*)
return 0
;;
*)
return 1
;;
esac
}

# 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

while ! check_power_status "$@"; do
Copy link
Owner

Choose a reason for hiding this comment

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

The timeout value will be propagated to check_power_status, so we need a shift

# AC is not online
[ $timeout -gt 0 ] && [ $timecount -ge $timeout ] && return 1
sleep 30s
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
}
43 changes: 43 additions & 0 deletions sysconfig.btrfsmaintenance
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
# '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
## Default: "/sys/class/power_supply/*/online"
#
# Path of AC status flag
BTRFS_AC_POWER_DEVICE="/sys/class/power_supply/*/online"
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like it will do the trick for laptops. I mentioned upower in another comment, because I think that /sys/class/power_supply/*/online will return true even when a server is on UPS battery. (eg: NUT or apcupsd). IIRC it provides a generic interface, and alternatives are upsmon (NUT) and apcaccess -p TONBATT (apcupsd)

Copy link
Owner

Choose a reason for hiding this comment

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

We may want to revisit that later, with support for UPS, but for now I'm fine with the more relaxed check. Meanwhile I verified the path exists on my other systems, so I think there's nothing more left to discuss.

Copy link
Contributor Author

@comio comio Feb 20, 2018

Choose a reason for hiding this comment

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

I had no time to dedicate to search a portable solution yet. @sten0, can you provide some example of upower, upsmon, apsacces (with outputs). I have a very basic system (my home NAS) and I cannot checks all these tools.


## Path: System/File systems/btrfs
## Type: string
## Default: ""
Expand All @@ -31,6 +45,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 +94,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 +133,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 +160,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"