LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-02-2015, 03:07 AM   #1
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466
Blog Entries: 6

Rep: Reputation: 51
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
 
Old 08-02-2015, 03:58 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 08-02-2015, 08:48 AM   #3
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

Rep: Reputation: 51
Thanks. I got it working. http://www.shellcheck.net/ helped me too.
 
Old 08-02-2015, 10:55 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
This site works on people learning from each other, so it would be nice if you would share with others what the solution was
 
Old 08-03-2015, 09:32 AM   #5
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

Rep: Reputation: 51
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?
 
Old 08-03-2015, 11:23 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 08-03-2015, 11:33 AM   #7
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

Rep: Reputation: 51
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.
 
Old 08-03-2015, 02:53 PM   #8
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,978

Rep: Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623
Thanks for the update and solution.
 
Old 08-03-2015, 08:29 PM   #9
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

Rep: Reputation: 51
But still I need to see how to run the while loop in parallel.
Any idea?
 
Old 08-03-2015, 10:05 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
If MegaCLi can preform the tasks in parallel, then simply pass the name of all drives to it instead of using the loop.
 
Old 08-03-2015, 10:25 PM   #11
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

Rep: Reputation: 51
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..

Last edited by your_shadow03; 08-03-2015 at 10:26 PM.
 
Old 08-04-2015, 12:20 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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)
 
Old 08-04-2015, 05:36 AM   #13
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

Rep: Reputation: 51
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?
 
Old 08-04-2015, 08:47 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] RAID 5 with 4 hard disks... array started with only 3 out of 4 disks kikinovak Slackware 9 08-11-2012 06:33 AM
problems adding disks/ raid configuration - intel embedded hardware raid birdmanpdx Linux - Hardware 0 09-23-2010 02:17 PM
Adding disks to RH9 Raid 0 ? netboy_541 Linux - Server 3 01-18-2008 08:05 AM
Adding timestamp to a BASH script thefox Linux - Software 1 11-10-2007 09:29 PM
Checking the status of the physical disks in a RAID hondo Linux - Hardware 2 10-19-2007 01:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration