#!/bin/bash # Find the largest device for drive in "$@"; do # Count partitions on drive parts=${drive}?* # Unpartitioned devices are treated like one partition [[ -z "$parts" ]] && { todo+=" $drive"; continue; } count=$(echo $parts | wc -w) # Check if this is the largest device so far if [[ $count -gt $lgcount ]]; then # Add previous largest to the processing list [[ -n "$largest" ]] && todo+=" $largest" # Set new largest to the partitions in this device largest=$parts lgcount=$count else # Add this device's partitions to the processing list todo+=" $parts" fi done # Ensure at least one device is partitioned [[ -z "$largest" ]] && { echo 'Error: Partition at least one device or use mdadm directly' exit 1 } # The number of arrays is the number of partitions on the largest device arrays=($largest) # The other partitions are laid down like bricks, distributed among the arrays i=0 for part in $todo; do arrays[$i]+=" $part" let i=i+1 [[ $i -ge ${#arrays[@]} ]] && i=0 done # Create the arrays i=1 for array in "${arrays[@]}"; do mdadm --create /dev/md/${NAME-raid5}-$i -R \ -n $(echo $array | wc -w) -l raid5 -k ppl $array || exit 1 let i=i+1 done