#!/bin/sh
. functions
. variables
DATE=`date +%Y%m%d.%H%m%S`


check_root		# check if we are root

# Install required packages for mirrored disks and logical volumes
#
apt-get -y upgrade
apt-get -y install $INSTALL_PACKAGES
apt-get -y install $INSTALL_PACKAGES2
apt-get -y autoremove


# Create mirror /dev/md0 from 2 partitions,
# add it as /boot to /etc/fstab and 
# update mirror configuration in /etc/mdadm/mdadm.cfg.
# combine $BOOT_DEVICE_1 $BOOT_DEVICE_2 into a mirrored device $INSTALL_BOOT
#
yes | mdadm --create $INSTALL_BOOT --level=1 --raid-devices=2 --assume-clean $INSTALL_BOOTMD1 $INSTALL_BOOT_MD2

# Add new info to /etc/mdadm/mdadm.conf
#
update_mdadm.cfg

# Make sure the system also boots when one of the mirrored boot disks fails.
#
sed 's/BOOT_DEGRADED\=false/BOOT_DEGRADED\=yes/' < /etc/initramfs-tools/conf.d/mdadm > /tmp/mdadm.conf
mv /tmp/mdadm.conf /etc/initramfs-tools/conf.d/mdadm

# Make boot filesystem
#
echo creating boot on $INSTALL_BOOT
mkfs -t ext3 $INSTALL_BOOT
update_fstab /etc/fstab $INSTALL_BOOT /boot ext3 relatime,errors=remount-ro 1 2

# add mirrored disks md[1-9] into a single volume group.
# (boot disk md0 is excluded from logical volumes).
# Then create logical volumes (virtual disks) as needed,
# and update /etc/fstab.
# Initalise all mirrored partitions /dev/md? (except /dev/md0,
# which will be the boot-mirror) as physical volume, ready to
# join a single volume group.
#
for MD in /dev/md[1-9]
do	pvcreate $MD	;done

# Add all /dev/md? (except /dev/md0, who will not join the
# volume group) # into a volume group: a collection of diskblocks,
# regardless their physical location.
#
vgcreate -c n all_disks /dev/md[1-9]

# Read configuration, create a logical volume (virtual disk) for each config line
#
cat $HOME/bin/logical-volumes |
grep -v "^[ 	]*#" | grep -v "^[ 	]*$" |
while read NAME SIZE MOUNTPT TYPE OPTIONS BACKUP CHECK_PRI
do
	if lvcreate --name $NAME --size $SIZE /dev/all_disks
	then
		case "$TYPE" in

		swap)	mkswap /dev/all_disks/$NAME
			;;

		*)	mkfs -t $TYPE /dev/all_disks/$NAME
			;;
		esac
		update_fstab /etc/fstab /dev/all_disks/$NAME $MOUNTPT $TYPE $OPTIONS $BACKUP $CHECK_PRI
		mkdir $MOUNTPT
	fi
done


# Copy data from the root filesystem to the (logical, mirrored)
# new root filesystem and boot filesystem.
#

# No action if creation of mirrored partitions failed.
#
if ! mount /dev/all_disks/root /mnt
then echo Mount /dev/all_disks/root FAILED. ; exit ; fi
mkdir /mnt/boot
if ! mount $INSTALL_BOOT /mnt/boot
then echo Mount $INSTALL_BOOT FAILED. ; exit ; fi



# Copy Root filesystem to logical volume.
echo Copying Root
(
cd /
find . -xdev | cpio -pdum /mnt
# only the root filesystem, not hopping to other disks,
# especially not to /mnt !
)

# May be necessary if /bout is not on /root
echo Copying Boot
(
cd /boot
find . -xdev | cpio -pdum /mnt/boot
# only the root filesystem, not hopping to other disks,
# especially not to /mnt !
)

(
cd /mnt/boot
ln -s vmlinuz-* vmlinuz
ln -s initrd.img-* initrd.img
)


# Update new fstab on the new root filesystem, reflecting the
# situation after reboot on the new boot disk.
#
# Then create a new RAM file system on the mirrored boot partitions.
#

# update fstab reflecting the future situation
#
update_fstab /mnt/etc/fstab $INSTALL_ROOT / ext3 relatime,errors=remount-ro 1 1
update_fstab /mnt/etc/fstab $INSTALL_BOOT /boot ext3 relatime,errors=remount-ro 1 2


echo get UUIDs.
# Get uuid of boot disk
#
INSTALL_ROOT_UUID=`lvol2uuid $INSTALL_ROOT`
INSTALL_BOOT_UUID=`lvol2uuid $INSTALL_BOOT`
UNAME_R=`uname -r` 
set `mount | grep ' / '`
OLD_ROOT_UUID=$1
OLD_ROOT_UUID=`lvol2uuid $OLD_ROOT_UUID`


# Update fstab on NEW root disk.
#
echo update NEW fstab.
update_fstab /mnt/etc/fstab $INSTALL_BOOT /boot ext3 relatime,errors=remount-ro 1 2
update_fstab /mnt/etc/fstab $INSTALL_ROOT / ext3 relatime,errors=remount-ro 1 1


mount --bind /proc /mnt/proc
mount --bind /dev /mnt/dev
mount --bind /var /mnt/var
mount --bind /sys /mnt/sys

# Update Grub menu and update initramfs with changes above.
#
echo create new initramfs
chroot /mnt << THERE
	update-grub
	grub-mkdevicemap
	grub-mkconfig > boot/grub/grub.cfg
	update-initramfs -k `uname -r` -c -t
THERE
for PART in $INSTALL_BOOTMD1 $INSTALL_BOOTMD2 
do
	DISK=`echo $PART | sed s/[0-9]$//`
	echo installing grub on $DISK
	grub-install --root-directory=/ $DISK
done

umount /mnt/sys
umount /mnt/var
umount /mnt/dev
umount /mnt/proc

umount /mnt/boot	# /dev/md0
umount /mnt		# /dev/all_disks/root


