Find the answer to your Linux question:
Results 1 to 3 of 3
I was trying to write a bash script which displays me the degraded containers in the software raid as well Example: SWRAID:/usr/sbin# cat /proc/mdstat Personalities : [raid1] md5 : active ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2012
    Posts
    33

    Exclamation Help Required in Bash and Software Raid


    I was trying to write a bash script which displays me the degraded containers in the software raid
    as well

    Example:

    SWRAID:/usr/sbin# cat /proc/mdstat
    Personalities : [raid1]
    md5 : active raid1 sda9[0]
    200242048 blocks [2/1] [U_]

    md4 : active raid1 sda8[0]
    4883648 blocks [2/1] [U_]

    md3 : active raid1 sda7[0]
    20506816 blocks [2/1] [U_]

    md2 : active raid1 sda6[0]
    979840 blocks [2/1] [U_]

    md1 : active (auto-read-only) raid1 sda5[0] sdb5[1]
    7815488 blocks [2/2] [UU]
    resync=PENDING

    md0 : active raid1 sda1[0]
    9767424 blocks [2/1] [U_]

    unused devices: <none>


    Here my script checks for the pattern [U_] and then marks those as degraded.What I want is to display the container names (In this case md0,md2,md3,md4,md5).

    Sudo code:

    RAID_STATUS=`grep "\[.*_.*\]" /proc/mdstat -c`
    This gives me the right count of the degraded containers,but I am not able to display the degraded container names.
    Can anyone help as soon as possible?

  2. #2
    Just Joined!
    Join Date
    May 2012
    Posts
    33
    Something that allows me to check for occurences of pattern [U_] and then output the corresponding containers. So in the above example:
    I need to get the output:
    md0 md2 md3 md4 md5

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,288
    Hi.

    So we need to search for the flag "_" (because it could be _U or U_, etc.), and we need to have captured the previous line. Then we need to isolate the device string on that previous line. Here's one way of doing that:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate search for pattern "_", copy previous, isolate device.
    
    # Infrastructure details, environment, debug commands for forum posts.
    pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && $C grep sed
    
    FILE=${1-data1}
    pl " Input file $FILE (simulates cat /proc/mdstat):"
    cat $FILE
    
    pl " Results:"
    grep -B1 '_' $FILE |
    sed -rn  '/raid/s/(^[^ ]+).*/\1/p'
    
    exit 0
    producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
    bash GNU bash 3.2.39
    grep GNU grep 2.5.3
    sed GNU sed version 4.1.5
    
    -----
     Input file data1 (simulates cat /proc/mdstat):
    Personalities : [raid1]
    md5 : active raid1 sda9[0]
    200242048 blocks [2/1] [U_]
    
    md4 : active raid1 sda8[0]
    4883648 blocks [2/1] [U_]
    
    md3 : active raid1 sda7[0]
    20506816 blocks [2/1] [U_]
    
    md2 : active raid1 sda6[0]
    979840 blocks [2/1] [U_]
    
    md1 : active (auto-read-only) raid1 sda5[0] sdb5[1]
    7815488 blocks [2/2] [UU]
    resync=PENDING
    
    md0 : active raid1 sda1[0]
    9767424 blocks [2/1] [U_]
    
    unused devices: <none>
    
    -----
     Results:
    md5
    md4
    md3
    md2
    md0
    See man pages for details.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •