#!/bin/sh
#
# pve_software_raid v1.0 - 2013/07/01
# Author: Davide Lucchesi <davide.lucchesi@ams-ix.net>
#
# Configure software raid in a fresh installed Proxmox VE 3.x system
#
levels="0 1 4 5 6 10"
disks=`ls /dev/sd[a-z] | wc -l`
usage() {
echo "Usage: $0 [raid_level]"
echo ""
echo "Supported raid levels: $levels"
echo ""
echo "If not specified, raid_level will be guessed depending on the available disks."
exit 1
}
# basic checks to see if the script can run safely
#
if [ `id -u` != "0" ]; then
echo "ERROR: you need super user privileges to run this script."
echo " ";
usage
fi
if [ ! -e /sbin/mdadm ]; then
echo "ERROR: please execute the following command before running this script:"
echo "\tapt-get update && apt-get dist-upgrade && apt-get install mdadm"
echo " "
usage
fi
# check user parameters
#
if [ $# -gt 1 ]; then
usage
fi
if [ ! -z "$@" ]; then
if [ $1 = '-h' -o $1 = '--help' ]; then
usage
fi
fi
if [ $# -eq 1 ]; then
res=`echo "$levels" | grep $1`
if [ -z "$res" ]; then
usage
else
level=$1
fi
else
# guess suitable raid level
#
if [ $disks -eq 2 ]; then
# 2 disks: mirror
level=1
else
val=`expr $disks % 2`
if [ $val -eq 1 ]; then
# odd number of disks: raid5
level=5
else
# even number of disks: raid10 (performances!!!)
level=10
fi
fi
fi
if [ -e /dev/md0 ]; then
#
# Done with first reboot, move original partitions to the raidsets
#
grub-install /dev/sda --recheck
for i in /dev/sd[b-z] ; do
grub-install $i
done
grub-install /dev/md0
update-grub
update-initramfs -u
# add the old boot partition to the boot raidset
sfdisk -c /dev/sda 1 fd
mdadm --add /dev/md0 /dev/sda1
# migrate the LVM setup to the data raidset
pvcreate /dev/md1
vgextend pve /dev/md1
pvmove /dev/sda2 /dev/md1
vgreduce pve /dev/sda2
pvremove /dev/sda2
sfdisk -c /dev/sda 2 fd
mdadm --add /dev/md1 /dev/sda2
echo "Rebuilding raidsets, it can take a while: check /proc/mdstat for details."
echo "Please wait until the process is complete, then reboot and check how it works."
exit 0
else
#
# First execution, creating raidsets
#
echo "Creating boot raid1 and data raid$level setups on top of $disks disks..."
echo " "
echo -n "Confirm? (y/N) "
read conf
if [ "$conf" != "y" ]; then
echo "Giving up as requested."
exit 1
fi
for i in `ls /dev/sd[a-z] | grep -v sda`; do
sfdisk -d /dev/sda | sfdisk -f $i
sfdisk -c $i 1 fd
sfdisk -c $i 2 fd
done
boot=""
data=""
for i in `ls /dev/sd[a-z] | grep -v sda`; do
boot="$boot $i"1
data="$data $i"2
done
mdadm --create -l 1 -n $disks /dev/md0 missing $boot
mdadm --create -l $level -n $disks /dev/md1 missing $data
mdadm --detail --scan >> /etc/mdadm/mdadm.conf
# Configuring system to boot from the raid1 partitions
#
mkfs.ext3 /dev/md0
mkdir -p /mnt/md0
mount /dev/md0 /mnt/md0
rsync -vua /boot/ /mnt/md0/
umount /mnt/md0
rmdir /mnt/md0
umount /boot
sed -i "s:UUID.*boot:/dev/md0 /boot:" /etc/fstab
mount /boot
echo '#' >> /etc/default/grub
echo '# software raid' >> /etc/default/grub
echo 'GRUB_DISABLE_LINUX_UUID=true' >> /etc/default/grub
echo 'GRUB_PRELOAD_MODULES="raid dmraid"' >> /etc/default/grub
echo raid1 >> /etc/modules
echo raid1 >> /etc/initramfs-tools/modules
echo "Initial raid setup completed, please reboot and type once more"
echo "\t$0 $@"
exit 0
fi