# Fixes: kernel io hangs during mdcheck/resync 
# (https://bugs.launchpad.net/ubuntu/+source/linux-signed-hwe-5.11/+bug/1942935)

# Causing hangs when starting and stopping mdraid scrubs periodically as it is implemented in 
# Ubuntu 20.04 and above
#
# adapted from https://bugs.launchpad.net/ubuntu/+source/linux-signed-hwe-5.11/+bug/1942935/comments/5
#
# In case of desaster, this may help:
# echo active > /sys/block/md1/md/array_state
# (https://bugs.launchpad.net/ubuntu/+source/linux-signed-hwe-5.11/+bug/1942935/comments/10)


rm /etc/systemd/system/mdcheck_start.timer /etc/systemd/system/mdcheck_start.service
systemctl mask mdcheck_continue.service mdcheck_continue.timer mdcheck_start.service mdcheck_start.timer
cat > /etc/cron.d/mdadm << 'EOF'
#
# cron.d/mdadm -- schedules periodic redundancy checks of MD devices
#
# Copyright © martin f. krafft <email address hidden>
# distributed under the terms of the Artistic Licence 2.0
#

# By default, run at 00:57 on every Sunday, but do nothing unless the day of
# the month is less than or equal to 7. Thus, only run on the first Sunday of
# each month. crontab(5) sucks, unfortunately, in this regard; therefore this
# hack (see #380425).
57 0 * * 0 root if [ -x /usr/share/mdadm/checkarray ] && [ $(date +\%d) -le 7 ]; then /usr/share/mdadm/checkarray --cron --all --idle --quiet; fi
EOF