#!/bin/sh


#------------------------- ANALYZE-YOUR-LVM ----------------------- Hugo Rabson
# Last modified: Oct 1st, 2001
#------------------------------------------------------------------------------


Die() {
    echo "$1" >> /dev/stderr
    exit 1
}



GetValueFromField() {
    local res
#    cat $1 | grep -i "$2" | tr -s ' ' '\t' | cut -f2
    cat "$1" | sed s/'    '/~/ | tr -s ' ' ' ' | sed s/'~ '/'~'/ | grep -i "$2~" | cut -d'~' -f2,3,4,5 | tr '~' ' ' | gawk '{ if ($2=="MB") {printf "%sm",$1;} else if ($2=="KB") {printf "%sk",$1;} else if ($2=="GB") {printf "%sg",$1;} else {print $0;};}'
}


GetLastBit() {
    local i res
    i=20
    res=""
    while [ ! "$res" ] ; do
	i=$(($i-1))
	res=`echo "$1" | cut -d'/' -f$i`
    done
    echo "$res"
}


ProcessLogicalVolume() {
    local LV_full_string fname logical_volume volume_group device
    LV_full_string=$1
    [ ! -e "$1" ] && Die "Cannot find LV file $1"
    volume_group=`echo "$LV_full_string" | cut -d'/' -f3`
    logical_volume=`echo "$LV_full_string" | cut -d'/' -f4`
#    fname=`find /proc/lvm -name $logical_volume`
    fname=/proc/lvm/VGs/$volume_group/LVs/$logical_volume
    if [ ! -e "$fname" ] ; then
	echo "Warning - cannot find $volume_group's $logical_volume LV file"
    else
	device=`GetValueFromField $fname "name:"`
	params=`GenerateLvcreateParameters $device`
	echo "# lvcreate$params -n $logical_volume $volume_group"
    fi
}


GenerateLvcreateParameters() {
    local device stripes stripesize device fname allocation output readahead
    fname=/tmp/PLF.$$.txt
    device=$1
    output=""
    lvdisplay $device > $fname
    stripes=`GetValueFromField $fname "Stripes"`
    stripesize=`GetValueFromField $fname "Stripe size (MByte)"`m
    [ "$stripesize" = "m" ] && stripesize=`GetValueFromField $fname "Stripe size (KByte)"`k
    [ "$stripesize" = "k" ] && stripesize=""
    allocation=`GetValueFromField $fname "Allocated LE"`
    [ ! "`echo "$allocation" | grep "[k,m,g]"`" ] && allocation="$allocation"m
    readahead=`GetValueFromField $fname "Read ahead sectors"`
    rm -f $fname
    [ "$stripes" ]    && output="$output -i $stripes"
    [ "$stripesize" ] && output="$output -I $stripesize"
    [ "$allocation" ] && output="$output -L $allocation"
    [ "$readahead" ]  && output="$output -r $readahead"
    echo "$output"
}



GenerateVgcreateParameters() {
    local current_VG device fname incoming VG_info_file max_logical_volumes max_physical_volumes physical_extent_size output blanklines
    current_VG=$1
    VG_info_file=/tmp/$$.vg-info.txt
    vgdisplay $current_VG > $VG_info_file
    max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
    max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
    physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
    output=""
    [ "$max_logical_volumes" ]  && output="$output -l $max_logical_volumes"
    [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
    [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
    echo "$output"
    rm -f $VG_info_file
}





ProcessVolumeGroup() {
    local current_VG physical_volumes i list_of_devices VG_params
    current_VG=$1
    info_file=/proc/lvm/VGs/$current_VG/group
    physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
    VG_params=`GenerateVgcreateParameters $current_VG`
    list_of_devices=""
    for i in $physical_volumes ; do
	fname=/proc/lvm/VGs/$current_VG/PVs/$i
	device=`GetValueFromField $fname "name:"`
	list_of_devices="$list_of_devices $device"
    done
    echo "# vgcreate $current_VG$VG_params$list_of_devices"
    echo "# vgchange -a y $current_VG"
}



ListAllPhysicalVolumes() {
    pvscan 2> /dev/null | grep '"' | cut -d'"' -f2
}


ListAllVolumeGroups() {
    vgscan 2> /dev/null | grep '"' | grep volume | cut -d'"' -f2
}


ListLvmDrivesAndPartitions() {
    lvmdiskscan -l | grep /dev | cut -d' ' -f3
}



PrettifyList() {
    local i
    echo -en "$1"
    for i in $2 ; do
	echo -en "$i "
    done
    echo ""
}


ListAllLogicalVolumes() {
    lvscan | grep '"' | cut -d'"' -f2
}



WriteShutdownScript() {
    local i
    echo ""
    echo "Finally, to shut down and delete the volumes, do this:-"
    for i in `ListAllLogicalVolumes` ; do
	echo "(lvremove -f $i)"
    done
    for i in `ListAllVolumeGroups` ; do
	echo "(vgchange -a n $i)"
    done
    for i in `ListAllVolumeGroups` ; do
	echo "(vgremove $i)"
    done
    echo "(rmmod lvm-mod)"
}



# -------------------------------- main -----------------------------------

which lvmdiskscan || Die "Cannot find lvmdiskscan. Are you sure you're using LVM?"
if [ -e "/proc/lvm/global" ] && [ "`cat /proc/lvm/global | tr -s '\t' ' ' | grep "0 VGs 0 PVs 0 LVs"`" != "" ] ; then
    exit 0
fi
all_lvm_drives_and_partitions=`ListLvmDrivesAndPartitions`
echo "Just before you extrapolate mountlist to include RAID partitions,"
echo "extrapolate it to include the following LVM drives and partitions:-"
PrettifyList ">>>>> " "$all_lvm_drives_and_partitions"
echo "To get started, type:-"
echo "(insmod lvm-mod)"
echo "# vgscan"
for i in `ListAllPhysicalVolumes` ; do
    echo "# echo y | pvcreate -ff $i"
done
#PrettifyList "# echo y | pvcreate -ff " "`ListAllPhysicalVolumes`"
echo ""
echo "Create and activate the VG's (volume groups)."
# all_physical_volumes=`ListAllPhysicalVolumes`
all_volume_groups=`ListAllVolumeGroups`
for current_VG in $all_volume_groups ; do
    echo "# rm -Rf /dev/$current_VG"
    ProcessVolumeGroup $current_VG
done
echo ""
echo "Finally, create the LV's (logical volumes)."
all_logical_volumes=`ListAllLogicalVolumes`
for current_LV in $all_logical_volumes ; do
    ProcessLogicalVolume $current_LV
done
echo ""
echo "Now you may format the LV's:-"
for i in `ListAllLogicalVolumes` ; do
    echo "(mkfs -t foo $i or something like that)"
done
WriteShutdownScript
exit 0
