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 e60d197
Show file tree
Hide file tree
Showing 6 changed files with 85 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
26 changes: 26 additions & 0 deletions btrfsmaintenance-functions
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,29 @@ is_btrfs() {
[ "$FS" == "btrfs" ] && return 0
return 1
}

# function: wait_ac_power
# parameter: {timeout} {AC power online device}
#
# wait until ac power goes online
wait_ac_power() {
local timecount=0
local timeout=0
local ac_power_device=/sys/class/power_supply/AC/online

[ -n "$1" ] && [[ $1 =~ ^[0-9]+$ ]] && timeout=$1
[ -n "$2" ]&& ac_power_device="$2"

echo timeout $timeout ac_power_device $ac_power_device

if [ -f "${ac_power_device}" ]; then
while [ "$(cat ${ac_power_device})" -ne 1 ]; do
# AC is not online
[ $timeout -gt 0 ] && [ $timecount -ge $timeout ] && return 1
sleep 1s
timecount=$((timecount+1))
done
return 0
fi
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/AC/online"
#
# Path of AC status flag
BTRFS_AC_POWER_DEVICE="/sys/class/power_supply/AC/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 e60d197

Please sign in to comment.