#!/bin/sh

pvcreate -ff $disk0 $disk1 $disk2
vgcreate $vg $disk0 $disk1 $disk2;
# we assume all PVs have the same amount of free space
[ `pvs -o pv_free --noheadings $disk0 $disk1 | uniq | wc -l` = "1" ] || {
	echo "identical PV sizes needed";
	exit 1;
}
# capacity of one pv in MB
formatted=`pvs -o pv_pe_count --noheading $disk0`
# strip whitespace, so we can calculate
capacity=$formatted;
halfcapacity=$(( $capacity / 2 ));

lvcreate -l 1 -n lx1 $vg $disk0
lvcreate -l 1 -n lx2 $vg $disk1

mirrorsize=$(( $halfcapacity - 6 ));
# data will allocated now on disk2 and disk0
lvcreate ${allocparm} -m1 -l ${mirrorsize} -n m1 $vg
lvcreate -l 3 -n lx3 $vg $disk2

# produce a 1 PE hole on the PVs holding the data
lvcreate -l 1 -n l1 $vg $disk2
lvcreate -l 1 -n l2 $vg $disk2
lvcreate -l 1 -n l3 $vg $disk0
lvcreate -l 1 -n l4 $vg $disk0
lvremove -f $vg/l1
lvremove -f $vg/l3

# find minimum amount free space
free=$(( `pvs -o pv_pe_count --noheading $disk0` - `pvs -o pv_pe_alloc_count --noheading $disk0` ));
free1=$(( `pvs -o pv_pe_count --noheading $disk2` - `pvs -o pv_pe_alloc_count --noheading $disk2` ));
if [ "$free1" -lt "$free" ]; then
	free=$free1;
fi

lvchange -an $vg/m1

lvextend -l +$free $vg/m1 $disk0 $disk2
