Changeset 2077 for devel/mindi/bin


Ignore:
Timestamp:
Dec 11, 2008, 12:28:51 PM (11 years ago)
Author:
bruno
Message:

much improved version of analyze-lvm based on man page and -c option usage for LVM commands - still not working - wip

File:
1 edited

Legend:

Unmodified
Added
Removed
  • devel/mindi/bin/analyze-lvm

    r2076 r2077  
    189189print OUTPUT "\n";
    190190
     191print OUTPUT "# Creating Physical Volumes\n";
    191192# Analyze the existing physical volumes
    192 my @lvmpvs = ();
    193 open(LVM,"$lvmcmd pvdisplay |") || mr_exit(-1,"Unable to execute $lvmcmd pvdisplay");
    194 while (<LVM>) {
    195         if (/PV Name/) {
    196                 my $lvmpv = $_;
    197                 $lvmpv =~ s/^\s*PV Name ([A-z0-9/])/$1/;
    198         }
    199         push($lvmpv,@lvmpvs);
    200 }
    201 close(LVM);
    202 
    203 ListAllPhysicalVolumes() {
    204     if [ $lvmversion = 2 ]; then
    205         $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}'
    206     else
    207         pvscan 2> /dev/null | grep '"' | cut -d'"' -f2
    208     fi
    209 }
    210 print OUTPUT "# Creating Physical Volumes\n";
    211 foreach my $pv (@lvmpvs) {
    212     print OUTPUT "echo y | $lvmcmd pvcreate -ff $pv\n";
    213 }
    214 print OUTPUT "\n";
    215 
     193open(LVM,"$lvmcmd pvdisplay -c |") || mr_exit(-1,"Unable to execute $lvmcmd pvdisplay -c");
     194while (<LVM>) {
     195        my ($pvname,$vgname,$pvsize,$ipvn,$pvstat,$pvna,$lvnum,$pesize,$petot,$pefree,$pelloc) = split(/:/);
     196        print OUTPUT "$lvmcmd pvcreate -ff -y -s $pesize $pvname\n";
     197}
     198close(LVM);
     199print OUTPUT "\n";
    216200print OUTPUT "# Scanning again Volume Groups\n";
    217201print OUTPUT "$lvmcmd vgscan\n";
     
    220204# Analyze the existing volume groups
    221205print OUTPUT "# Creating Volume Groups and Activating them\n";
    222 my @lvmvgs = ();
    223 open(LVM,"$lvmcmd vgdisplay |") || mr_exit(-1,"Unable to execute $lvmcmd vgdisplay");
    224 while (<LVM>) {
    225         if (/VG Name/) {
    226                 my $lvmvg = $_;
    227                 $lvmvg =~ s/^\s*VG Name ([A-z0-9/])/$1/;
    228                 #$LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
    229         }
    230         push($lvmvg,@lvmvgs);
    231 }
    232 close(LVM);
    233 
    234 foreach my $vg (@lvmvgs) {
     206open(LVM,"$lvmcmd vgdisplay -c |") || mr_exit(-1,"Unable to execute $lvmcmd vgdisplay -c");
     207while (<LVM>) {
     208    my ($vgname,$vgaccess,$vgstat,$vgnum,$lvmaxnum,$lvnum,$ocalvinvg,$lvmaxsize,$pvmaxnum,$cnumpv,$anumpv,$vgsize,$pesize,$penum,$pealloc,$pefree,$uuid) = split(/:/);
    235209    if ($lvmver < 2) {
    236210        print OUTPUT "# Removing device first as LVM v1 doesn't do it\n";
    237211        print OUTPUT "rm -Rf /dev/$vg\n";
    238212    }
    239     mr_lvm_create_vg($vg);
    240 }
    241 
    242 sub mr_lvm_create_vg {
    243 
    244 my $vg = shift;
    245 
    246 $vg_params=`GenerateVgcreateParameters $vg`
    247 GenerateVgcreateParameters() {
    248     local current_VG device fname incoming VG_info_file max_logical_volumes max_physical_volumes physical_extent_size output blanklines
    249     current_VG=$1
    250     VG_info_file=$MINDI_TMP/$$.vg-info.txt
    251     $LVMCMD vgdisplay $current_VG > $VG_info_file
    252     max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
    253     [ $max_logical_volumes -ge 256 ] && max_logical_volumes=255
    254     max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
    255     [ $max_physical_volumes -ge 256 ] && max_physical_volumes=255
    256     physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
    257     output=""
    258     [ "$max_logical_volumes" ]  && output="$output -l $max_logical_volumes"
    259     [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
    260     [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
    261     echo "$output"
    262     rm -f $VG_info_file
    263 }
    264 
    265 
    266 if ($lvmver = 1) {
    267     $info_file = "/proc/lvm/VGs/$vg/group";
    268     $pvs=`ls /proc/lvm/VGs/$vg/PVs`
    269         $list_of_devices=""
    270         for i in $pvs ; do
    271             fname=/proc/lvm/VGs/$vg/PVs/$i
    272             device=`GetValueFromField $fname "name:"`
    273             $list_of_devices="$list_of_devices $device"
    274         done
    275 } elsif ($lvmver = 2) {
    276         $list_of_devices=`$lvmcmd pvs | grep " $vg " | awk '{print $1}'`
    277 }
    278     print OUTPUT "# Create Volume Group $vg\n";
    279     print OUTPUT "$lvmcmd vgcreate $vg $vg_param $list_of_devices\n";
    280     print OUTPUT "# Activate Volume Group $vg\n";
    281     print OUTPUT "$lvmcmd vgchnge -a y $vg\n";
    282 }
    283 
    284 echo "Finally, create the LV's (logical volumes)."
    285 all_logical_volumes=`ListAllLogicalVolumes`
    286 for current_LV in $all_logical_volumes ; do
    287     ProcessLogicalVolume $current_LV
    288 done
    289 echo ""
    290 echo "# $LVMCMD vgscan"
    291 echo "Now you may format the LV's:-"
    292 for i in `ListAllLogicalVolumes` ; do
    293     echo "(mkfs -t foo $i or something like that)"
    294 done
    295 WriteShutdownScript
    296 exit 0
    297 
    298 
    299 GetValueFromField() {
    300     local res
    301     sed s/'    '/~/ "$1" | tr -s ' ' ' ' | sed s/'~ '/'~'/ | grep -i "$2~" | cut -d'~' -f2,3,4,5 | tr '~' ' ' | gawk '{ if ($2=="MB") {printf "%dm",$1;} else if ($2=="KB") {printf "%dk",$1;} else if ($2=="GB") {printf "%fg",$1;} else {print $0;};}'
    302 }
    303 
    304 
    305 GetLastBit() {
    306     local i res
    307     i=20
    308     res=""
    309     while [ ! "$res" ] ; do
    310         i=$(($i-1))
    311         res=`echo "$1" | cut -d'/' -f$i`
    312     done
    313     echo "$res"
    314 }
    315 
    316 
    317 ProcessLogicalVolume() {
    318     local LV_full_string fname logical_volume volume_group device
    319     LV_full_string=$1
    320     [ ! -e "$1" ] && Die "Cannot find LV file $1"
    321     volume_group=`echo "$LV_full_string" | cut -d'/' -f3`
    322     logical_volume=`echo "$LV_full_string" | cut -d'/' -f4`
    323     if [ $lvmversion = 2 ]; then
    324         device=$LV_full_string
    325         params=`GenerateLvcreateParameters $device`
    326     else
    327         fname=/proc/lvm/VGs/$volume_group/LVs/$logical_volume
    328         if [ ! -e "$fname" ] ; then
    329             echo "Warning - cannot find $volume_group's $logical_volume LV file"
    330         else
    331             device=`GetValueFromField $fname "name:"`
    332             params=`GenerateLvcreateParameters $device`
    333         fi
    334     fi
    335     echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
    336 }
    337 
    338 
    339 GenerateLvcreateParameters() {
    340     local device stripes stripesize device fname allocation output readahead
    341     fname=$MINDI_TMP/PLF.$$.txt
    342     device=$1
    343     output=""
    344     $LVMCMD lvdisplay $device > $fname
    345     stripes=`GetValueFromField $fname "Stripes"`
    346     stripesize=`GetValueFromField $fname "Stripe size (MByte)"`m
    347     [ "$stripesize" = "m" ] && stripesize=`GetValueFromField $fname "Stripe size (KByte)"`k
    348     [ "$stripesize" = "k" ] && stripesize=""
    349     allocation=`GetValueFromField $fname "LV Size"`
    350     [ ! "`echo "$allocation" | grep "[k,m,g]"`" ] && allocation="$allocation"m
    351     if echo "$allocation" | grep -E '^.*g$' > /dev/null 2> /dev/null ; then
    352         val=`echo "$allocation" | sed s/g//`
    353         allocation=`echo "$val" | awk '{c=$1; printf "%d", c*1024;}'`m
    354     fi
    355     readahead=`GetValueFromField $fname "Read ahead sectors"`
    356     rm -f $fname
     213    $lvmaxnum = 255 if ($lvmaxnum > 256);
     214    $pvmaxnum = 255 if ($pvmaxnum > 256);
     215    print OUTPUT "# Create Volume Group $vgname\n";
     216    # Pb sur pesize unite ?
     217    print OUTPUT "$lvmcmd vgcreate $vgname -p $pvmaxnum -s $pesize -l $lvmaxnum $lvmaxnum\n";
     218}
     219close(LVM);
     220print OUTPUT "\n";
     221print OUTPUT "# Activate Volume Group $vg\n";
     222print OUTPUT "$lvmcmd vgchange -ay\n";
     223print OUTPUT "\n";
     224
     225print OUTPUT "# Creating Logical Volumes\n";
     226open(LVM,"$lvmcmd lvdisplay -c |") || mr_exit(-1,"Unable to execute $lvmcmd lvdisplay -c");
     227while (<LVM>) {
     228    my ($lvname,$vgname,$lvaccess,$lvstat,$lvnum,$oclv,$lvsize,$leinlv,$lealloc,$allocpol,$reaadhead,$major,$minor) = split(/:/);
     229    print OUTPUT "# Create Logical Volume $lvname\n";
     230    print OUTPUT "$lvmcmd lvcreate -n $lvname -L $lvsize -r $redahed $vgname\n";
    357231    [ "$stripes" ]    && output="$output -i $stripes"
    358232    [ "$stripesize" ] && output="$output -I $stripesize"
    359     [ "$allocation" ] && output="$output -L $allocation"
    360     [ "$readahead" ]  && output="$output -r $readahead"
    361     echo "$output"
    362 }
    363 
    364 
    365 
    366 
    367 
    368 
    369 ListAllLogicalVolumes() {
    370     if [ $lvmversion = 2 ]; then
    371         $LVMCMD lvscan 2> /dev/null | grep "'" | cut -d"'" -f2
    372     else
    373         lvscan 2> /dev/null | grep '"' | cut -d'"' -f2
    374     fi
    375 }
    376 
    377 
    378 
    379 WriteShutdownScript() {
    380     local i
    381     echo ""
    382     echo "Finally, to shut down and delete the volumes, do this:-"
    383     for i in `ListAllLogicalVolumes` ; do
    384         echo "($LVMCMD lvremove -f $i)"
    385     done
    386     for i in `ListAllVolumeGroups` ; do
    387         echo "($LVMCMD vgchange -a n $i)"
    388     done
    389     for i in `ListAllVolumeGroups` ; do
    390         echo "($LVMCMD vgremove $i)"
    391     done
    392     if [ $lvmversion = 2 ]; then
    393         echo "(rmmod dm-mod & rmmod dm_mod & )"
    394     else
    395         echo "(rmmod lvm-mod)"
    396     fi
    397 }
    398 
    399 
    400 
     233}
     234close(LVM);
     235print OUTPUT "\n";
     236print OUTPUT "# Scanning again Volume Groups\n";
     237print OUTPUT "$lvmcmd vgscan\n";
     238print OUTPUT "\n";
     239
     240WriteShutdownScript
     241mr_exit(0,"End of analyze-lvm");
Note: See TracChangeset for help on using the changeset viewer.