From 7c89cf0a86361ad478729fa606a66e2c75a8d6eb Mon Sep 17 00:00:00 2001 From: Luigi 'Comio' Mantellini Date: Wed, 31 Jan 2018 10:17:26 +0100 Subject: [PATCH] Make generic the AC power online check. --- btrfs-balance.sh | 2 +- btrfs-defrag.sh | 2 +- btrfs-scrub.sh | 2 +- btrfs-trim.sh | 2 +- btrfsmaintenance-functions | 39 +++++++++++++++++++++++--------------- sysconfig.btrfsmaintenance | 4 ++-- 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/btrfs-balance.sh b/btrfs-balance.sh index c4019d2..42f0e77 100755 --- a/btrfs-balance.sh +++ b/btrfs-balance.sh @@ -15,7 +15,7 @@ if [ -f /etc/default/btrfsmaintenance ] ; then fi if [ "$BTRFS_BALANCE_WAIT_AC_POWER" = "true" ]; then - wait_ac_power $BTRFS_AC_POWER_TIMEOUT "$BTRFS_AC_POWER_DEVICE" + wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE fi LOGIDENTIFIER='btrfs-balance' diff --git a/btrfs-defrag.sh b/btrfs-defrag.sh index c6a78ad..2262a76 100755 --- a/btrfs-defrag.sh +++ b/btrfs-defrag.sh @@ -15,7 +15,7 @@ if [ -f /etc/default/btrfsmaintenance ] ; then fi if [ "$BTRFS_DEFRAG_WAIT_AC_POWER" = "true" ]; then - wait_ac_power $BTRFS_AC_POWER_TIMEOUT "$BTRFS_AC_POWER_DEVICE" + wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE fi LOGIDENTIFIER='btrfs-defrag' diff --git a/btrfs-scrub.sh b/btrfs-scrub.sh index 25399a5..a87c3df 100755 --- a/btrfs-scrub.sh +++ b/btrfs-scrub.sh @@ -15,7 +15,7 @@ if [ -f /etc/default/btrfsmaintenance ] ; then fi if [ "$BTRFS_SCRUB_WAIT_AC_POWER" = "true" ]; then - wait_ac_power $BTRFS_AC_POWER_TIMEOUT "$BTRFS_AC_POWER_DEVICE" + wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE fi LOGIDENTIFIER='btrfs-scrub' diff --git a/btrfs-trim.sh b/btrfs-trim.sh index f6faced..bc3b567 100755 --- a/btrfs-trim.sh +++ b/btrfs-trim.sh @@ -15,7 +15,7 @@ if [ -f /etc/default/btrfsmaintenance ] ; then fi if [ "$BTRFS_TRIM__WAIT_AC_POWER" = "true" ]; then - wait_ac_power $BTRFS_AC_POWER_TIMEOUT "$BTRFS_AC_POWER_DEVICE" + wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE fi LOGIDENTIFIER='btrfs-trim' diff --git a/btrfsmaintenance-functions b/btrfsmaintenance-functions index b3ea826..55d175c 100644 --- a/btrfsmaintenance-functions +++ b/btrfsmaintenance-functions @@ -67,28 +67,37 @@ check_balance_running() { return 0 } +# 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=0 - local ac_power_device=/sys/class/power_supply/AC/online + local timeout=$1 - [ -n "$1" ] && [[ $1 =~ ^[0-9]+$ ]] && timeout=$1 - [ -n "$2" ]&& ac_power_device="$2" + while ! check_power_status "$@"; do + # AC is not online + [ $timeout -gt 0 ] && [ $timecount -ge $timeout ] && return 1 + sleep 1s + timecount=$((timecount+1)) + done - 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 } diff --git a/sysconfig.btrfsmaintenance b/sysconfig.btrfsmaintenance index 871ab4e..1d259ac 100644 --- a/sysconfig.btrfsmaintenance +++ b/sysconfig.btrfsmaintenance @@ -15,10 +15,10 @@ BTRFS_AC_POWER_TIMEOUT=3600 ## Path: System/File systems/btrfs ## Type: string -## Default: "/sys/class/power_supply/AC/online" +## Default: "/sys/class/power_supply/*/online" # # Path of AC status flag -BTRFS_AC_POWER_DEVICE="/sys/class/power_supply/AC/online" +BTRFS_AC_POWER_DEVICE="/sys/class/power_supply/*/online" ## Path: System/File systems/btrfs ## Type: string