Skip to contentSkip to conversation

mdadm: rework mdcheck systemd service logic

PATCH In review Martin Wilck 2 participants 11 messages Sep 12, 2025 – Sep 16, 2025 MD RAID

lore
Share & tools
lore
#0 lore raw

cover mdadm: rework mdcheck systemd service logic

This Patch set changes the logic of the "mdcheck" tool and the related systemd services mdcheck_start.service and mdcheck_continue.service.
The set and related discussion are also posted as GitHub PR [1].

The set is meant to be applied on top of PR#189 [2], which has already been merged in the current main branch on GitHub.

[1] https://github.com/md-raid-utilities/mdadm/pull/190
[2] https://github.com/md-raid-utilities/mdadm/pull/189

The current behavior is like this:

* mdcheck without arguments starts a RAID check on all arrays on the system, starting at position 0. This is started from mdcheck_start.service, started by a systemd timer once a month.
* mdcheck --continue looks for files /var/lib/mdcheck/MD_UUID_$UUID, reads the start position from them, and starts a check from that position on the array with the respective UUID. This is started from a systemd timer every night.

In either case, mdcheck won't do anything if the kernel is already running a sync_action on a given array. The check runs for a given period of time (default 6h) and saves the last position in the MD_UUID file, to be taken up when mdcheck --continue is called next time. When the entire array has been checked, the MD_UUID_ file is deleted. When all checks are finished, mdcheck_continue.timer is stopped, to be restarted when mdcheck_start.timer expires next time.

Before the recent commit 8aa4ea9 ("systemd: start mdcheck_continue.timer before mdcheck_start.timer"), this could lead to a race condition when the check for a given array didn't complete throughout the month. mdcheck_start.service would start and reset the check position to 0 before mdcheck_continue.service could pick up at the last saved position. Commit 8aa4ea9 works around this by starting mdcheck_continue.service a few minutes before mdcheck_start.timer.

Yet the general problem still exists: both services trigger checks on the kernel's part which they can only passively monitor. So if a user plays with the timer settings (which he is in his rights to do), another similar race might happen.

This patch set changes the behavior as follows:

Only mdcheck_continue.service actually starts and stops kernel-based sync actions. This service will continue at the saved start position if an MD_UUID* file exists, or start a new check at position 0 otherwise. Starting at 0 can be inhibited by creating a file /var/lib/mdcheck/Checked_$UUID. These files will be created by mdcheck when it finishes checking a given array. Thus future invocations of mdcheck_continue.service will not restart the check on this array.

mdcheck_start.service runs mdcheck --restart, which simply removes all Checked_* markers from /var/lib/mdcheck, so that the next invocation of mdcheck_continue.service will start new checks on all arrays which don't have already running checks.

The general behavior of the systemd timers and services is like before, but the mentioned race condition is avoided, even if the user modifies the timer settings arbitrarily.

This set slightly changes the behavior of the mdcheck script. Without --continue, it will still start checks on all array, but unlike before it will skip arrays for with a Checked_ marker exists. To avoid that, run mdcheck --restart before mdcheck.

More details in the commit description of the patch "mdcheck: simplify start / continue logic and add "--restart" logic".

Martin Wilck (4):
mdcheck: loop over sync_action files in sysfs
mdcheck: replace deprecated "$[cnt+1]" syntax
mdcheck: simplify start / continue logic and add "--restart" logic
mdcheck: log to stderr from systemd units

 misc/mdcheck                     | 105 ++++++++++++++++++++-----------
 systemd/mdcheck_continue.service |   6 +-
 systemd/mdcheck_start.service    |   3 +-
 systemd/mdcheck_start.timer      |   2 +-
 4 files changed, 75 insertions(+), 41 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/4 mdcheck: loop over sync_action files in sysfs

The way mdcheck is currently written, it loops over /dev/md?*, which will contain RAID partitions and arrays that don't support sync operations, such as RAID0. This is inefficient and makes the script difficult to trace.

Instead, loop over the sync_action files which actually matter for checking.

---
 misc/mdcheck | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
misc/mdcheck +14 -14
index 398a1ea..e654c5c 100644
@@ -31,12 +31,13 @@
 # To support '--continue', arrays are identified by UUID and the 'sync_completed'
 # value is stored  in /var/lib/mdcheck/$UUID
 
-# convert a /dev/md name into /sys/.../md equivalent
-sysname() {
-	set `ls -lLd $1`
-	maj=${5%,}
-	min=$6
-	readlink -f /sys/dev/block/$maj:$min
+# get device name from sysfs
+devname() {
+    local dev
+    [[ -f "$1/uevent" ]] && \
+	    dev=$(. "$1/uevent" && echo -n "$DEVNAME")
+    [[ "$dev" && -b "/dev/$dev" ]] || return 1
+    echo -n "/dev/$dev"
 }
 
 args=$(getopt -o hcd: -l help,continue,duration: -n mdcheck -- "$@")
@@ -100,21 +101,20 @@ mkdir -p /var/lib/mdcheck
 find /var/lib/mdcheck -name "MD_UUID*" -type f -mtime +180 -exec rm {} \;
 
 # Now look at each md device.
-for dev in /dev/md?*
+for sync_act in /sys/block/*/md/sync_action
 do
-	[ -e "$dev" ] || continue
-	sys=`sysname $dev`
-	if [ ! -f "$sys/md/sync_action" ]
-	then # cannot check this array
-		continue
-	fi
-	if [ "`cat $sys/md/sync_action`" != 'idle' ]
+	[ -e "$sync_act" ] || continue
+	if [ "`cat $sync_act`" != 'idle' ]
 	then # This array is busy
 		continue
 	fi
 
+	sys=${sync_act%/md/*}
+	dev=$(devname "$sys") || continue
 	mdadm --detail --export "$dev" | grep '^MD_UUID=' > $tmp || continue
 	source $tmp
+	[[ "$MD_UUID" ]] || continue
+
 	fl="/var/lib/mdcheck/MD_UUID_$MD_UUID"
 	if [ -z "$cont" ]
 	then
Signature · 1 line
2.51.0
#2 lore raw

PATCH 2/4 mdcheck: replace deprecated "$[cnt+1]" syntax

In reply to Martin Wilck

This syntax used to be marked as deprecated [1]. In current bash man pages, it isn't even mentioned any more. Use the POSIX compatible syntax "$((X+=1))" instead [2, 3].

[1] https://stackoverflow.com/questions/41081417/difference-between-a-b-and-a-b-in-bash
[2] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04
[3] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html#tag_17_01_02_01

---
 misc/mdcheck | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
misc/mdcheck +1 -1
index e654c5c..5ea26cd 100644
@@ -129,7 +129,7 @@ do
 		logger -p daemon.info mdcheck continue checking $dev from $start
 	fi
 
-	cnt=$[cnt+1]
+	: "$((cnt+=1))"
 	eval MD_${cnt}_fl=\$fl
 	eval MD_${cnt}_sys=\$sys
 	eval MD_${cnt}_dev=\$dev
Signature · 1 line
2.51.0
#3 lore raw

PATCH 3/4 mdcheck: simplify start / continue logic and add "--restart" logic

In reply to Martin Wilck

The current logic of "mdcheck" is susceptible to races when multiple mdcheck instances run simultaneously, as checks can be initiated from both "mdcheck_start.service" and "mdcheck_continue.service".

This patch changes the logic as follows.

* When mdcheck has finished checking a RAID array, it will create a marker /var/lib/mdcheckChecked_$UUID.
* A new option --restart is introduced. mdcheck --restart removes all /var/lib/mdcheck/Checked_* markers.
This is called from mdcheck_start.service, which is typically started by a timer in large time intervals (default once per month).
* mdcheck --continue works as it used to. It continues previously started checks (where the /var/lib/mdcheck/MD_UUID_$UUID file is present and contains a start position) for the check.
This usage is *not recommended any more*.
* mdcheck with no arguments is like --continue, but it also starts new checks for all arrays for which no check has previously been started, *except* for arrays for which a marker /var/lib/mdcheck/Checked_$UUID exists. mdcheck_continue.service calls mdcheck this way. It is called in short time intervals, by default once per day.
* Combining --restart and --continue is an error.

This way, the only systemd service that actually triggers a kernel-level RAID check is mdcheck_continue.service, which avoids races.

When all checks have finished, mdcheck_continue.service is no-op. When mdcheck_start.service runs, the checks re re-enabled and will be started from 0 by the next mdcheck_continue.service invocation.

---
 misc/mdcheck                     | 61 ++++++++++++++++++++++----------
 systemd/mdcheck_continue.service |  6 ++--
 systemd/mdcheck_start.service    |  3 +-
 systemd/mdcheck_start.timer      |  2 +-
 4 files changed, 48 insertions(+), 24 deletions(-)
misc/mdcheck +42 -19
index 5ea26cd..df4fd3b 100644
@@ -21,15 +21,21 @@
 #
 # It supports a 'time budget' such that any incomplete 'check'
 # will be checkpointed when that time has expired.
-# A subsequent invocation can allow the 'check' to continue.
+# A subsequent invocation will allow the 'check' to continue.
 #
 # Options are:
-#   --continue    Don't start new checks, only continue old ones.
+#   --continue    Don't start new checks, only continue previously started ones.
+#   --restart:    Enable restarting the array checks
 #   --duration    This is passed to "date --date=$duration" to find out
 #		  when to finish
 #
-# To support '--continue', arrays are identified by UUID and the 'sync_completed'
-# value is stored  in /var/lib/mdcheck/$UUID
+# Arrays are identified by UUID and the 'sync_completed' value is stored
+# in /var/lib/mdcheck/MD_UUID_$UUID. If this file exists on startup of
+# the script, the check will continue where the previous check left off.
+# After the check completes, /var/lib/mdcheck/Checked_$UUID will be created.
+# Another full check will be started after this file is removed.
+# Use "mdcheck --restart" to remove these markers and re-enable checking
+# all arrays.
 
 # get device name from sysfs
 devname() {
@@ -40,24 +46,29 @@ devname() {
     echo -n "/dev/$dev"
 }
 
-args=$(getopt -o hcd: -l help,continue,duration: -n mdcheck -- "$@")
+args=$(getopt -o hcrd: -l help,continue,restart,duration: -n mdcheck -- "$@")
 rv=$?
 if [ $rv -ne 0 ]; then exit $rv; fi
 
 eval set -- $args
 
 cont=
+restart=
 endtime=
 while [ " $1" != " --" ]
 do
     case $1 in
 	--help )
-		echo >&2 'Usage: mdcheck [--continue] [--duration time-offset]'
+		echo >&2 'Usage: mdcheck [--restart|--continue] [--duration time-offset]'
 		echo >&2 '  time-offset must be understood by "date --date"'
 		exit 0
 		;;
-	--continue ) cont=yes ;;
-	--duration ) shift; dur=$1
+	--continue )
+		cont=yes ;;
+	--restart )
+		restart=yes ;;
+	--duration )
+		shift; dur=$1
 		endtime=$(date --date "$dur" "+%s")
 		;;
     esac
@@ -65,6 +76,17 @@ do
 done
 shift
 
+if [ "$cont" = yes ]; then
+	if [ "$restart" = yes ]; then
+		echo 'ERROR: --restart and --continue cannot be combined' >&2
+		exit 1
+	fi
+elif [ "$restart" = yes ]; then
+	echo 'Re-enabling array checks for all arrays' >&2
+	rm -f /var/lib/mdcheck/Checked_*
+	exit $?
+fi
+
 # We need a temp file occasionally...
 tmp=/var/lib/mdcheck/.md-check-$$
 cnt=0
@@ -116,17 +138,16 @@ do
 	[[ "$MD_UUID" ]] || continue
 
 	fl="/var/lib/mdcheck/MD_UUID_$MD_UUID"
-	if [ -z "$cont" ]
-	then
-		start=0
-		logger -p daemon.info mdcheck start checking $dev
-	elif [ -z "$MD_UUID" -o ! -f "$fl" ]
-	then
-		# Nothing to continue here
-		continue
-	else
+	checked="${fl/MD_UUID_/Checked_}"
+	if [[ -f "$fl" ]]; then
+		[[ ! -f "$checked" ]] || {
+		    echo "WARNING: $checked exists, continuing anyway" >&2
+		}
 		start=`cat "$fl"`
-		logger -p daemon.info mdcheck continue checking $dev from $start
+	elif [[ ! -f "$checked" && -z "$cont" ]]; then
+		start=0
+	else # nothing to do
+		continue
 	fi
 
 	: "$((cnt+=1))"
@@ -136,6 +157,7 @@ do
 	echo $start > $fl
 	echo $start > $sys/md/sync_min
 	echo check > $sys/md/sync_action
+	logger -p daemon.info mdcheck checking $dev from $start
 done
 
 if [ -z "$endtime" ]
@@ -158,7 +180,8 @@ do
 		then
 			logger -p daemon.info mdcheck finished checking $dev
 			eval MD_${i}_fl=
-			rm -f $fl
+			rm -f "$fl"
+			touch "${fl/MD_UUID_/Checked_}"
 			continue;
 		fi
 		read a rest < $sys/md/sync_completed
systemd/mdcheck_continue.service +4 -2
index cd12db8..8eb97cf 100644
@@ -7,10 +7,12 @@
 
 [Unit]
 Description=MD array scrubbing - continuation
-ConditionPathExistsGlob=/var/lib/mdcheck/MD_UUID_*
 Documentation=man:mdadm(8)
 
 [Service]
 Type=simple
 Environment="MDADM_CHECK_DURATION=6 hours"
-ExecStart=/usr/share/mdadm/mdcheck --continue --duration ${MDADM_CHECK_DURATION}
+# Note that we're not calling "mdcheck --continue" here.
+# We want previously started checks to be continued, and new ones
+# to be started.
+ExecStart=/usr/share/mdadm/mdcheck --duration ${MDADM_CHECK_DURATION}
systemd/mdcheck_start.service +1 -2
index 16ba6b6..c7ddd4f 100644
@@ -12,5 +12,4 @@ Documentation=man:mdadm(8)
 
 [Service]
 Type=simple
-Environment="MDADM_CHECK_DURATION=6 hours"
-ExecStart=/usr/share/mdadm/mdcheck --duration ${MDADM_CHECK_DURATION}
+ExecStart=/usr/share/mdadm/mdcheck --restart
systemd/mdcheck_start.timer +1 -1
index 1b8f3f2..8d09b3f 100644
@@ -9,7 +9,7 @@
 Description=MD array scrubbing
 
 [Timer]
-OnCalendar=Sun *-*-1..7 1:05:00
+OnCalendar=Sun *-*-1..7 0:45:00
 
 [Install]
 WantedBy= mdmonitor.service
Signature · 1 line
2.51.0
#4 lore raw

PATCH 4/4 mdcheck: log to stderr from systemd units

In reply to Martin Wilck

If the script is run from systemd, simply writing to stderr will create more concise output. Otherwise, use logger like before.

Use the same logic for other messages printed by the script during runtime, except for error messages related to the invocation.

---
 misc/mdcheck | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
misc/mdcheck +15 -5
index df4fd3b..85d79a3 100644
@@ -37,6 +37,16 @@
 # Use "mdcheck --restart" to remove these markers and re-enable checking
 # all arrays.
 
+# If the script is run from systemd, simply write to the journal on stderr.
+# Otherwise, use logger.
+log() {
+    if [[ "$INVOCATION_ID" ]]; then
+	    echo "$@" >&2
+    else
+	    logger -p daemon.info "$@"
+    fi
+}
+
 # get device name from sysfs
 devname() {
     local dev
@@ -82,7 +92,7 @@ if [ "$cont" = yes ]; then
 		exit 1
 	fi
 elif [ "$restart" = yes ]; then
-	echo 'Re-enabling array checks for all arrays' >&2
+	log 'Re-enabling array checks for all arrays'
 	rm -f /var/lib/mdcheck/Checked_*
 	exit $?
 fi
@@ -110,7 +120,7 @@ cleanup() {
 	fi
 	echo idle > $sys/md/sync_action
 	cat $sys/md/sync_min > $fl
-	logger -p daemon.info pause checking $dev at `cat $fl`
+	log pause checking $dev at `cat $fl`
     done
     rm -f "$tmp"
 }
@@ -141,7 +151,7 @@ do
 	checked="${fl/MD_UUID_/Checked_}"
 	if [[ -f "$fl" ]]; then
 		[[ ! -f "$checked" ]] || {
-		    echo "WARNING: $checked exists, continuing anyway" >&2
+		    log "WARNING: $checked exists, continuing anyway"
 		}
 		start=`cat "$fl"`
 	elif [[ ! -f "$checked" && -z "$cont" ]]; then
@@ -157,7 +167,7 @@ do
 	echo $start > $fl
 	echo $start > $sys/md/sync_min
 	echo check > $sys/md/sync_action
-	logger -p daemon.info mdcheck checking $dev from $start
+	log mdcheck checking $dev from $start
 done
 
 if [ -z "$endtime" ]
@@ -178,7 +188,7 @@ do
 
 		if [ "`cat $sys/md/sync_action`" != 'check' ]
 		then
-			logger -p daemon.info mdcheck finished checking $dev
+			log mdcheck finished checking $dev
 			eval MD_${i}_fl=
 			rm -f "$fl"
 			touch "${fl/MD_UUID_/Checked_}"
Signature · 1 line
2.51.0
#5 lore raw
In reply to Martin Wilck
Quoted 28 lines · Martin Wilck

This Patch set changes the logic of the "mdcheck" tool and the related systemd services mdcheck_start.service and mdcheck_continue.service.
The set and related discussion are also posted as GitHub PR [1].

The set is meant to be applied on top of PR#189 [2], which has already been merged in the current main branch on GitHub.

[1] https://github.com/md-raid-utilities/mdadm/pull/190
[2] https://github.com/md-raid-utilities/mdadm/pull/189

The current behavior is like this:

* mdcheck without arguments starts a RAID check on all arrays on the system, starting at position 0. This is started from mdcheck_start.service, started by a systemd timer once a month.
* mdcheck --continue looks for files /var/lib/mdcheck/MD_UUID_$UUID, reads the start position from them, and starts a check from that position on the array with the respective UUID. This is started from a systemd timer every night.

In either case, mdcheck won't do anything if the kernel is already running a sync_action on a given array. The check runs for a given period of time (default 6h) and saves the last position in the MD_UUID file, to be taken up when mdcheck --continue is called next time. When the entire array has been checked, the MD_UUID_ file is deleted. When all checks are finished, mdcheck_continue.timer is stopped, to be restarted when mdcheck_start.timer expires next time.

Quoted 7 lines

Before the recent commit 8aa4ea9 ("systemd: start mdcheck_continue.timer before mdcheck_start.timer"), this could lead to a race condition when the check for a given array didn't complete throughout the month. mdcheck_start.service would start and reset the check position to 0 before mdcheck_continue.service could pick up at the last saved position. Commit 8aa4ea9 works around this by starting mdcheck_continue.service a few minutes before mdcheck_start.timer.

Hi Martin

The race condition is caused by the faulty modification by admin. commit 8aa4ea9 ("systemd: start mdcheck_continue.timer before mdcheck_start.timer") already fixes the problem that an array should continue to do the check if it doesn't finish checking in one month. The admin changes the timer sequence back again, it's a faulty action. We can add a warning comment in the timer service file to avoid such a race.

Quoted 5 lines

Yet the general problem still exists: both services trigger checks on the kernel's part which they can only passively monitor. So if a user plays with the timer settings (which he is in his rights to do), another similar race might happen.

misc/mdcheck +1 -1
index 398a1ea607ca..7d1d79f795f0 100644
@@ -116,7 +116,7 @@ do
        mdadm --detail --export "$dev" | grep '^MD_UUID=' > $tmp || continue
        source $tmp
        fl="/var/lib/mdcheck/MD_UUID_$MD_UUID"
-       if [ -z "$cont" ]
+       if [ -z "$cont" -a ! -f "$fl" ]
        then
                start=0
                logger -p daemon.info mdcheck start checking $dev

How about this? The start action checks the UUID file. So the check will continue if it doesn't finish in one month.

Best Regards
Xiao

Quoted 43 lines

This patch set changes the behavior as follows:

Only mdcheck_continue.service actually starts and stops kernel-based sync actions. This service will continue at the saved start position if an MD_UUID* file exists, or start a new check at position 0 otherwise. Starting at 0 can be inhibited by creating a file /var/lib/mdcheck/Checked_$UUID. These files will be created by mdcheck when it finishes checking a given array. Thus future invocations of mdcheck_continue.service will not restart the check on this array.

mdcheck_start.service runs mdcheck --restart, which simply removes all Checked_* markers from /var/lib/mdcheck, so that the next invocation of mdcheck_continue.service will start new checks on all arrays which don't have already running checks.

The general behavior of the systemd timers and services is like before, but the mentioned race condition is avoided, even if the user modifies the timer settings arbitrarily.

This set slightly changes the behavior of the mdcheck script. Without --continue, it will still start checks on all array, but unlike before it will skip arrays for with a Checked_ marker exists. To avoid that, run mdcheck --restart before mdcheck.

More details in the commit description of the patch "mdcheck: simplify start / continue logic and add "--restart" logic".

Martin Wilck (4):
mdcheck: loop over sync_action files in sysfs
mdcheck: replace deprecated "$[cnt+1]" syntax
mdcheck: simplify start / continue logic and add "--restart" logic
mdcheck: log to stderr from systemd units

 misc/mdcheck                     | 105 ++++++++++++++++++++-----------
 systemd/mdcheck_continue.service |   6 +-
 systemd/mdcheck_start.service    |   3 +-
 systemd/mdcheck_start.timer      |   2 +-
 4 files changed, 75 insertions(+), 41 deletions(-)

--
2.51.0

#6 lore raw
Quoted 55 lines · Xiao Ni
Quoted 41 lines · Martin Wilck

This Patch set changes the logic of the "mdcheck" tool and the related systemd
services mdcheck_start.service and mdcheck_continue.service. The set and related discussion are also posted as GitHub PR [1].

The set is meant to be applied on top of PR#189 [2], which has already been
merged in the current main branch on GitHub.

[1] https://github.com/md-raid-utilities/mdadm/pull/190
[2] https://github.com/md-raid-utilities/mdadm/pull/189

The current behavior is like this:

* mdcheck without arguments starts a RAID check on all arrays on the system,
  starting at position 0. This is started from mdcheck_start.service,
  started by a systemd timer once a month.
* mdcheck --continue looks for files /var/lib/mdcheck/MD_UUID_$UUID, reads the
  start position from them, and starts a check from that position on the array
  with the respective UUID. This is started from a systemd timer every night.

In either case, mdcheck won't do anything if the kernel is already running a
sync_action on a given array. The check runs for a given period of time
(default 6h) and saves the last position in the MD_UUID file, to be taken up
when mdcheck --continue is called next time. When the entire array has been
checked, the MD_UUID_ file is deleted. When all checks are finished,
mdcheck_continue.timer is stopped, to be restarted when mdcheck_start.timer
expires next time.

Quoted 9 lines

Before the recent commit 8aa4ea9 ("systemd: start mdcheck_continue.timer
before mdcheck_start.timer"), this could lead to a race condition when the
check for a given array didn't complete throughout the month. mdcheck_start.service would start and reset the check position to 0 before mdcheck_continue.service could pick up at the last saved position. Commit 8aa4ea9 works around this by starting mdcheck_continue.service a few minutes before mdcheck_start.timer.

Hi Martin

Quoted 9 lines

The race condition is caused by the faulty modification by admin. commit 8aa4ea9 ("systemd: start mdcheck_continue.timer before mdcheck_start.timer") already fixes the problem that an array should continue to do the check if it doesn't finish checking in one month. The admin changes the timer sequence back again, it's a faulty action.
We can add a warning comment in the timer service file to avoid such a
race.

True. Still, the cleaner solution is to avoid multiple user space services interacting with the kernel's sync_action API. Why hope that a warning comment will be adhered to, if we can avoid the issue in the first place?

Quoted 24 lines
Quoted 8 lines

Yet the general problem still exists: both services trigger checks on the
kernel's part which they can only passively monitor. So if a user plays with
the timer settings (which he is in his rights to do), another similar race
might happen.

misc/mdcheck +0 -0
index 398a1ea607ca..7d1d79f795f0 100644
@@ -116,7 +116,7 @@ do

        mdadm --detail --export "$dev" | grep '^MD_UUID=' > $tmp || continue
        source $tmp fl="/var/lib/mdcheck/MD_UUID_$MD_UUID"
-       if [ -z "$cont" ]
+       if [ -z "$cont" -a ! -f "$fl" ]
        then
                start=0
                logger -p daemon.info mdcheck start checking $dev

Quoted 2 lines

How about this? The start action checks the UUID file. So the check will continue if it doesn't finish in one month.

Yes, we could do that. The question is if it's better than what I'm proposing. Personally, I started looking closely at "mdcheck" and the related services only recently, and I found the way the two services interoperate kind of confusing. The fact that it took more than 10y to discover the discussed race condition indicates that I wasn't the only one who was confused.

Therefore I would like to get rid of two independent systemd services that both start and stop RAID checks. To try an analogy, instead of two drivers competing for the steering wheel, we'll have just one driver and one that operates the traffic light. IMO that logic is easier to assess. You seem to disagree; I'd like to understand what your reasons are. So far you have argued that this change isn't strictly necessary. That's true, but that doesn't imply that the idea is wrong.

Best Regards,
Martin

Quoted 63 lines

Best Regards
Xiao

Quoted 60 lines

This patch set changes the behavior as follows:

Only mdcheck_continue.service actually starts and stops kernel- based sync
actions. This service will continue at the saved start position if an MD_UUID*
file exists, or start a new check at position 0 otherwise. Starting at 0 can
be inhibited by creating a file /var/lib/mdcheck/Checked_$UUID. These files
will be created by mdcheck when it finishes checking a given array. Thus
future invocations of mdcheck_continue.service will not restart the check on
this array.

mdcheck_start.service runs mdcheck --restart, which simply removes all
Checked_* markers from /var/lib/mdcheck, so that the next invocation of
mdcheck_continue.service will start new checks on all arrays which don't have
already running checks.

The general behavior of the systemd timers and services is like before, but
the mentioned race condition is avoided, even if the user modifies the timer
settings arbitrarily.

This set slightly changes the behavior of the mdcheck script. Without
--continue, it will still start checks on all array, but unlike before it will
skip arrays for with a Checked_ marker exists. To avoid that, run mdcheck
--restart before mdcheck.

More details in the commit description of the patch "mdcheck:
simplify start /
continue logic and add "--restart" logic".

Martin Wilck (4):
  mdcheck: loop over sync_action files in sysfs
  mdcheck: replace deprecated "$[cnt+1]" syntax
  mdcheck: simplify start / continue logic and add "--restart" logic
  mdcheck: log to stderr from systemd units

 misc/mdcheck                     | 105 ++++++++++++++++++++-------
----
 systemd/mdcheck_continue.service |   6 +- systemd/mdcheck_start.service    |   3 +- systemd/mdcheck_start.timer      |   2 +-

 4 files changed, 75 insertions(+), 41 deletions(-)

--
2.51.0

#7 lore raw
Quoted 67 lines · Martin Wilck
Quoted 55 lines · Xiao Ni
Quoted 41 lines · Martin Wilck

This Patch set changes the logic of the "mdcheck" tool and the related systemd
services mdcheck_start.service and mdcheck_continue.service. The set and related discussion are also posted as GitHub PR [1].

The set is meant to be applied on top of PR#189 [2], which has already been
merged in the current main branch on GitHub.

[1] https://github.com/md-raid-utilities/mdadm/pull/190
[2] https://github.com/md-raid-utilities/mdadm/pull/189

The current behavior is like this:

* mdcheck without arguments starts a RAID check on all arrays on the system,
starting at position 0. This is started from
mdcheck_start.service,
started by a systemd timer once a month.
* mdcheck --continue looks for files /var/lib/mdcheck/MD_UUID_$UUID, reads the
start position from them, and starts a check from that position
on the array
with the respective UUID. This is started from a systemd timer
every night.

In either case, mdcheck won't do anything if the kernel is already running a
sync_action on a given array. The check runs for a given period of time
(default 6h) and saves the last position in the MD_UUID file, to be taken up
when mdcheck --continue is called next time. When the entire array has been
checked, the MD_UUID_ file is deleted. When all checks are finished,
mdcheck_continue.timer is stopped, to be restarted when mdcheck_start.timer
expires next time.

Quoted 9 lines

Before the recent commit 8aa4ea9 ("systemd: start mdcheck_continue.timer
before mdcheck_start.timer"), this could lead to a race condition when the
check for a given array didn't complete throughout the month. mdcheck_start.service would start and reset the check position to 0 before mdcheck_continue.service could pick up at the last saved position. Commit 8aa4ea9 works around this by starting mdcheck_continue.service a few minutes before mdcheck_start.timer.

Hi Martin

Quoted 9 lines

The race condition is caused by the faulty modification by admin. commit 8aa4ea9 ("systemd: start mdcheck_continue.timer before mdcheck_start.timer") already fixes the problem that an array should continue to do the check if it doesn't finish checking in one month. The admin changes the timer sequence back again, it's a faulty action.
We can add a warning comment in the timer service file to avoid such a
race.

Quoted 4 lines

True. Still, the cleaner solution is to avoid multiple user space services interacting with the kernel's sync_action API. Why hope that a warning comment will be adhered to, if we can avoid the issue in the first place?

The sync_action can handle multiple access. In fact all sys files can handle such races. And we should think about backward compatibility as we talked. It's better to keep the original meaning of these scripts themselves. start_check kicks off the check and start_continue is used to continue the check if the check can't finish.

Quoted 35 lines
Quoted 25 lines
Quoted 9 lines

Yet the general problem still exists: both services trigger checks on the
kernel's part which they can only passively monitor. So if a user plays with
the timer settings (which he is in his rights to do), another similar race
might happen.

misc/mdcheck +0 -0
index 398a1ea607ca..7d1d79f795f0 100644
@@ -116,7 +116,7 @@ do
        mdadm --detail --export "$dev" | grep '^MD_UUID=' > $tmp ||

continue

        source $tmp
        fl="/var/lib/mdcheck/MD_UUID_$MD_UUID"
misc/mdcheck +1 -1
-       if [ -z "$cont" ]
+       if [ -z "$cont" -a ! -f "$fl" ]
        then
                start=0
                logger -p daemon.info mdcheck start checking $dev
Quoted 2 lines

How about this? The start action checks the UUID file. So the check will continue if it doesn't finish in one month.

Yes, we could do that. The question is if it's better than what I'm proposing. Personally, I started looking closely at "mdcheck" and the related services only recently, and I found the way the two services interoperate kind of confusing. The fact that it took more than 10y to discover the discussed race condition indicates that I wasn't the only one who was confused.

Quoted 7 lines

Therefore I would like to get rid of two independent systemd services that both start and stop RAID checks. To try an analogy, instead of two drivers competing for the steering wheel, we'll have just one driver and one that operates the traffic light. IMO that logic is easier to assess. You seem to disagree; I'd like to understand what your reasons are. So far you have argued that this change isn't strictly necessary. That's true, but that doesn't imply that the idea is wrong.

The reason is mentioned above about backward compatibility. If it's a new function, I totally agree with you. But the two services have run for many years on many machines. So can you fix this problem based on the two services and mark mdcheck_continue as deprecated? And we can remove mdcheck_continue in future.

Regards
Xiao

Quoted 68 lines

Best Regards,
Martin

Quoted 63 lines

Best Regards
Xiao

Quoted 60 lines

This patch set changes the behavior as follows:

Only mdcheck_continue.service actually starts and stops kernel- based sync
actions. This service will continue at the saved start position if an MD_UUID*
file exists, or start a new check at position 0 otherwise. Starting at 0 can
be inhibited by creating a file /var/lib/mdcheck/Checked_$UUID. These files
will be created by mdcheck when it finishes checking a given array. Thus
future invocations of mdcheck_continue.service will not restart the check on
this array.

mdcheck_start.service runs mdcheck --restart, which simply removes all
Checked_* markers from /var/lib/mdcheck, so that the next invocation of
mdcheck_continue.service will start new checks on all arrays which don't have
already running checks.

The general behavior of the systemd timers and services is like before, but
the mentioned race condition is avoided, even if the user modifies the timer
settings arbitrarily.

This set slightly changes the behavior of the mdcheck script. Without
--continue, it will still start checks on all array, but unlike before it will
skip arrays for with a Checked_ marker exists. To avoid that, run mdcheck
--restart before mdcheck.

More details in the commit description of the patch "mdcheck:
simplify start /
continue logic and add "--restart" logic".

Martin Wilck (4):
mdcheck: loop over sync_action files in sysfs
mdcheck: replace deprecated "$[cnt+1]" syntax
mdcheck: simplify start / continue logic and add "--restart"
logic
mdcheck: log to stderr from systemd units

 misc/mdcheck                     | 105 ++++++++++++++++++++-------
----
 systemd/mdcheck_continue.service |   6 +-
 systemd/mdcheck_start.service    |   3 +-
 systemd/mdcheck_start.timer      |   2 +-
 4 files changed, 75 insertions(+), 41 deletions(-)

--
2.51.0

#8 lore raw
Quoted 91 lines · Xiao Ni
Quoted 82 lines · Martin Wilck
Quoted 67 lines · Xiao Ni
Quoted 49 lines · Martin Wilck

This Patch set changes the logic of the "mdcheck" tool and the related systemd
services mdcheck_start.service and mdcheck_continue.service. The set and related discussion are also posted as GitHub PR [1].

The set is meant to be applied on top of PR#189 [2], which has already been
merged in the current main branch on GitHub.

[1] https://github.com/md-raid-utilities/mdadm/pull/190
[2] https://github.com/md-raid-utilities/mdadm/pull/189

The current behavior is like this:

* mdcheck without arguments starts a RAID check on all arrays on
the system,
  starting at position 0. This is started from mdcheck_start.service,
  started by a systemd timer once a month.
* mdcheck --continue looks for files /var/lib/mdcheck/MD_UUID_$UUID, reads the
  start position from them, and starts a check from that position
on the array
  with the respective UUID. This is started from a systemd timer
every night.

In either case, mdcheck won't do anything if the kernel is already
running a
sync_action on a given array. The check runs for a given period of
time
(default 6h) and saves the last position in the MD_UUID file, to be
taken up
when mdcheck --continue is called next time. When the entire array
has been
checked, the MD_UUID_ file is deleted. When all checks are finished,
mdcheck_continue.timer is stopped, to be restarted when mdcheck_start.timer
expires next time.

Quoted 12 lines

Before the recent commit 8aa4ea9 ("systemd: start mdcheck_continue.timer
before mdcheck_start.timer"), this could lead to a race condition
when the
check for a given array didn't complete throughout the month. mdcheck_start.service would start and reset the check position to 0
before mdcheck_continue.service could pick up at the last saved position. Commit 8aa4ea9 works around this by starting mdcheck_continue.service a few minutes before mdcheck_start.timer.

Hi Martin

Quoted 12 lines

The race condition is caused by the faulty modification by admin. commit 8aa4ea9 ("systemd: start mdcheck_continue.timer before mdcheck_start.timer") already fixes the problem that an array should
continue to do the check if it doesn't finish checking in one month.
The admin changes the timer sequence back again, it's a faulty action.
We can add a warning comment in the timer service file to avoid such
a
race.

Quoted 6 lines

True. Still, the cleaner solution is to avoid multiple user space services interacting with the kernel's sync_action API. Why hope that a
warning comment will be adhered to, if we can avoid the issue in the
first place?

Quoted 2 lines

The sync_action can handle multiple access. In fact all sys files can handle such races.

Sure. But the two competing scripts make logic difficult to follow.

Quoted 61 lines

And we should think about backward compatibility as
we talked. It's better to keep the original meaning of these scripts themselves. start_check kicks off the check and start_continue is used
to continue the check if the check can't finish.

Quoted 42 lines
Quoted 28 lines
Quoted 11 lines

Yet the general problem still exists: both services trigger checks
on the
kernel's part which they can only passively monitor. So if a user
plays with
the timer settings (which he is in his rights to do), another similar race
might happen.

misc/mdcheck +0 -0
index 398a1ea607ca..7d1d79f795f0 100644
@@ -116,7 +116,7 @@ do

        mdadm --detail --export "$dev" | grep '^MD_UUID=' > $tmp ||
continue
        source $tmp fl="/var/lib/mdcheck/MD_UUID_$MD_UUID"
-       if [ -z "$cont" ]
+       if [ -z "$cont" -a ! -f "$fl" ]
        then
                start=0
                logger -p daemon.info mdcheck start checking $dev

Quoted 3 lines

How about this? The start action checks the UUID file. So the check
will continue if it doesn't finish in one month.

Yes, we could do that. The question is if it's better than what I'm proposing. Personally, I started looking closely at "mdcheck" and the
related services only recently, and I found the way the two services
interoperate kind of confusing. The fact that it took more than 10y to discover the discussed race condition indicates that I wasn't the
only one who was confused.

Quoted 13 lines

Therefore I would like to get rid of two independent systemd services
that both start and stop RAID checks. To try an analogy, instead of two
drivers competing for the steering wheel, we'll have just one driver
and one that operates the traffic light. IMO that logic is easier to
assess. You seem to disagree; I'd like to understand what your reasons
are. So far you have argued that this change isn't strictly necessary.
That's true, but that doesn't imply that the idea is wrong.

Quoted 5 lines

The reason is mentioned above about backward compatibility. If it's a new function, I totally agree with you. But the two services have run for many years on many machines. So can you fix this problem based on the two services and mark mdcheck_continue as deprecated? And we can remove mdcheck_continue in future.

I don't understand why you want to keep two concurrent services doing the same thing. What potential backward compatibilty issue does this solve?

I agree that backward compatibility is important for mdcheck itself and its various incantations, but I have a hard time getting the point for the services. What matters here, AFAICS, is that we have a "long" time interval in which we start new checks, and a "short" one in which we continue previously started ones (defaulting to "1 month" and "1 day", respectively). My patch doesn't change these semantics. Actually, it makes them more obvious.

I also don't understand how we'd be able to deprecate mdcheck_continue.service if we want to keep both services.

Anyway, to avoid going in circles, I will just drop the patch you dislike. As discussed, it isn't strictly necessary to avoid the known race.

Martin

Quoted 80 lines

Regards
Xiao

Quoted 77 lines

Best Regards,
Martin

Quoted 72 lines

Best Regards
Xiao

Quoted 69 lines

This patch set changes the behavior as follows:

Only mdcheck_continue.service actually starts and stops kernel- based sync
actions. This service will continue at the saved start position if
an MD_UUID*
file exists, or start a new check at position 0 otherwise. Starting
at 0 can
be inhibited by creating a file /var/lib/mdcheck/Checked_$UUID. These files
will be created by mdcheck when it finishes checking a given array.
Thus
future invocations of mdcheck_continue.service will not restart the
check on
this array.

mdcheck_start.service runs mdcheck --restart, which simply removes
all
Checked_* markers from /var/lib/mdcheck, so that the next invocation of
mdcheck_continue.service will start new checks on all arrays which
don't have
already running checks.

The general behavior of the systemd timers and services is like before, but
the mentioned race condition is avoided, even if the user modifies
the timer
settings arbitrarily.

This set slightly changes the behavior of the mdcheck script. Without
--continue, it will still start checks on all array, but unlike before it will
skip arrays for with a Checked_ marker exists. To avoid that, run
mdcheck
--restart before mdcheck.

More details in the commit description of the patch "mdcheck:
simplify start /
continue logic and add "--restart" logic".

Martin Wilck (4):
  mdcheck: loop over sync_action files in sysfs
  mdcheck: replace deprecated "$[cnt+1]" syntax
  mdcheck: simplify start / continue logic and add "--restart" logic
  mdcheck: log to stderr from systemd units

 misc/mdcheck                     | 105 ++++++++++++++++++++---
----
----
 systemd/mdcheck_continue.service |   6 +- systemd/mdcheck_start.service    |   3 +- systemd/mdcheck_start.timer      |   2 +-

 4 files changed, 75 insertions(+), 41 deletions(-)

--
2.51.0

#9 lore raw
Quoted 177 lines · Martin Wilck
Quoted 91 lines · Xiao Ni
Quoted 82 lines · Martin Wilck
Quoted 67 lines · Xiao Ni
Quoted 49 lines · Martin Wilck

This Patch set changes the logic of the "mdcheck" tool and the related systemd
services mdcheck_start.service and mdcheck_continue.service. The set and related discussion are also posted as GitHub PR [1].

The set is meant to be applied on top of PR#189 [2], which has already been
merged in the current main branch on GitHub.

[1] https://github.com/md-raid-utilities/mdadm/pull/190
[2] https://github.com/md-raid-utilities/mdadm/pull/189

The current behavior is like this:

* mdcheck without arguments starts a RAID check on all arrays on
the system,
starting at position 0. This is started from
mdcheck_start.service,
started by a systemd timer once a month.
* mdcheck --continue looks for files /var/lib/mdcheck/MD_UUID_$UUID, reads the
start position from them, and starts a check from that
position
on the array
with the respective UUID. This is started from a systemd
timer
every night.

In either case, mdcheck won't do anything if the kernel is already
running a
sync_action on a given array. The check runs for a given period of
time
(default 6h) and saves the last position in the MD_UUID file, to be
taken up
when mdcheck --continue is called next time. When the entire array
has been
checked, the MD_UUID_ file is deleted. When all checks are finished,
mdcheck_continue.timer is stopped, to be restarted when mdcheck_start.timer
expires next time.

Quoted 12 lines

Before the recent commit 8aa4ea9 ("systemd: start mdcheck_continue.timer
before mdcheck_start.timer"), this could lead to a race condition
when the
check for a given array didn't complete throughout the month. mdcheck_start.service would start and reset the check position to 0
before mdcheck_continue.service could pick up at the last saved position. Commit 8aa4ea9 works around this by starting mdcheck_continue.service a few minutes before mdcheck_start.timer.

Hi Martin

Quoted 12 lines

The race condition is caused by the faulty modification by admin. commit 8aa4ea9 ("systemd: start mdcheck_continue.timer before mdcheck_start.timer") already fixes the problem that an array should
continue to do the check if it doesn't finish checking in one month.
The admin changes the timer sequence back again, it's a faulty action.
We can add a warning comment in the timer service file to avoid such
a
race.

Quoted 6 lines

True. Still, the cleaner solution is to avoid multiple user space services interacting with the kernel's sync_action API. Why hope that a
warning comment will be adhered to, if we can avoid the issue in the
first place?

Quoted 2 lines

The sync_action can handle multiple access. In fact all sys files can handle such races.

Sure. But the two competing scripts make logic difficult to follow.

Quoted 61 lines

And we should think about backward compatibility as
we talked. It's better to keep the original meaning of these scripts themselves. start_check kicks off the check and start_continue is used
to continue the check if the check can't finish.

Quoted 42 lines
Quoted 28 lines
Quoted 11 lines

Yet the general problem still exists: both services trigger checks
on the
kernel's part which they can only passively monitor. So if a user
plays with
the timer settings (which he is in his rights to do), another similar race
might happen.

misc/mdcheck +0 -0
index 398a1ea607ca..7d1d79f795f0 100644
@@ -116,7 +116,7 @@ do
        mdadm --detail --export "$dev" | grep '^MD_UUID=' > $tmp

||
continue

        source $tmp
        fl="/var/lib/mdcheck/MD_UUID_$MD_UUID"
misc/mdcheck +1 -1
-       if [ -z "$cont" ]
+       if [ -z "$cont" -a ! -f "$fl" ]
        then
                start=0
                logger -p daemon.info mdcheck start checking $dev
Quoted 3 lines

How about this? The start action checks the UUID file. So the check
will continue if it doesn't finish in one month.

Yes, we could do that. The question is if it's better than what I'm proposing. Personally, I started looking closely at "mdcheck" and the
related services only recently, and I found the way the two services
interoperate kind of confusing. The fact that it took more than 10y to discover the discussed race condition indicates that I wasn't the
only one who was confused.

Quoted 13 lines

Therefore I would like to get rid of two independent systemd services
that both start and stop RAID checks. To try an analogy, instead of two
drivers competing for the steering wheel, we'll have just one driver
and one that operates the traffic light. IMO that logic is easier to
assess. You seem to disagree; I'd like to understand what your reasons
are. So far you have argued that this change isn't strictly necessary.
That's true, but that doesn't imply that the idea is wrong.

Quoted 5 lines

The reason is mentioned above about backward compatibility. If it's a new function, I totally agree with you. But the two services have run for many years on many machines. So can you fix this problem based on the two services and mark mdcheck_continue as deprecated? And we can remove mdcheck_continue in future.

I don't understand why you want to keep two concurrent services doing the same thing. What potential backward compatibilty issue does this solve?

I agree that backward compatibility is important for mdcheck itself and its various incantations, but I have a hard time getting the point for the services. What matters here, AFAICS, is that we have a "long" time interval in which we start new checks, and a "short" one in which we continue previously started ones (defaulting to "1 month" and "1 day", respectively). My patch doesn't change these semantics. Actually, it makes them more obvious.

Quoted 6 lines

I also don't understand how we'd be able to deprecate mdcheck_continue.service if we want to keep both services.

Anyway, to avoid going in circles, I will just drop the patch you dislike. As discussed, it isn't strictly necessary to avoid the known race.

Hi Martin

It looks like I understood you wrongly. I thought you wanted to remove mdcheck_continue.service totally in future. So I tried to discuss this problem with you. For patch 3/4, I'd like you to describe the race in detail. Because commit 8aa4ea9 ("systemd: start mdcheck_continue.timer before mdcheck_start.timer") already fixes this problem. Why does the race come up again?

I have a question about the mdcheck_start.timer change. Now by default, the start timer is triggered at 0:45:00 and the continue timer is triggered at 1:00. So the check can't continue if the check hasn't finished in one month which you fixed in 8aa4ea9?

Sorry for bringing this trouble to you.

Best Regards
Xiao

Quoted 84 lines

Martin

Quoted 80 lines

Regards
Xiao

Quoted 77 lines

Best Regards,
Martin

Quoted 72 lines

Best Regards
Xiao

Quoted 69 lines

This patch set changes the behavior as follows:

Only mdcheck_continue.service actually starts and stops kernel- based sync
actions. This service will continue at the saved start position if
an MD_UUID*
file exists, or start a new check at position 0 otherwise. Starting
at 0 can
be inhibited by creating a file /var/lib/mdcheck/Checked_$UUID. These files
will be created by mdcheck when it finishes checking a given array.
Thus
future invocations of mdcheck_continue.service will not restart the
check on
this array.

mdcheck_start.service runs mdcheck --restart, which simply removes
all
Checked_* markers from /var/lib/mdcheck, so that the next invocation of
mdcheck_continue.service will start new checks on all arrays which
don't have
already running checks.

The general behavior of the systemd timers and services is like before, but
the mentioned race condition is avoided, even if the user modifies
the timer
settings arbitrarily.

This set slightly changes the behavior of the mdcheck script. Without
--continue, it will still start checks on all array, but unlike before it will
skip arrays for with a Checked_ marker exists. To avoid that, run
mdcheck
--restart before mdcheck.

More details in the commit description of the patch "mdcheck:
simplify start /
continue logic and add "--restart" logic".

Martin Wilck (4):
mdcheck: loop over sync_action files in sysfs
mdcheck: replace deprecated "$[cnt+1]" syntax
mdcheck: simplify start / continue logic and add "--restart"
logic
mdcheck: log to stderr from systemd units

 misc/mdcheck                     | 105 ++++++++++++++++++++---
----
----
 systemd/mdcheck_continue.service |   6 +-
 systemd/mdcheck_start.service    |   3 +-
 systemd/mdcheck_start.timer      |   2 +-
 4 files changed, 75 insertions(+), 41 deletions(-)

--
2.51.0

#10 lore raw
Quoted 228 lines · Xiao Ni
Quoted 218 lines · Martin Wilck
Quoted 108 lines · Xiao Ni
Quoted 95 lines · Martin Wilck
Quoted 77 lines · Xiao Ni
Quoted 56 lines · Martin Wilck

This Patch set changes the logic of the "mdcheck" tool and the
related systemd
services mdcheck_start.service and mdcheck_continue.service.
The set and related discussion are also posted as GitHub PR [1].

The set is meant to be applied on top of PR#189 [2], which has
already been
merged in the current main branch on GitHub.

[1] https://github.com/md-raid-utilities/mdadm/pull/190
[2] https://github.com/md-raid-utilities/mdadm/pull/189

The current behavior is like this:

* mdcheck without arguments starts a RAID check on all arrays
on
the system,
  starting at position 0. This is started from mdcheck_start.service,
  started by a systemd timer once a month.
* mdcheck --continue looks for files /var/lib/mdcheck/MD_UUID_$UUID, reads the
  start position from them, and starts a check from that position
on the array
  with the respective UUID. This is started from a systemd timer
every night.

In either case, mdcheck won't do anything if the kernel is already
running a
sync_action on a given array. The check runs for a given period
of
time
(default 6h) and saves the last position in the MD_UUID file,
to be
taken up
when mdcheck --continue is called next time. When the entire
array
has been
checked, the MD_UUID_ file is deleted. When all checks are finished,
mdcheck_continue.timer is stopped, to be restarted when mdcheck_start.timer
expires next time.

Quoted 15 lines

Before the recent commit 8aa4ea9 ("systemd: start mdcheck_continue.timer
before mdcheck_start.timer"), this could lead to a race condition
when the
check for a given array didn't complete throughout the month.
mdcheck_start.service would start and reset the check position
to 0
before mdcheck_continue.service could pick up at the last saved
position. Commit 8aa4ea9 works around this by starting mdcheck_continue.service a few minutes before mdcheck_start.timer.

Hi Martin

Quoted 15 lines

The race condition is caused by the faulty modification by admin.
commit 8aa4ea9 ("systemd: start mdcheck_continue.timer before mdcheck_start.timer") already fixes the problem that an array should
continue to do the check if it doesn't finish checking in one month.
The admin changes the timer sequence back again, it's a faulty
action.
We can add a warning comment in the timer service file to avoid
such
a
race.

Quoted 9 lines

True. Still, the cleaner solution is to avoid multiple user space
services interacting with the kernel's sync_action API. Why hope
that a
warning comment will be adhered to, if we can avoid the issue in
the
first place?

Quoted 3 lines

The sync_action can handle multiple access. In fact all sys files can
handle such races.

Sure. But the two competing scripts make logic difficult to follow.

Quoted 72 lines

And we should think about backward compatibility as
we talked. It's better to keep the original meaning of these scripts
themselves. start_check kicks off the check and start_continue is used
to continue the check if the check can't finish.

Quoted 50 lines
Quoted 32 lines
Quoted 13 lines

Yet the general problem still exists: both services trigger checks
on the
kernel's part which they can only passively monitor. So if a
user
plays with
the timer settings (which he is in his rights to do), another
similar race
might happen.

misc/mdcheck +0 -0
index 398a1ea607ca..7d1d79f795f0 100644
@@ -116,7 +116,7 @@ do

        mdadm --detail --export "$dev" | grep '^MD_UUID=' > $tmp

Quoted 1 line
Quoted 1 line

continue
        source $tmp fl="/var/lib/mdcheck/MD_UUID_$MD_UUID"
-       if [ -z "$cont" ]
+       if [ -z "$cont" -a ! -f "$fl" ]
        then
                start=0
                logger -p daemon.info mdcheck start checking $dev

Quoted 3 lines

How about this? The start action checks the UUID file. So the check
will continue if it doesn't finish in one month.

Yes, we could do that. The question is if it's better than what I'm
proposing. Personally, I started looking closely at "mdcheck" and
the
related services only recently, and I found the way the two services
interoperate kind of confusing. The fact that it took more than 10y
to discover the discussed race condition indicates that I wasn't
the
only one who was confused.

Quoted 15 lines

Therefore I would like to get rid of two independent systemd services
that both start and stop RAID checks. To try an analogy, instead of
two
drivers competing for the steering wheel, we'll have just one driver
and one that operates the traffic light. IMO that logic is easier
to
assess. You seem to disagree; I'd like to understand what your reasons
are. So far you have argued that this change isn't strictly necessary.
That's true, but that doesn't imply that the idea is wrong.

Quoted 9 lines

The reason is mentioned above about backward compatibility. If it's a
new function, I totally agree with you. But the two services have run
for many years on many machines. So can you fix this problem based on
the two services and mark mdcheck_continue as deprecated? And we can
remove mdcheck_continue in future.

I don't understand why you want to keep two concurrent services doing
the same thing. What potential backward compatibilty issue does this
solve?

I agree that backward compatibility is important for mdcheck itself and
its various incantations, but I have a hard time getting the point for
the services. What matters here, AFAICS, is that we have a "long" time
interval in which we start new checks, and a "short" one in which we
continue previously started ones (defaulting to "1 month" and "1 day",
respectively). My patch doesn't change these semantics. Actually, it
makes them more obvious.

Quoted 7 lines

I also don't understand how we'd be able to deprecate mdcheck_continue.service if we want to keep both services.

Anyway, to avoid going in circles, I will just drop the patch you dislike. As discussed, it isn't strictly necessary to avoid the known
race.

Quoted 5 lines

Hi Martin

It looks like I understood you wrongly. I thought you wanted to remove
mdcheck_continue.service totally in future.

The history of my GitHub PR probably confused you. Sorry for that.

The first version of the PR on GitHub ("combine mdcheck_start and mdcheck_continue into a single service") actually removed one of the services. But I'd changed that in the 2nd version on Aug 21st [1].

[1] https://github.com/md-raid-utilities/mdadm/pull/190#event-19274907168

I'd realized that this was problematic in terms of backward compatibility. Removing or renaming systemd services in a package update is almost impossible do properly for distributions. Being a distribution person myself, I didn't want to cause needless trouble for myself and fellow distro people ;-)

Moreover, I'd started to think my first approach wasn't elegant. It had big backward compatibility issues, much more than the current version.

Quoted 6 lines

So I tried to discuss this
problem with you. For patch 3/4, I'd like you to describe the race in detail. Because commit 8aa4ea9 ("systemd: start mdcheck_continue.timer
before mdcheck_start.timer") already fixes this problem. Why does the race come up again?

It would only come up again if a user modified the timer schedule such that mdcheck_start.service was again started before mdcheck_continue.service on the same day. That's possible, but not likely, and we could blame the user if it happened.

OTOH, my approach in this patchset would have completely avoided this sort of race.

Quoted 4 lines

I have a question about the mdcheck_start.timer change. Now by default, the start timer is triggered at 0:45:00 and the continue timer is triggered at 1:00. So the check can't continue if the check hasn't finished in one month which you fixed in 8aa4ea9?

No. Checks that haven't been finished yet would continue normally. With this set, mdcheck_start.service would just delete the markers that inhibit starting new checks.

Consider two arrays A and B. A has finished checking, B has not. So under /var/lib/mdcheck, you'd have files "MD_UUID_$B" and "Checked_$A".

* On a "normal" day, only mdcheck_continue.service would run and continue the check for B. Nothing would happen for A.

* On a "monthly" day, mdcheck_start.service runs first and deletes "Checked_$A". When mdcheck_continue.service starts later, it will continue checking B at the position saved in "MD_UUID_$B", and start checking A from position 0.

Anyway, I've already closed this PR and I'll send a new one that keeps the services as they used to be. If I've convinced you of this set now, let me know :-)

Otherwise I'll just send a minimal new PR without the controversial patch.

Regards,
Martin