1

Does anyone have have a script to check when a RAID-rebuild is done and then execute a command?

I am checking the status of /proc/mdadm and when the resync is done, it should execute a command that notifies the user to tell me that its done.

I'm not sure what loop is the best to check when the RAID is done rebuilding and I found no such script on the internet.

1
  • 1
    mdadm --wait /dev/mdX; command Commented Jan 14, 2020 at 11:36

2 Answers 2

2

The relevant part of /proc/mdstat looks like below, where U means a device that is "up", and _ a device that is "down". When it's recovering, the devices show as "down", so we could wait until all devices are "up" again.

md0 : active raid5 sda1[0] sdd1[2] sdb1[1]
     1465151808 blocks level 5, 64k chunk, algorithm 2 [4/3] [UUU_]

So, something like this:

dev=md0
until grep -A1 "^$dev :" /proc/mdstat | grep -q '\[UU*\]' ; do
    sleep 2
done 
run some command here...

(The first grep looks for the given device name, and grep -A1 prints the matching line plus one after. The second looks for a string of Us between the brackets.)

Of course this never checks if a recovery was running in the first place, we'd need smarter logic to interpret the complete output since the number of output lines varies.

0

I'm doing this in ubuntu 18.04LTS, so maybe grep is different, but A1 gives you one line, so your check doesn't work. I changed it to grep -A2 and can now at least return what you're talking about with:

dev=md0
foo=$(grep -A2 "^$dev :" /proc/mdstat);echo $foo

Example output:

md0 : active raid5 sdh1[0] sdg1[1] sdf1[2] sde1[4] 8790399744 blocks super 1.2 level 5, 256k chunk, algorithm 2 [4/4] [UUUU] bitmap: 0/22 pages [0KB], 65536KB chunk

So, I think it should read:

dev=md0
until grep -A2 "^$dev :" /proc/mdstat | grep -q '\[UU*\]' ; do
    sleep 2
done 
Whatever you want to do goes here.

Cheers!

You must log in to answer this question.