Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Configure two partitions (one already done) to work as RAID1.
#!/bin/bash -e
#
# Goal:
# Configure two partitions (one already done) to work as RAID1.
#
# Inspiration:
# http://blog.drewwithers.com/2013/11/raspberry-pi-usb-raid1-root-partition.html
# https://wiki.archlinux.org/index.php/Convert_a_single_drive_system_to_RAID
#
# Prerequisites:
# 1. Run 05-configure_root_raid_pass1.
# 2. Set the following variables:
#
FIRST_SD_CARD=/dev/mmcblk0
#FIRST_BOOT_PARTITION=${FIRST_SD_CARD}p1
FIRST_ROOT_PARTITION=${FIRST_SD_CARD}p2
#FIRST_APP_PARTITION=${FIRST_SD_CARD}p3
SECOND_SD_CARD=/dev/sda
#SECOND_BOOT_PARTITION=${SECOND_SD_CARD}1
SECOND_ROOT_PARTITION=${SECOND_SD_CARD}2
#SECOND_APP_PARTITION=${SECOND_SD_CARD}3
ARRAY=/dev/md
# BOOT cannot be RAIDed
ROOT_ARRAY=${ARRAY}2
#APP_ARRAY=${ARRAY}3
echo "Are we superuser?"
[[ $EUID -eq 0 ]]
echo "OK"
echo "Are we running on the rPi?"
[[ "$(uname --machine)" == arm* ]]
echo "OK"
echo "Are we running from the array $ROOT_ARRAY?"
df --exclude=tmpfs | awk -v PATTERN="$ROOT_ARRAY" '$6 == "/" { if($1 != PATTERN) exit 1 }'
# --exclude-type to shorten list
# -v to pass ROOT_ARRAY from bash into awk script
# $6 == "/" find the root partition and if it is not mounted to ROOT_ARRAY exit with error
echo "OK"
echo "Is partition $FIRST_ROOT_PARTITION not mounted?"
if df | grep --quiet "$FIRST_ROOT_PARTITION"
then
umount "$FIRST_ROOT_PARTITION"
fi
echo "OK"
echo "Format partition $FIRST_ROOT_PARTITION."
mkfs.ext2 "$FIRST_ROOT_PARTITION"
echo "OK"
echo "Create full RAID."
mdadm --zero-superblock "$FIRST_ROOT_PARTITION"
mdadm --add "$ROOT_ARRAY" "$FIRST_ROOT_PARTITION"
echo "Done."
echo
echo "Note: The array $ROOT_ARRAY is now usable, but for the next ~30minutes is being rebuilt. This could"
echo "cause the system to have slower response during this time. You can monitor the progress with"
echo "something like 'watch -n 5 cat /proc/mdstat'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment