From 14c44e9d6e9ccfe6be5bcae42fd4cfa80480f35e Mon Sep 17 00:00:00 2001 From: Luigi 'Comio' Mantellini Date: Tue, 20 Feb 2018 08:51:58 +0100 Subject: [PATCH 1/2] Wait AC Power online before to perform the actions. --- btrfs-balance.sh | 4 ++++ btrfs-defrag.sh | 4 ++++ btrfs-scrub.sh | 4 ++++ btrfs-trim.sh | 4 ++++ btrfsmaintenance-functions | 35 +++++++++++++++++++++++++++++++ sysconfig.btrfsmaintenance | 43 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 94 insertions(+) diff --git a/btrfs-balance.sh b/btrfs-balance.sh index 4e90934..689e6d5 100755 --- a/btrfs-balance.sh +++ b/btrfs-balance.sh @@ -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 diff --git a/btrfs-defrag.sh b/btrfs-defrag.sh index 284abc8..68be48d 100755 --- a/btrfs-defrag.sh +++ b/btrfs-defrag.sh @@ -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' { diff --git a/btrfs-scrub.sh b/btrfs-scrub.sh index d38f3c9..557c87a 100755 --- a/btrfs-scrub.sh +++ b/btrfs-scrub.sh @@ -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 diff --git a/btrfs-trim.sh b/btrfs-trim.sh index 253cee1..fe94d96 100755 --- a/btrfs-trim.sh +++ b/btrfs-trim.sh @@ -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 diff --git a/btrfsmaintenance-functions b/btrfsmaintenance-functions index 50a3e4f..097e3b9 100644 --- a/btrfsmaintenance-functions +++ b/btrfsmaintenance-functions @@ -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 +} diff --git a/sysconfig.btrfsmaintenance b/sysconfig.btrfsmaintenance index 06155c6..1d259ac 100644 --- a/sysconfig.btrfsmaintenance +++ b/sysconfig.btrfsmaintenance @@ -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: "" @@ -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: "/" @@ -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: "/" @@ -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) @@ -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" From edc384f3e1760c2d579f971c1266bfb27b4cebb0 Mon Sep 17 00:00:00 2001 From: Luigi 'Comio' Mantellini Date: Tue, 6 Mar 2018 17:20:17 +0100 Subject: [PATCH 2/2] Implement logic on unkwnown power status. --- btrfs-balance.sh | 13 +++++-- btrfs-defrag.sh | 14 +++++-- btrfs-scrub.sh | 12 ++++-- btrfs-trim.sh | 13 +++++-- btrfsmaintenance-functions | 77 ++++++++++++++++++++++++++++++-------- sysconfig.btrfsmaintenance | 15 ++++++-- 6 files changed, 110 insertions(+), 34 deletions(-) diff --git a/btrfs-balance.sh b/btrfs-balance.sh index 689e6d5..53c7992 100755 --- a/btrfs-balance.sh +++ b/btrfs-balance.sh @@ -14,13 +14,18 @@ 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 +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" diff --git a/btrfs-defrag.sh b/btrfs-defrag.sh index 68be48d..e36f108 100755 --- a/btrfs-defrag.sh +++ b/btrfs-defrag.sh @@ -14,11 +14,17 @@ 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' +$(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" diff --git a/btrfs-scrub.sh b/btrfs-scrub.sh index 557c87a..d793c95 100755 --- a/btrfs-scrub.sh +++ b/btrfs-scrub.sh @@ -14,13 +14,19 @@ 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 +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 diff --git a/btrfs-trim.sh b/btrfs-trim.sh index fe94d96..0c7e16c 100755 --- a/btrfs-trim.sh +++ b/btrfs-trim.sh @@ -14,13 +14,18 @@ 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 +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" diff --git a/btrfsmaintenance-functions b/btrfsmaintenance-functions index 097e3b9..3373670 100644 --- a/btrfsmaintenance-functions +++ b/btrfsmaintenance-functions @@ -78,20 +78,45 @@ is_btrfs() { } # function: check_power_status -# parameter: paths to check the status +# parameter: # -# Return true if at least one path is online +# 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 status=$(cat "$@" 2>/dev/null) - [ -z "$status" ] && return 0 - case "$status" in - *1*) - return 0 - ;; - *) - return 1 - ;; - esac + 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 @@ -100,12 +125,34 @@ check_power_status() { # wait until ac power goes online wait_ac_power() { local timecount=0 - local timeout=$1 + local timeout=${1:-0} + local check=0 - while ! check_power_status "$@"; do + 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 30s + sleep 1s timecount=$((timecount+1)) done diff --git a/sysconfig.btrfsmaintenance b/sysconfig.btrfsmaintenance index 1d259ac..022802d 100644 --- a/sysconfig.btrfsmaintenance +++ b/sysconfig.btrfsmaintenance @@ -14,11 +14,18 @@ BTRFS_LOG_OUTPUT="stdout" BTRFS_AC_POWER_TIMEOUT=3600 ## Path: System/File systems/btrfs -## Type: string -## Default: "/sys/class/power_supply/*/online" +## 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" # -# Path of AC status flag -BTRFS_AC_POWER_DEVICE="/sys/class/power_supply/*/online" +# Default action when ac power status is unknown +BTRFS_AC_POWER_ACTION_ON_NO_AC_POWER="abort" ## Path: System/File systems/btrfs ## Type: string