LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Adding physical RAID disks via BASH script? (https://www.linuxquestions.org/questions/linux-newbie-8/adding-physical-raid-disks-via-bash-script-4175549603/)

your_shadow03 08-02-2015 03:07 AM

Adding physical RAID disks via BASH script?
 
Hello,

I am writing a script which adds a hard disk to existing raid configuration. Here is how the script goes:

Code:


raidlevel=`$MegaCli -LDInfo -Lall -aALL | awk '/RAID Level/' | awk '{print $4}' | cut -c 9`
echo $raidlevel
 drives=`$MegaCli -PDlist -aALL -NoLog | egrep 'Slot|state' | awk '/Slot/{if (x)print x;x="";}{x=(!x)?$0:x" -"$0;}END{print x;}' | sed 's/Firmware state://g'`
 echo $drives
 sleep 5
 option="-r"
 echo "You have chosen to add drives to RAID. Please wait.."
 echo $drives | while IFS=" :" read _ _ slot _ status; do if [[ $status != "Online, Spun Up" ]]; then aslot=${slot%:}; `$MegaCli -LDRecon -Start -r$raidlevel -Add -PhysDrv[$ENCLOSURE:$aslot] -l0 -a0` ; fi; done
exit
fi

O/P:

5
Slot Number: 0 - Online, Spun Up Slot Number: 1 - Online, Spun Up Slot Number: 2 - Online, Spun Up Slot Number: 3 - Online, Spun Up Slot Number: 4 - Unconfigured(good), Spun Up Slot Number: 12 - Unconfigured(good), Spun Up
You have chosen to add drives to RAID. Please wait..
./lsi.sh: line 109: $'\r': command not found
[root@localhost ~]#

Actually if I run manually the command look like:

[root@testmachine ~]# /usr/sbin/MegaCli -LDRecon -Start -r5 -Add -PhysDrv[32:3] -l0 -a0

Start Reconstruction of Virtual Drive Success.

Exit Code: 0x00

All it does is adding the newly hard drive to RAID.
Please suggest what is correct way with this code:

Code:

echo $drives | while IFS=" :" read _ _ slot _ status; do if [[ $status != "Online, Spun Up" ]]; then aslot=${slot%:}; `$MegaCli -LDRecon -Start -r$raidlevel -Add -PhysDrv[$ENCLOSURE:$aslot] -l0 -a0` ; fi; done

grail 08-02-2015 03:58 AM

Well we are obviously only getting a snippet, as error is at line 109, but my suggestion would either be to turn on debugging (set -xv) or echo the command prior to calling.

There are 2 issues I can see straight off though:

1. Your read is already using IFS=' :' so then the following would be pointless as there would be no ':' in the variable:
Code:

aslot=${slot:}
2. There is no use for `` when calling the command as you are not passing the output of the command to anything.

On a side note, you could re-write nearly all you have shown to be much tidier as there is almost never a reason to use multiple awk's on a single line and also, awk can do all the jobs of sed, grep and cut

your_shadow03 08-02-2015 08:48 AM

Thanks. I got it working. http://www.shellcheck.net/ helped me too.

grail 08-02-2015 10:55 AM

This site works on people learning from each other, so it would be nice if you would share with others what the solution was :)

your_shadow03 08-03-2015 09:32 AM

grail,

The code which worked is here
Code:


 echo "$drives" | while IFS=" :" read -r _ _ slot _ status; do if [[ $status != "Online, Spun Up" ]]; then slot=${slot}; $MegaCli -LDRecon -Start -r"$raidlevel" -Add -PhysDrv[$ENCLOSURE:$slot] -l0 -a0 ; fi; done
exit
fi

But the problem now is the above script tried to reconstruct and rebuild the newly added disk one after another. If I have added two hard drive of 146GB it will take almost double the time to get it rebuilt.
Is there any way I can run it in parallel. Say, if I add two hard disk, both of them should start reconstructed parallel.

How will the code look like?

grail 08-03-2015 11:23 AM

I am not familiar with MegaCli, so you may have to look at the man page or web site to see if it can perform the tasks you need.

your_shadow03 08-03-2015 11:33 AM

Its not a question about MegaCLi but how to make while loop run parallel.
I read about xargs tool which can be helpful but not sure about it.

jefro 08-03-2015 02:53 PM

Thanks for the update and solution.

your_shadow03 08-03-2015 08:29 PM

But still I need to see how to run the while loop in parallel.
Any idea?

grail 08-03-2015 10:05 PM

If MegaCLi can preform the tasks in parallel, then simply pass the name of all drives to it instead of using the loop.

your_shadow03 08-03-2015 10:25 PM

A General way of adding newly added drives to the existing RAID is shown below:
Code:


 /usr/sbin/MegaCli -LDRecon -Start -r5 -Add -PhysDrv[32:3] -l0 -a0

Now user has to be manually detect which enclosure: slot has been added and then run the above command.

While I put it into script it looks like as shown below:

Code:

echo $drives | while IFS=" :" read _ _ slot _ status; do if [[ $status != "Online, Spun Up" ]]; then aslot=${slot:}; `$MegaCli -LDRecon -Start -r$raidlevel -Add -PhysDrv[$ENCLOSURE:$aslot] -l0 -a0` ; fi; done
Now if I add 10 drives so I dont want to run 10 commands for it. My script will use while loop to know which all drives shows added like shown below:
Code:

[root@localhost ~]# ./raidconfig.sh drives
Slot Number: 0 - Online, Spun Up
Slot Number: 1 - Online, Spun Up
Slot Number: 2 - Online, Spun Up
Slot Number: 3 - Online, Spun Up
Slot Number: 4 - Unconfigured(good)
Slot Number: 12 - Unconfigured(good)
Slot Number: 6 - Unconfigured(good)
Slot Number: 7 - Unconfigured(good)
Slot Number: 9 - Unconfigured(good)
Slot Number: 10 - Unconfigured(good)
Slot Number: 5 - Unconfigured(good)

So thats the reason why I want to run this in parallel. As of now, the script chooses the first slot 4 then it runs it and completes it.
Then it chooses slot 12 and then it completes it before moving to 6, next 7 and so on..

grail 08-04-2015 12:20 AM

So what I understand then, is that MegaCli cannot perform the task in parallel. Are you certain that if you call the command on more than one disk at a time that this is ok? ie. You do not
need to add them one at a time?

If so you could try pushing each call into the background using '&' but I would probably confirm first that this is good practice and will not cause any potential issues.

There is also a tool called 'parallel' which you might look into as another alternative (with the same caveat as above)

your_shadow03 08-04-2015 05:36 AM

Thanks for the response.
I read the manual and found that we can pass multiple enclosure:slot combination as shown below:

Code:

MegaCli -LDRecon -Start -r5 -Add -PhysDrv[32:3, 32:4, 32:5] -l0 -a0
Now I tried putting it in script shown below:

Code:

echo "$drives" | while IFS=" :" read -r _ _ slot _ status; do if [[ $status != "Online, Spun Up" ]]; then slot=$slot+${,$ENCLOSURE:$slot}; fi; done
$MegaCli -LDRecon -Start -r"$raidlevel" -Add -PhysDrv[$slot] -l0 -a0
exit

It is throwing error:

./newraidconfig.sh: line 109: $slot+${,$ENCLOSURE:$slot%}: bad substitution
Invalid input at or near token -l

Exit Code: 0x01

I suspect slot=$slot+${,$ENCLOSURE:$slot%}; is incorrect.
Any idea?

grail 08-04-2015 08:47 AM

Yes it is, but you would have to explain what you are trying to do as none of that makes any sense.

Maybe you could also show me what is in the variable 'drives' as I am not sure what the passed in data looks like?

On a personal note, I am also not a fan of using the single line for what is a complicated while loop ... to me it just looks a bit cluttered


All times are GMT -5. The time now is 04:03 PM.