Changeset 300 in MondoRescue for trunk/mindi/analyze-my-lvm


Ignore:
Timestamp:
Jan 11, 2006, 2:20:38 PM (18 years ago)
Author:
bcornec
Message:

merge -r295:299 $SVN_M/branches/2.06

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/mindi/analyze-my-lvm

    r276 r300  
    3636
    3737Die() {
    38     echo "$1" >> /dev/stderr
    39     exit 1
     38    echo "$1" >> /dev/stderr
     39    exit 1
    4040}
    4141
     
    4343
    4444GetValueFromField() {
    45     local res
    46     sed s/'    '/~/ "$1" | 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;};}'
     45    local res
     46    sed s/'    '/~/ "$1" | 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;};}'
    4747}
    4848
    4949
    5050GetLastBit() {
    51     local i res
    52     i=20
    53     res=""
    54     while [ ! "$res" ] ; do
    55     i=$(($i-1))
    56     res=`echo "$1" | cut -d'/' -f$i`
    57     done
    58     echo "$res"
     51    local i res
     52    i=20
     53    res=""
     54    while [ ! "$res" ] ; do
     55        i=$(($i-1))
     56        res=`echo "$1" | cut -d'/' -f$i`
     57    done
     58    echo "$res"
    5959}
    6060
    6161
    6262ProcessLogicalVolume() {
    63     local LV_full_string fname logical_volume volume_group device
    64     LV_full_string=$1
    65     [ ! -e "$1" ] && Die "Cannot find LV file $1"
    66     volume_group=`echo "$LV_full_string" | cut -d'/' -f3`
    67     logical_volume=`echo "$LV_full_string" | cut -d'/' -f4`
    68     if [ $lvmversion = 2 ]; then
    69     device=$LV_full_string
    70     params=`GenerateLvcreateParameters $device`
    71     echo "# lvm lvcreate$params -n $logical_volume $volume_group"
    72     else
    73     fname=/proc/lvm/VGs/$volume_group/LVs/$logical_volume
    74     if [ ! -e "$fname" ] ; then
    75         echo "Warning - cannot find $volume_group's $logical_volume LV file"
    76     else
    77         device=`GetValueFromField $fname "name:"`
    78         params=`GenerateLvcreateParameters $device`
    79         echo "# lvcreate$params -n $logical_volume $volume_group"
    80     fi
    81     fi
     63    local LV_full_string fname logical_volume volume_group device
     64    LV_full_string=$1
     65    [ ! -e "$1" ] && Die "Cannot find LV file $1"
     66    volume_group=`echo "$LV_full_string" | cut -d'/' -f3`
     67    logical_volume=`echo "$LV_full_string" | cut -d'/' -f4`
     68    if [ $lvmversion = 2 ]; then
     69        device=$LV_full_string
     70        params=`GenerateLvcreateParameters $device`
     71    else
     72        fname=/proc/lvm/VGs/$volume_group/LVs/$logical_volume
     73        if [ ! -e "$fname" ] ; then
     74            echo "Warning - cannot find $volume_group's $logical_volume LV file"
     75        else
     76            device=`GetValueFromField $fname "name:"`
     77            params=`GenerateLvcreateParameters $device`
     78        fi
     79    fi
     80    echo "# $LVMCMD lvcreate$params -n $logical_volume $volume_group"
    8281}
    8382
    8483
    8584GenerateLvcreateParameters() {
    86     local device stripes stripesize device fname allocation output readahead
    87     fname=/tmp/PLF.$$.txt
    88     device=$1
    89     output=""
    90     if [ $lvmversion = 2 ]; then
    91     lvm lvdisplay $device > $fname
    92     else
    93     lvdisplay $device > $fname
    94     fi
    95     stripes=`GetValueFromField $fname "Stripes"`
    96     stripesize=`GetValueFromField $fname "Stripe size (MByte)"`m
    97     [ "$stripesize" = "m" ] && stripesize=`GetValueFromField $fname "Stripe size (KByte)"`k
    98     [ "$stripesize" = "k" ] && stripesize=""
    99     allocation=`GetValueFromField $fname "LV Size"`
    100     [ ! "`echo "$allocation" | grep "[k,m,g]"`" ] && allocation="$allocation"m
    101     if echo "$allocation" | grep -x ".*g" > /dev/null 2> /dev/null ; then
    102     val=`echo "$allocation" | sed s/g//`
    103     allocation=`echo "$val" | awk '{c=$1; printf "%d", c*1024;}'`m
    104     fi
    105     readahead=`GetValueFromField $fname "Read ahead sectors"`
    106     rm -f $fname
    107     [ "$stripes" ]    && output="$output -i $stripes"
    108     [ "$stripesize" ] && output="$output -I $stripesize"
    109     [ "$allocation" ] && output="$output -L $allocation"
    110     [ "$readahead" ]  && output="$output -r $readahead"
    111     echo "$output"
     85    local device stripes stripesize device fname allocation output readahead
     86    fname=/tmp/PLF.$$.txt
     87    device=$1
     88    output=""
     89    $LVMCMD lvdisplay $device > $fname
     90    stripes=`GetValueFromField $fname "Stripes"`
     91    stripesize=`GetValueFromField $fname "Stripe size (MByte)"`m
     92    [ "$stripesize" = "m" ] && stripesize=`GetValueFromField $fname "Stripe size (KByte)"`k
     93    [ "$stripesize" = "k" ] && stripesize=""
     94    allocation=`GetValueFromField $fname "LV Size"`
     95    [ ! "`echo "$allocation" | grep "[k,m,g]"`" ] && allocation="$allocation"m
     96    if echo "$allocation" | grep -x ".*g" > /dev/null 2> /dev/null ; then
     97        val=`echo "$allocation" | sed s/g//`
     98        allocation=`echo "$val" | awk '{c=$1; printf "%d", c*1024;}'`m
     99    fi
     100    readahead=`GetValueFromField $fname "Read ahead sectors"`
     101    rm -f $fname
     102    [ "$stripes" ]    && output="$output -i $stripes"
     103    [ "$stripesize" ] && output="$output -I $stripesize"
     104    [ "$allocation" ] && output="$output -L $allocation"
     105    [ "$readahead" ]  && output="$output -r $readahead"
     106    echo "$output"
    112107}
    113108
     
    115110
    116111GenerateVgcreateParameters() {
    117     local current_VG device fname incoming VG_info_file max_logical_volumes max_physical_volumes physical_extent_size output blanklines
    118     current_VG=$1
    119     VG_info_file=/tmp/$$.vg-info.txt
    120     if [ $lvmversion = 2 ]; then
    121     lvm vgdisplay $current_VG > $VG_info_file
    122     else
    123     vgdisplay $current_VG > $VG_info_file
    124     fi
    125     max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
    126     [ $max_logical_volumes -ge 256 ] && max_logical_volumes=255
    127     max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
    128     [ $max_physical_volumes -ge 256 ] && max_physical_volumes=255
    129     physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
    130     output=""
    131     [ "$max_logical_volumes" ]  && output="$output -l $max_logical_volumes"
    132     [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
    133     [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
    134     echo "$output"
    135     rm -f $VG_info_file
     112    local current_VG device fname incoming VG_info_file max_logical_volumes max_physical_volumes physical_extent_size output blanklines
     113    current_VG=$1
     114    VG_info_file=/tmp/$$.vg-info.txt
     115    $LVMCMD vgdisplay $current_VG > $VG_info_file
     116    max_logical_volumes=`GetValueFromField "$VG_info_file" "MAX LV"`
     117    [ $max_logical_volumes -ge 256 ] && max_logical_volumes=255
     118    max_physical_volumes=`GetValueFromField "$VG_info_file" "MAX PV"`
     119    [ $max_physical_volumes -ge 256 ] && max_physical_volumes=255
     120    physical_extent_size=`GetValueFromField "$VG_info_file" "PE Size"`
     121    output=""
     122    [ "$max_logical_volumes" ]  && output="$output -l $max_logical_volumes"
     123    [ "$max_physical_volumes" ] && output="$output -p $max_physical_volumes"
     124    [ "$physical_extent_size" ] && output="$output -s $physical_extent_size"
     125    echo "$output"
     126    rm -f $VG_info_file
    136127}
    137128
     
    141132
    142133ProcessVolumeGroup() {
    143     local current_VG physical_volumes i list_of_devices VG_params
    144     current_VG=$1
    145     if [ $lvmversion = 2 ]; then
    146     VG_params=`GenerateVgcreateParameters $current_VG`
    147     list_of_devices=`lvm pvs | grep "$current_VG" | awk '{print $1}'`
    148     echo "# lvm vgcreate $current_VG$VG_params $list_of_devices"
    149     echo "# lvm vgchange -a y $current_VG"
    150     else
    151     info_file=/proc/lvm/VGs/$current_VG/group
    152     physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
    153     VG_params=`GenerateVgcreateParameters $current_VG`
    154     list_of_devices=""
    155     for i in $physical_volumes ; do
    156         fname=/proc/lvm/VGs/$current_VG/PVs/$i
    157         device=`GetValueFromField $fname "name:"`
    158         list_of_devices="$list_of_devices $device"
    159     done
    160     echo "# vgcreate $current_VG$VG_params$list_of_devices"
    161     echo "# vgchange -a y $current_VG"
    162     fi
     134    local current_VG physical_volumes i list_of_devices VG_params
     135    current_VG=$1
     136    if [ $lvmversion = 2 ]; then
     137        VG_params=`GenerateVgcreateParameters $current_VG`
     138        list_of_devices=`$LVMCMD pvs | grep "$current_VG" | awk '{print $1}'`
     139    else
     140        info_file=/proc/lvm/VGs/$current_VG/group
     141        physical_volumes=`ls /proc/lvm/VGs/$current_VG/PVs`
     142        VG_params=`GenerateVgcreateParameters $current_VG`
     143        list_of_devices=""
     144        for i in $physical_volumes ; do
     145            fname=/proc/lvm/VGs/$current_VG/PVs/$i
     146            device=`GetValueFromField $fname "name:"`
     147            list_of_devices="$list_of_devices $device"
     148        done
     149    fi
     150    echo "# $LVMCMD vgcreate $current_VG$VG_params $list_of_devices"
     151    echo "# $LVMCMD vgchange -a y $current_VG"
    163152}
    164153
     
    166155
    167156ListAllPhysicalVolumes() {
    168     if [ $lvmversion = 2 ]; then
    169     lvm pvscan 2> /dev/null | grep 'PV' | awk '{print $2}'
    170     else
    171     pvscan 2> /dev/null | grep '"' | cut -d'"' -f2
    172     fi
     157    if [ $lvmversion = 2 ]; then
     158        $LVMCMD pvscan 2> /dev/null | grep 'PV' | awk '{print $2}'
     159    else
     160        pvscan 2> /dev/null | grep '"' | cut -d'"' -f2
     161    fi
    173162}
    174163
    175164
    176165ListAllVolumeGroups() {
    177     if [ $lvmversion = 2 ]; then
    178         lvm vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
    179     else
    180         vgdisplay 2> /dev/null | awk '/^VG Name/ {print $3;}'
    181     fi
     166    $LVMCMD vgdisplay 2> /dev/null | awk '/^ *VG Name/ {print $3;}'
    182167}
    183168
    184169
    185170ListLvmDrivesAndPartitions() {
    186     if [ $lvmversion = 2 ]; then
    187     lvm vgdisplay -v |grep "PV Name" | awk '{print $3}'
    188     else
    189     vgdisplay -v |grep "PV Name" | awk '{print $4}'
    190     fi
     171    $LVMCMD vgdisplay -v |grep "PV Name" | awk '{print $3}'
    191172}
    192173
     
    194175
    195176PrettifyList() {
    196     local i
    197     echo -en "$1"
    198     for i in $2 ; do
    199     echo -en "$i "
    200     done
    201     echo ""
     177    local i
     178    echo -en "$1"
     179    for i in $2 ; do
     180        echo -en "$i "
     181    done
     182    echo ""
    202183}
    203184
    204185
    205186ListAllLogicalVolumes() {
    206     if [ $lvmversion = 2 ]; then
    207     lvm lvscan | grep "'" | cut -d"'" -f2
    208     else
    209     lvscan | grep '"' | cut -d'"' -f2
    210     fi
     187    if [ $lvmversion = 2 ]; then
     188        $LVMCMD lvscan | grep "'" | cut -d"'" -f2
     189    else
     190        lvscan | grep '"' | cut -d'"' -f2
     191    fi
    211192}
    212193
     
    214195
    215196WriteShutdownScript() {
    216     local i
    217     echo ""
    218     echo "Finally, to shut down and delete the volumes, do this:-"
    219     if [ $lvmversion = 2 ]; then
     197    local i
     198    echo ""
     199    echo "Finally, to shut down and delete the volumes, do this:-"
    220200    for i in `ListAllLogicalVolumes` ; do
    221         echo "(lvm lvremove -f $i)"
     201        echo "($LVMCMD lvremove -f $i)"
    222202    done
    223203    for i in `ListAllVolumeGroups` ; do
    224         echo "(lvm vgchange -a n $i)"
     204        echo "($LVMCMD vgchange -a n $i)"
    225205    done
    226206    for i in `ListAllVolumeGroups` ; do
    227         echo "(lvm vgremove $i)"
    228     done
    229     echo "(rmmod dm-mod & rmmod dm_mod & )"
    230     else
    231     for i in `ListAllLogicalVolumes` ; do
    232         echo "(lvremove -f $i)"
    233     done
    234     for i in `ListAllVolumeGroups` ; do
    235         echo "(vgchange -a n $i)"
    236     done
    237     for i in `ListAllVolumeGroups` ; do
    238         echo "(vgremove $i)"
    239     done
    240     echo "(rmmod lvm-mod)"
    241     fi
     207        echo "($LVMCMD vgremove $i)"
     208    done
     209    if [ $lvmversion = 2 ]; then
     210        echo "(rmmod dm-mod & rmmod dm_mod & )"
     211    else
     212        echo "(rmmod lvm-mod)"
     213    fi
    242214}
    243215
     
    247219which lvmdiskscan 2>/dev/null 2>&1 || Die "Cannot find lvmdiskscan. Are you sure you're using LVM?"
    248220if [ -e "/proc/lvm/global" ] && [ "`tr -s '\t' ' ' < /proc/lvm/global | grep "0 VGs 0 PVs 0 LVs"`" != "" ] ; then
    249     exit 0
    250 fi
    251 
    252 # Sq-Mod ... V1 has different output. --version is not legal but version
    253 # gets displayed so I guess it's ok... This should work for both.
    254 
    255 #lvmversion=`lvmdiskscan --version | grep "LVM version:" | cut -d: -f2 | cut -d. -f1 | sed -e 's/ //g'`
    256 
    257 lvmversion=`lvmdiskscan --help |
     221    exit 0
     222fi
     223
     224lvmversion=`lvmdiskscan --help 2>&1 |
    258225  grep -E "Logical Volume Manager|LVM version:" |
    259226  cut -d: -f2 | cut -d. -f1 |
     
    262229
    263230if which lvm 2>/dev/null; then
    264     version=`lvm version | grep "LVM version" | awk '{print $3}'`
    265     i="`echo "$version" | cut -d'.' -f1`"
    266     echo "i=$i"
    267     if [ "$i" -ge "2" ] ; then
    268 #    if [ $version >= "2.00" ]; then
    269     lvmversion=2
    270     fi
     231    version=`lvm version | grep "LVM version" | awk '{print $3}'`
     232    i="`echo "$version" | cut -d'.' -f1`"
     233    echo "i=$i"
     234    if [ "$i" -ge "2" ] ; then
     235        lvmversion=2
     236    fi
    271237fi
    272238
    273239if [ $lvmversion = 2 ]; then
    274     echo "LVM version >= 2.0 found."
     240    echo "LVM version >= 2.0 found."
     241    LVMCMD="lvm"
     242else
     243    LVMCMD=""
    275244fi
    276245
     
    281250echo "To get started, type:-"
    282251if [ $lvmversion = 2 ]; then
    283     echo "(insmod dm-mod)"
    284     echo "(insmod dm_mod)"
    285     echo "# lvm vgchange -an"
     252    echo "(insmod dm-mod)"
     253    echo "(insmod dm_mod)"
    286254else
    287     echo "(insmod lvm-mod)"
    288     echo "# vgchange -an"
    289 fi
     255    echo "(insmod lvm-mod)"
     256fi
     257echo "# $LVMCMD vgchange -an"
    290258for i in `ListAllPhysicalVolumes` ; do
    291 #    echo "# dd if=/dev/zero of=$i bs=512 count=1"
    292     if [ $lvmversion = 2 ]; then
    293     echo "# echo y | lvm pvcreate -ff $i"
    294     else
    295     echo "# echo y | pvcreate -ff $i"
    296     fi
    297 done
    298 if [ $lvmversion = 2 ]; then
    299     echo "# lvm vgscan; echo"
    300 else
    301     echo "# vgscan; echo"
    302 fi
     259    echo "# echo y | $LVMCMD pvcreate -ff $i"
     260done
     261echo "# $LVMCMD vgscan; echo"
    303262echo ""
    304263echo "Create and activate the VG's (volume groups)."
    305264all_volume_groups=`ListAllVolumeGroups`
    306265for current_VG in $all_volume_groups ; do
    307     if [ $lvmversion -ne 2 ]; then
    308         echo "# rm -Rf /dev/$current_VG"
    309     fi
    310     ProcessVolumeGroup $current_VG
     266    if [ $lvmversion -ne 2 ]; then
     267        echo "# rm -Rf /dev/$current_VG"
     268    fi
     269    ProcessVolumeGroup $current_VG
    311270done
    312271echo ""
     
    314273all_logical_volumes=`ListAllLogicalVolumes`
    315274for current_LV in $all_logical_volumes ; do
    316     ProcessLogicalVolume $current_LV
     275    ProcessLogicalVolume $current_LV
    317276done
    318277echo ""
    319 if [ $lvmversion = 2 ]; then
    320     echo "# lvm vgscan"
    321 else
    322     echo "# vgscan"
    323 fi
     278echo "# $LVMCMD vgscan"
    324279echo "Now you may format the LV's:-"
    325280for i in `ListAllLogicalVolumes` ; do
    326     echo "(mkfs -t foo $i or something like that)"
     281    echo "(mkfs -t foo $i or something like that)"
    327282done
    328283WriteShutdownScript
Note: See TracChangeset for help on using the changeset viewer.