2009-10-12 17:36:19 +01:00
|
|
|
{ config, pkgs, ... }:
|
2009-03-06 12:27:30 +00:00
|
|
|
|
|
|
|
###### implementation
|
2007-01-11 00:40:28 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
|
2007-01-23 20:09:06 +00:00
|
|
|
tempConf = "/var/run/mdadm.conf";
|
2010-05-08 18:18:16 +01:00
|
|
|
tempStatus = "/var/run/mdadm.status";
|
2009-12-29 10:04:54 +00:00
|
|
|
logFile = "/var/log/mdadmEvents.log";
|
2009-03-06 12:27:30 +00:00
|
|
|
modprobe = config.system.sbin.modprobe;
|
2009-12-29 21:23:22 +00:00
|
|
|
inherit (pkgs) mdadm diffutils;
|
2007-01-11 00:40:28 +00:00
|
|
|
|
2009-12-29 10:04:54 +00:00
|
|
|
mdadmEventHandler = pkgs.writeScript "mdadmEventHandler.sh" ''
|
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
echo "$@" >> ${logFile}
|
|
|
|
case $1 in
|
|
|
|
(NewArray)
|
2009-12-29 14:10:05 +00:00
|
|
|
initctl emit -n new-raid-array
|
2009-12-29 10:04:54 +00:00
|
|
|
;;
|
|
|
|
(*) ;;
|
|
|
|
esac
|
|
|
|
'';
|
|
|
|
|
2007-01-11 00:40:28 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
2009-12-29 14:10:05 +00:00
|
|
|
jobs.swraid = {
|
|
|
|
startOn = [ "new-raid-array" ];
|
|
|
|
description = "Assemble RAID arrays.";
|
|
|
|
|
|
|
|
script = ''
|
|
|
|
# Load the necessary RAID personalities.
|
|
|
|
# !!! hm, doesn't the kernel load these automatically?
|
|
|
|
for mod in raid0 raid1 raid5; do
|
|
|
|
${modprobe}/sbin/modprobe $mod || true
|
|
|
|
done
|
|
|
|
|
2010-05-08 18:18:16 +01:00
|
|
|
# Wait until we can fetch the status of mdadm devices.
|
|
|
|
while ! test -e /proc/mdstat; do
|
|
|
|
sleep 10
|
|
|
|
done
|
2009-12-29 14:10:05 +00:00
|
|
|
|
|
|
|
# Scan /proc/partitions for RAID devices.
|
|
|
|
${mdadm}/sbin/mdadm --examine --brief --scan -c partitions > ${tempConf}
|
|
|
|
|
2010-05-16 17:36:01 +01:00
|
|
|
# If there is some array to assemble and if the status has changed.
|
|
|
|
if test -e /proc/mdstat -a -s ${tempConf} &&
|
|
|
|
! ${diffutils}/bin/diff -q /proc/mdstat ${tempStatus} > /dev/null; then
|
2009-12-29 14:10:05 +00:00
|
|
|
|
2010-05-08 18:18:16 +01:00
|
|
|
# Keep the previous status to watch changes.
|
|
|
|
cp ${tempStatus} ${tempStatus}.old
|
|
|
|
|
|
|
|
try=0
|
|
|
|
# Loop until the status change.
|
|
|
|
while ${diffutils}/bin/diff -q ${tempStatus} ${tempStatus}.old > /dev/null; do
|
|
|
|
test "$try" -gt 12 && break
|
|
|
|
test "$try" -ne 0 && sleep 10
|
|
|
|
|
|
|
|
# Activate each device found.
|
|
|
|
${mdadm}/sbin/mdadm --assemble -c ${tempConf} --scan
|
2009-12-29 14:10:05 +00:00
|
|
|
|
2010-05-08 18:18:16 +01:00
|
|
|
# Register the new status
|
|
|
|
cp /proc/mdstat ${tempStatus}
|
|
|
|
|
|
|
|
try=$(($try + 1))
|
|
|
|
done
|
|
|
|
|
|
|
|
if test "$try" -le 6; then
|
|
|
|
# Send notifications.
|
|
|
|
initctl emit -n new-devices
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Remove previous status.
|
|
|
|
rm ${tempStatus}.old
|
|
|
|
fi
|
2009-12-29 14:10:05 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
task = true;
|
|
|
|
};
|
2009-10-12 17:36:19 +01:00
|
|
|
|
2009-12-29 10:04:54 +00:00
|
|
|
jobs.swraidEvents = {
|
|
|
|
name = "swraid-events";
|
|
|
|
description = "Watch mdadm events.";
|
|
|
|
startOn = [ "startup" ];
|
2009-12-29 14:10:05 +00:00
|
|
|
|
|
|
|
postStart = ''
|
|
|
|
echo > ${tempConf}
|
2010-05-08 18:18:16 +01:00
|
|
|
echo > ${tempStatus}
|
2009-12-29 14:10:05 +00:00
|
|
|
|
|
|
|
# Assemble early raid devices.
|
|
|
|
initctl emit -n new-raid-array
|
|
|
|
'';
|
|
|
|
|
|
|
|
# !!! Someone should ensure that this is indeed doing what the manual says.
|
2009-12-29 10:04:54 +00:00
|
|
|
exec = "${mdadm}/sbin/mdadm --monitor --scan --program ${mdadmEventHandler}";
|
|
|
|
};
|
|
|
|
|
2007-01-11 00:40:28 +00:00
|
|
|
}
|