Skip to content

Commit

Permalink
Wait AC Power online before to perform the actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
comio committed Feb 20, 2018
1 parent 2c7cb39 commit 14c44e9
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
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
# AC is not online
[ $timeout -gt 0 ] && [ $timecount -ge $timeout ] && return 1
sleep 30s
timecount=$((timecount+1))
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"

## 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"

0 comments on commit 14c44e9

Please sign in to comment.