Create Linux software RAID from command line

create software raid

apt-get install -y mdadm parted --no-install-recommends
 
# view discs
lshw -short -c disc
 
# old 
for DEV in sda sdb; do
   umount /dev/${DEV}1
   parted /dev/${DEV} -s rm 1
   #parted /dev/${DEV} -s rm 2
   #dd if=/dev/zero of=/dev/${DEV} bs=1M count=1
   parted /dev/${DEV} -s mklabel msdos
   parted /dev/${DEV} -s mkpart primary 1 256G
   #parted /dev/sda -s mkpart primary 256G 128G
done
 
# new
for DEV in /dev/sd{a..b}; do
    parted ${DEV} -s rm 1
    parted ${DEV} -s mklabel msdos
    parted ${DEV} -s mkpart primary 2048s 100%
    parted ${DEV} -s set 1 raid on
done
 
 
# --assume-clean 
mdadm --create /dev/md0 --name=backup --auto=yes --level=1 --raid-devices=2 /dev/sd{a..b}1
 
mkfs.ext4 -L backup /dev/disk/by-label/backup

CLI

# view partitions
lsblk
 
# create data partition
mdadm --create --verbose /dev/md0 --auto=yes --metadata=0.90 --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1
 
# raid10 - quick init
mdadm --create --assume-clean --verbose /dev/md10 --auto=yes --level=10 --raid-devices=8 /dev/sd{a..h}1
 
# SSD + HDD RAID
mdadm --create /dev/md1 --level=1 --raid-devices=2 --bitmap=internal --write-behind=1024 /dev/sda1 --write-mostly /dev/sdb2
# echo writemostly > /sys/block/md1/md/dev-sdd2/state
 
mdadm --readwrite /dev/md0
mkfs.ext3 /dev/md0
mount /dev/md0 /mnt
 
# create swap partition
mdadm --create --verbose /dev/md1 --auto=yes --metadata=0.90 --level=1 --raid-devices=2 /dev/sda2 /dev/sdb2 
mkswap /dev/md1
swapon /dev/md1
 
# wait
echo "Wait util soft raid is build"
while [ $(cat /proc/mdstat | grep resync | wc -l) -gt 0 ]; do
    sleep 1
    echo -n .
done
echo "DONE"
 
# configure verifiy speed
echo 2048 > /proc/sys/dev/raid/speed_limit_min
echo 1048576 > /proc/sys/dev/raid/speed_limit_max
 
# stop raid
mdadm --stop /dev/md2
mdadm --zero-superblock /dev/sd{a..b}1
mdadm --zero-superblock /dev/nvme1n1p1 /dev/nvme0n1p1
 
# check
#cd /sys/block/md0/md
#echo check >sync_action
#watch -n1 cat /proc/mdstat
 
# default cronjob: /etc/cron.d/mdadm
/usr/share/mdadm/checkarray --cron --all --idle --quiet
 
# change  rebuilding speed limit
cat /proc/sys/dev/raid/speed_limit_max
sysctl -w dev.raid.speed_limit_min=value
 
# start software RAID with LVM
mdadm --examine --scan
# mdadm --examine --scan | grep "md/1" >> /etc/mdadm/mdadm.conf 
 
mdadm --detail /dev/md1
mdadm -A /dev/md1
vgchange -a y vg2
 
 
# Create Linux software RAID
mdadm --create /dev/md2 --level=raid5 --raid-devices=4 --spare-devices=0 /dev/sdb4
/dev/sdc4 /dev/sdd4
 
# replace device
mdadm --remove /dev/md0 /dev/sdb1
mdadm --zero-superblock /dev/sdb1
mdadm --manage /dev/md0 --add /dev/sdb1
 
# start raid
mdadm --assemble --scan -v

Mount inactive / broken RAID

mdadm --manage /dev/md1  --run
mount /dev/md1 /mnt/

Links
http://panticz.de/Rebuild-Linux-software-RAID
http://www.cyberciti.biz/tips/linux-raid-increase-resync-rebuild-speed.html
http://sysadmin.compxtreme.ro/mdadm-cheat-sheet/