Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Configure two partitions to work as RAID1.
#!/bin/bash -e
#
# Goal:
# Configure two partitions to work as RAID1.
#
# Prerequisites:
# 1. Run both 05-configure_root_raid_pass*.
# 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 "Create degraded RAID."
mdadm --zero-superblock "$SECOND_APP_PARTITION"
mdadm --create "$APP_ARRAY" --run --level=1 --raid-devices=2 "missing" "$SECOND_APP_PARTITION"
printf "ARRAY\t$APP_ARRAY\tname=$(hostname):3\n" >> /etc/mdadm/mdadm.conf
echo "OK"
echo "Format array $APP_ARRAY."
mkfs.ext2 -L "app" "$APP_ARRAY"
echo "OK"
echo "Copy files over to RAID."
APP_MOUNT=/tmp/app
mkdir --parents "$APP_MOUNT"
mount "$APP_ARRAY" "$APP_MOUNT"
rsync --archive --acls --xattrs --exclude="/lost+found" /app/ "$APP_MOUNT"
umount "$APP_MOUNT"
echo "OK"
echo "Is partition $FIRST_APP_PARTITION not mounted?"
if df | grep --quiet "$FIRST_APP_PARTITION"
then
umount "$FIRST_APP_PARTITION"
fi
echo "OK"
echo "Format partition $FIRST_APP_PARTITION."
mkfs.ext2 "$FIRST_APP_PARTITION"
echo "OK"
echo "Create full RAID."
mdadm --zero-superblock "$FIRST_APP_PARTITION"
mdadm --add "$APP_ARRAY" "$FIRST_APP_PARTITION"
echo "OK"
echo "Modify fstab."
#TODO: this makes assumptions I am not sure I am confortable with
sed --in-place 's/mmcblk0p3/md3/' "/etc/fstab"
echo "OK"
echo "Remount new array."
mount --all
[[ -e /app ]]
echo "Done."
echo
echo "Note: The array $APP_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