#!/bin/sh help() { echo "usage: mkraid --id array_id --name array_name --level raid_level" \ "--devices 'raid_device_1 raid_device_2 ...'" \ "--spares 'spare_device_1 spare_device_2 ...'" echo "\n-i, --id:\tid of RAID array as a number up to 127" echo "-n, --name:\tname of RAID array" echo "-l, --level:\tRAID level" echo "-d, --devices:\tdevices in RAID array - quoted and space separated" echo "-s, --spares:\thot spare devices - quoted and space separated if multiple" echo "\nexample: mkraid --id 0 --name arr1 --level 5 --devices"\ "'/dev/sda /dev/sdb /dev/sdc' --spares /dev/sdd" exit } opts=$(getopt -o "i:,n:,l:,d:,s:h" -l "id:,name:,level:,devices:,spares:,help" -- "$@") eval set -- "$opts" id= name= level= devices= spares= while true do case "$1" in '-i' | '--id') id="$2" shift 2; continue ;; '-n' | '--name') name="$2" shift 2; continue ;; '-l' | '--level') level="$2" shift 2; continue ;; '-d' | '--devices') devices="$2" shift 2; continue ;; '-s' | '--spares') spares="$2" shift 2; continue ;; '-h' | '--help') help ;; '--') shift; break ;; esac done [ -z "$id" ] && help [ -z "$name" ] && help [ -z "$level" ] && help [ -z "$devices" ] && help [ -z "$spares" ] && help numdevs=$(echo $devices | tr ' ' '\n' | wc -l) numspare=$(echo $spares | tr ' ' '\n' | wc -l) mdadm --create /dev/md$id --level=$level --raid-devices=$numdevs $devices --spare-devices=$numspare $spares uuid="$(mdadm --detail /dev/md0 | grep UUID | tr -d '[:space:]' | cut -d ':' -f 2-)" echo "ARRAY /dev/md$id metadata=1.2 UUID=$uuid name=$name" >> /etc/mdadm/mdadm.conf