Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Configure two partitions (one currently in use) to work as RAID1.
#!/bin/bash -e
#
# Goal:
# Configure two partitions (one currently in use) 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 04-configure_initramfs.
# 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 primary partition $FIRST_ROOT_PARTITION?"
df --exclude=tmpfs | awk -v PATTERN="$FIRST_ROOT_PARTITION" '$6 == "/" { if($1 != PATTERN) exit 1 }'
# --exclude-type to shorten list
# -v to pass FIRST_ROOT_PARTITION from bash into awk script
# $6 == "/" find the root partition and if it is not mounted to FIRST_ROOT_PARTITION exit with error
echo "OK"
echo "Is card $SECOND_SD_CARD plugged in?"
[[ -e "$SECOND_SD_CARD" ]]
echo "OK"
echo "Does partition $SECOND_ROOT_PARTITION exist?"
[[ -e "$SECOND_ROOT_PARTITION" ]]
echo "OK"
# need to install stuff, so:
apt-get update
echo "Install and configure mdadm."
printf "mdadm\tmdadm/initrdstart\tstring\tall" | debconf-set-selections
apt-get -qq install mdadm
echo "OK"
echo "Create degraded RAID."
mdadm --zero-superblock "$SECOND_ROOT_PARTITION"
mdadm --create "$ROOT_ARRAY" --run --level=1 --raid-devices=2 "missing" "$SECOND_ROOT_PARTITION"
printf "ARRAY\t$ROOT_ARRAY\tname=$(hostname):2\n" >> /etc/mdadm/mdadm.conf
echo "OK"
echo "Format array $ROOT_ARRAY."
mkfs.ext2 -L "root" "$ROOT_ARRAY"
echo "OK"
echo "Copy live system over to RAID."
echo "There is no progress indicator, and this could run for several minutes."
apt-get -qq install rsync
ROOT_MOUNT=/tmp/root
mkdir --parents "$ROOT_MOUNT"
mount "$ROOT_ARRAY" "$ROOT_MOUNT"
rsync --archive --acls --xattrs --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/boot/*","/app/*"} / "$ROOT_MOUNT"
echo "OK"
echo "Modify fstab on array $ROOT_ARRAY."
#TODO: this makes assumptions I am not sure I am confortable with
sed --in-place 's/mmcblk0p2/md2/' "$ROOT_MOUNT/etc/fstab"
sed --in-place 's/ext4/ext2/' "$ROOT_MOUNT/etc/fstab"
echo "OK"
echo "Configure boot parameters in cmdline.txt."
#TODO: this makes assumptions I am not sure I am confortable with
sed --in-place 's/mmcblk0p2/md2/' "/boot/cmdline.txt"
sed --in-place 's/ext4/ext2/' "/boot/cmdline.txt"
sed --in-place 's/rootwait/boot_delay=20 rootdelay=10 rootwait/' "/boot/cmdline.txt"
echo "OK"
echo "Requires reboot."
read -n1 -r -p "Press any key to continue, Ctrl-C to abort..." ignore
reboot
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.