#!/bin/sh
#
# Script:  lvm_setup.sh
# Version: 0.2
#
#   SY - 18th February 2003
#
# This script should set up lvm partitions on your newly installed
# machine, provided you have a nice lvm-enabled kernel, all the lvm
# tools, and stuff for creating filesystems (eg. mkreiserfs).
#
# Default filesystem is reiserfs, but you can change this by editing
# the $MKFS variable to point to the filesystem creation tool of
# your choice.
#
# Note: Must be run as root!
#
#***********************************************************************
# 
#                           DANGER! DANGER!
#
# Do not use this script unless you know *exactly* what it does!
# Only recommended for use on freshly installed systems, as running this
# script may destroy all data on your hard disks. You have been warned!
#
#                            HIGH VOLTAGE!
#
#***********************************************************************
#
# TODO:
#
# * Add checks for existence of lvm utilities and filesystem progs.
# * Add checks to see if the PV, VG or LV already exists, and act
#   accordingly.
# * Let the user decide which filesystem to use.
# * Use case statements to determine user input, instead of all that
#   mucking about with ifs.
#
#
# Modification History
# --------------------
#
# SY  13.05.2003  Version 0.2: Fixed bug - incorrect syntax for vgcreate
#
#-----------------------------------------------------------------------

MKFS='/sbin/mkreiserfs -f'

clear
echo
echo 'Logical Volume Setup - version 0.2'
echo '         (reiserfs only)          '
echo

#-----------------------------------------------------------------------
# Do we want to run vgscan?
#-----------------------------------------------------------------------
done=false
while [ $done = false ]; do
    echo -n "Would you like to run vgscan? [y/n]" >&2
    read VGS
    if [ "$VGS" = n ] || [ "$VGS" = N ]; then
        echo Skipping vgscan
        done=true
    elif [ "$VGS" = y ] || [ "$VGS" = Y ]; then
        echo 'Running vgscan...'
        /sbin/vgscan
        done=true
    fi
done

#-----------------------------------------------------------------------
# Do we want to create a Physical Volume?
#-----------------------------------------------------------------------
done=false
while [ $done = false ]; do
    echo -n "Would you like to create a Physical Volume? [y/n]" >&2
    read CPV
    if [ "$CPV" = n ] || [ "$CPV" = N ]; then
        echo Skipping physical volume creation
        done=true
    elif [ "$CPV" = y ] || [ "$CPV" = Y ]; then
        sure=false
        while [ $sure = false ]; do
            /sbin/fdisk -l
            echo
            echo -n "Where do you want to create the PV? " >&2
            read PVDEV
            echo -n "Creating Physical Volume in $PVDEV. Continue? [y/n]" >&2
            read PVC
            if [ "$PVC" = n ] || [ "$PVC" = N ]; then
                echo Skipping physical volume creation
                sure=true
            elif [ "$PVC" = y ] || [ "$PVC" = Y ]; then
                echo "Creating Physical Volume in $PVDEV..."
                /sbin/pvcreate $PVDEV
                sure=true
            fi
        done
        done=true
    fi
done

#-----------------------------------------------------------------------
# Name the Volume Group to be created in this Physical Volume.
# We have to get the name whether we need to create the VG or not, so we
# ask for it first.
#-----------------------------------------------------------------------
done=false
while [ $done = false ]; do
    echo -n "Please enter the Volume Group name (eg. vg00): " >&2
    read VGN
    echo -n "Create volume group $VGN? (y/n)"
    read VGC
    if [ "$VGC" = n ] || [ "$VGC" = N ]; then
        echo Not creating volume group
        /sbin/vgchange -ay
        done=true
    elif [ "$VGC" = y ] || [ "$VGC" = Y ]; then
        echo "Creating Volume Group in $PVDEV..."
        /sbin/vgcreate $VGN $PVDEV
        done=true
    fi 
done

#-----------------------------------------------------------------------
# Now for some Logical Volume creation...
#
# We want to create a few standard partitions here, but ask the user for
# partition sizes. We'd like to gather all the information before we
# start ripping the disks to shreds :-)
#-----------------------------------------------------------------------
clear
echo
echo "Enter Partition Information"
echo
echo -n 'Enter a size (in MB) for /usr: '  >&2; read USRSIZE
echo -n 'Enter a size (in MB) for /var: '  >&2; read VARSIZE
echo -n 'Enter a size (in MB) for /home: ' >&2; read HOMESIZE
echo -n 'Enter a size (in MB) for /tmp: '  >&2; read TMPSIZE

echo
echo 'Summary:'
echo
echo "/dev/$VGN/usr     $USRSIZE MB"
echo "/dev/$VGN/var     $VARSIZE MB"
echo "/dev/$VGN/home    $HOMESIZE MB"
echo "/dev/$VGN/tmp     $TMPSIZE MB"
echo

#-----------------------------------------------------------------------
# Please confirm your choices :-)
#-----------------------------------------------------------------------
done=false
while [ $done = false ]; do
    echo -n "Do you want to continue? (y/n)" >&2
    read DOIT
    if [ "$DOIT" = n ] || [ "$DOIT" = N ]; then
        echo 'OK. Exiting.'
        exit 2
    elif [ "$DOIT" = y ] || [ "$DOIT" = Y ]; then
        echo 'Here we go...'
        done=true
    fi
done

#-----------------------------------------------------------------------
# OK. We have what we need. Let's go to work...
#-----------------------------------------------------------------------

echo '------------------------------------------------------------------------'
echo
echo "Setting up /usr.."
echo
/sbin/lvcreate -L${USRSIZE}m -nusr $VGN && $MKFS /dev/$VGN/usr \
&& /bin/mv /usr /oldusr && mkdir /usr && /bin/mount /dev/$VGN/usr /usr \
&& chmod 755 /usr && /bin/mv /oldusr/* /usr/ && rmdir /oldusr
echo "Done."

echo '------------------------------------------------------------------------'
echo
echo "Setting up /var..."
echo
/sbin/lvcreate -L${VARSIZE}m -nvar $VGN && $MKFS /dev/$VGN/var \
&& /bin/mv /var /oldvar && mkdir /var && /bin/mount /dev/$VGN/var /var \
&& chmod 755 /var && /bin/mv /oldvar/* /var/ && rmdir /oldvar
echo "Done."

echo '------------------------------------------------------------------------'
echo
echo "Setting up /home..."
echo
/sbin/lvcreate -L${HOMESIZE}m -nhome $VGN && $MKFS /dev/$VGN/home \
&& /bin/mv /home /oldhome && mkdir /home && /bin/mount /dev/$VGN/home /home \
&& chmod 2775 /home && /bin/mv /oldhome/* /home/ && rmdir /oldhome
echo "Done."

echo '------------------------------------------------------------------------'
echo
echo "Setting up /tmp..."
echo
/sbin/lvcreate -L${TMPSIZE}m -ntmp $VGN && $MKFS /dev/$VGN/tmp \
&& /bin/mount /dev/$VGN/tmp /tmp && chmod 1777 /tmp
echo "Done."

exit 0
