How to create a RAID array using mdadm

  Uncategorized
# put back together an already created raid 0 striped array of 2 disks:

# be root, install mdadm
sudo su
apt-get install mdadm
apt-get install nfs-common
umount /dev/md0

# stop the array if it exists, or if it's started: 
mdadm --stop /dev/md0

# list drives
lsblk

# create a partition on disk 1
fdisk /dev/sda
n
p
# use type 83 linux, etc.
# do the same on all disks

# create the raid array using raid level 0 (striped)
mdadm --create /dev/md0 --level=0 --raid-devices=2 /dev/sda1 /dev/sdb1

# or, if it's already created and you just want to build it and access it:
# mdadm --build --verbose /dev/md0 --level=0 --raid-devices=2 /dev/sda1 /dev/sdb1

# check the array
mdadm --detail /dev/md0

# If it's a new array, format it. If not, this will DESTROY all your existing data:
mkfs.ext4 -F /dev/md0

# create a mount dir
mkdir -p /mnt/md0

# mount it
mount /dev/md0 /mnt/md0

# create a test file
touch /mnt/md0/hello.txt
df -h /mnt/md0
umount /dev/md0

# must add the current config to the mdadm config for after reboot
mdadm --detail --scan /dev/md0 | sudo tee -a /etc/mdadm/mdadm.conf
cat /etc/mdadm/mdadm.conf
# The end of the file will look like this:
# ARRAY /dev/md0 metadata=1.2 name=abc:0 UUID=82faa9a2:32538cbf:97416a44:053ec37c

# show the space available on the array
df -h -x devtmpfs -x tmpfs

# do this thing and wait, this stores the config for after reboot
update-initramfs -u

# add it to fstab
sh -c "echo `blkid /dev/md0 | awk '{print $2}' | sed 's/\"//g'` /mnt/md0 ext4 defaults 0 0 >> /etc/fstab"

cat /etc/fstab

# try the mount manually once, don't reboot
mount -a
systemctl daemon-reload
df -h /mnt/md0

# if it's a good mount, you can reboot
reboot

# also: useful to check your array if it's got some redundancy, i.e. not raid zero striped
/usr/share/mdadm/checkarray /dev/md0