Skip to content

Commit 14c44e9

Browse files
committed
Wait AC Power online before to perform the actions.
1 parent 2c7cb39 commit 14c44e9

6 files changed

Lines changed: 94 additions & 0 deletions

File tree

btrfs-balance.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ if [ -f /etc/default/btrfsmaintenance ] ; then
1414
. /etc/default/btrfsmaintenance
1515
fi
1616

17+
if [ "$BTRFS_BALANCE_WAIT_AC_POWER" = "true" ]; then
18+
wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE
19+
fi
20+
1721
LOGIDENTIFIER='btrfs-balance'
1822
. $(dirname $(realpath $0))/btrfsmaintenance-functions
1923

btrfs-defrag.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ if [ -f /etc/default/btrfsmaintenance ] ; then
1414
. /etc/default/btrfsmaintenance
1515
fi
1616

17+
if [ "$BTRFS_DEFRAG_WAIT_AC_POWER" = "true" ]; then
18+
wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE
19+
fi
20+
1721
LOGIDENTIFIER='btrfs-defrag'
1822

1923
{

btrfs-scrub.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ if [ -f /etc/default/btrfsmaintenance ] ; then
1414
. /etc/default/btrfsmaintenance
1515
fi
1616

17+
if [ "$BTRFS_SCRUB_WAIT_AC_POWER" = "true" ]; then
18+
wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE
19+
fi
20+
1721
LOGIDENTIFIER='btrfs-scrub'
1822
. $(dirname $(realpath $0))/btrfsmaintenance-functions
1923

btrfs-trim.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ if [ -f /etc/default/btrfsmaintenance ] ; then
1414
. /etc/default/btrfsmaintenance
1515
fi
1616

17+
if [ "$BTRFS_TRIM__WAIT_AC_POWER" = "true" ]; then
18+
wait_ac_power $BTRFS_AC_POWER_TIMEOUT $BTRFS_AC_POWER_DEVICE
19+
fi
20+
1721
LOGIDENTIFIER='btrfs-trim'
1822
. $(dirname $(realpath $0))/btrfsmaintenance-functions
1923

btrfsmaintenance-functions

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,38 @@ is_btrfs() {
7676
[ "$FS" == "btrfs" ] && return 0
7777
return 1
7878
}
79+
80+
# function: check_power_status
81+
# parameter: paths to check the status
82+
#
83+
# Return true if at least one path is online
84+
check_power_status() {
85+
local status=$(cat "$@" 2>/dev/null)
86+
[ -z "$status" ] && return 0
87+
case "$status" in
88+
*1*)
89+
return 0
90+
;;
91+
*)
92+
return 1
93+
;;
94+
esac
95+
}
96+
97+
# function: wait_ac_power
98+
# parameter: {timeout} {AC power online device}
99+
#
100+
# wait until ac power goes online
101+
wait_ac_power() {
102+
local timecount=0
103+
local timeout=$1
104+
105+
while ! check_power_status "$@"; do
106+
# AC is not online
107+
[ $timeout -gt 0 ] && [ $timecount -ge $timeout ] && return 1
108+
sleep 30s
109+
timecount=$((timecount+1))
110+
done
111+
112+
return 0
113+
}

sysconfig.btrfsmaintenance

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66
# 'btrfs-scrub' etc.
77
BTRFS_LOG_OUTPUT="stdout"
88

9+
## Path: System/File systems/btrfs
10+
## Type: number
11+
## Default: 3600
12+
#
13+
# Second to wait the AC Power before to continue anyway
14+
BTRFS_AC_POWER_TIMEOUT=3600
15+
16+
## Path: System/File systems/btrfs
17+
## Type: string
18+
## Default: "/sys/class/power_supply/*/online"
19+
#
20+
# Path of AC status flag
21+
BTRFS_AC_POWER_DEVICE="/sys/class/power_supply/*/online"
22+
923
## Path: System/File systems/btrfs
1024
## Type: string
1125
## Default: ""
@@ -31,6 +45,14 @@ BTRFS_DEFRAG_PERIOD="none"
3145
# Minimal file size to consider for defragmentation
3246
BTRFS_DEFRAG_MIN_SIZE="+1M"
3347

48+
## Path: System/File systems/btrfs
49+
## Type: boolean
50+
## Default: "false"
51+
#
52+
# Wait AC Power before defrag
53+
BTRFS_DEFRAG_WAIT_AC_POWER="false"
54+
55+
3456
## Path: System/File systems/btrfs
3557
## Type: string
3658
## Default: "/"
@@ -72,6 +94,13 @@ BTRFS_BALANCE_DUSAGE="1 5 10 20 30 40 50"
7294
# this will increase IO load on the system.
7395
BTRFS_BALANCE_MUSAGE="1 5 10 20 30"
7496

97+
## Path: System/File systems/btrfs
98+
## Type: boolean
99+
## Default: "false"
100+
#
101+
# Wait AC Power before balance
102+
BTRFS_BALANCE_WAIT_AC_POWER="false"
103+
75104
## Path: System/File systems/btrfs
76105
## Type: string
77106
## Default: "/"
@@ -104,6 +133,13 @@ BTRFS_SCRUB_PRIORITY="idle"
104133
# Do read-only scrub and don't try to repair anything.
105134
BTRFS_SCRUB_READ_ONLY="false"
106135

136+
## Path: System/File systems/btrfs
137+
## Type: boolean
138+
## Default: "false"
139+
#
140+
# Wait AC Power before scrub
141+
BTRFS_SCRUB_WAIT_AC_POWER="false"
142+
107143
## Path: System/File systems/btrfs
108144
## Description: Configuration for periodic fstrim
109145
## Type: string(none,daily,weekly,monthly)
@@ -124,3 +160,10 @@ BTRFS_TRIM_PERIOD="none"
124160
# (Colon separated paths)
125161
# The special word/mountpoint "auto" will evaluate all mounted btrfs filesystems at runtime
126162
BTRFS_TRIM_MOUNTPOINTS="/"
163+
164+
## Path: System/File systems/btrfs
165+
## Type: boolean
166+
## Default: "false"
167+
#
168+
# Wait AC Power before trim
169+
BTRFS_TRIM_WAIT_AC_POWER="false"

0 commit comments

Comments
 (0)