# 这是一个基于SHELL的Linux LVM管理工具.类似于AIX下的SMIT.当然,它只能管理LVM.
# 此工具当前版本为1.0.实现了如下功能:
物理卷模块的功能:创建,移除,更改,扫描,查看.
卷组模块的功能:创建,移除,更改,扫描,查看,合并,迁移.
逻辑卷模块的功能:创建,移除,更改,扫描,查看,快照.
# 已在Red Hat Enterprise Linux 5.4 64bit上测试通过.
# 脚本可通过附件下载
#!/bin/bash
# Version 1.0
# AUTHOR: Xin23 http://weibo.com/231988
Tab()
{
printf \\t
}
NextLine()
{
echo ' '
}
EchoTitle()
{
NextLine
if [ "$1" == "newline" ]
then echo "--------$TITLE-----------------------------------------"
NextLine
NextLine
else
echo "--------$TITLE-----------------------------------------"
fi
}
ErrorInput()
{
NextLine
echo "$(printf \\t)Warning:$(printf \\t)Invalid Value,Please Retype!"
NextLine
}
ReadError()
{
NextLine
read -p "$(printf \\t)Press Any Key Plus Enter To Return:$(printf \\t)" ErrorVar
NextLine
}
ChooseDisk()
{
NextLine
Tab
read -p "Please Choose Disk:$(printf \\t)(Example: /dev/sdc)$(printf \\t)" PARAMETER
NextLine
}
ChoosePV()
{
NextLine
Tab
read -p "Please Choose Physical Volumn:$(printf \\t)(Example: /dev/sdb)$(printf \\t)" PVVAR
NextLine
}
ChooseVG()
{
NextLine
Tab
read -p "Please Choose Volumn Group:$(printf \\t)(Example: datavg)$(printf \\t)" VGVAR
NextLine
}
ChooseLV()
{
NextLine
Tab
read -p "Please Choose Logical Volumn:$(printf \\t)(Example: /dev/datavg/datalv)$(printf \\t)" LVVAR
NextLine
}
GetChoice()
{
NextLine
Tab
read -p "Input Your Choice:$(printf \\t)" CHOICE
NextLine
}
RunCommand()
{
if [ $CHOICE == y ]
then $COMMAND $ADDTIONAL_OPTIONS $PARAMETER
elif [ $CHOICE == n ]
then LVMMain
else
VerifyOperate
fi
if [ $? -ne 0 ]
then exit 1
fi
}
VerifyOperate()
{
NextLine
echo "$(printf \\t)Attention:$(printf \\t)You Will Run Command:$(printf \\t) $COMMAND $ADDTIONAL_OPTIONS $PARAMETER"
NextLine
echo "$(printf \\t)Continue$(printf \\t)...... y
$(printf \\t)Return$(printf \\t)$(printf \\t)...... n"
GetChoice
NextLine
RunCommand
}
UnsetVar()
{
unset COMMAND ADDTIONAL_OPTIONS PARAMETER PVVAR VGVAR LVVAR TITLE PVNAME VGNAME PESIZE DestinationVG SourceVG VGSTATUS LVNAME LVNAMEVAR
unset LVOPTION LENUM LVSIZE SIZE MIRRORNUM STRIPESIZE STRIPEWIDTH MIRRORVAR STRIPEVAR LVSIZECreateType PVSTATUS LVSTATUS
}
PVChange()
{
echo "$(printf \\t)Which Operate You Want To do ?"
NextLine
echo "$(printf \\t)Enable Allocatable$(printf \\t)...... y
$(printf \\t)Disable Allocatable$(printf \\t)...... n"
GetChoice
if [ $CHOICE == y ]
then ADDTIONAL_OPTIONS='-x y'
PVSTATUS=Enable
TITLE='Enable Physical Volumn'
EchoTitle newline
elif [ $CHOICE == n ]
then ADDTIONAL_OPTIONS='-x n'
PVSTATUS=Disable
TITLE='Disable Physical Volumn'
EchoTitle newline
else
ErrorInput
ReadError
PV
fi
}
PVShow()
{
echo "$(printf \\t)Which Physical Volumn You Want To Show ?"
NextLine
echo "$(printf \\t)All Physical Volumn$(printf \\t)...... 1
$(printf \\t)Single Physical Volumn$(printf \\t)...... 2"
GetChoice
if [ $CHOICE -eq 1 ]
then clear
pvdisplay
elif [ $CHOICE -eq 2 ]
then EchoTitle newline
ChooseDisk
clear
pvdisplay $PARAMETER
else
ErrorInput
ReadError
PV
fi
}
MigratePE()
{
read -p "$(printf \\t)Please Input Source Physical Volumn:$(printf \\t)" SourcePV
NextLine
read -p "$(printf \\t)Please Input Destination Physical Volumn:$(printf \\t)" DestinationPV
NextLine
COMMAND=pvmove
PARAMETER="$SourcePV $DestinationPV"
echo "$(printf \\t)Attention:$(printf \\t)Extents on Physical Volumn $SourcePV Will Move To $DestinationPV"
VerifyOperate
}
PV()
{
UnsetVar
clear
TITLE='Physical Volumn Menu'
EchoTitle
ShowPVMenu()
{
echo "
$(printf \\t)Physical Volumn Menu
$(printf \\t)Input Number To Choose!
$(printf \\t)Create Physical Volumn$(printf \\t)...... 1
$(printf \\t)Remove Physical Volumn$(printf \\t)...... 2
$(printf \\t)Change Physical Volumn$(printf \\t)...... 3
$(printf \\t)Show Physical Volumn$(printf \\t)...... 4
$(printf \\t)Scan Physical Volumn$(printf \\t)...... 5
$(printf \\t)Migrate Physical Extent$(printf \\t)...... 6
$(printf \\t)Exit $(printf \\t)...... 7"
NextLine
}
ShowPVMenu
GetChoice
case $CHOICE in
"1")
TITLE='Create Physical Volumn'
EchoTitle newline
COMMAND=pvcreate
ChooseDisk
echo "$(printf \\t)Attention:$(printf \\t)Disk $PARAMETER Will Convert To Physical Volumn"
VerifyOperate
;;
"2")
TITLE='Remove Physical Volumn'
EchoTitle newline
COMMAND=pvremove
ChooseDisk
echo "$(printf \\t)Attention:$(printf \\t)Disk $PARAMETER Will Be Remove From Physical Volumn"
VerifyOperate
;;
"3")
TITLE='Change Physical Volumn'
EchoTitle newline
COMMAND=pvchange
PVChange
ChooseDisk
echo "$(printf \\t)Attention:$(printf \\t)Physical Volumn $PARAMETER Will $PVSTATUS"
VerifyOperate
;;
"4")
TITLE='Show Physical Volumn'
EchoTitle newline
PVShow
;;
"5")
TITLE='Scan Physical Volumn'
clear
EchoTitle newline
pvscan
;;
"6")
TITLE='Migrate Physical Extent'
EchoTitle newline
MigratePE
;;
"7")
LVMMain
;;
* )
ErrorInput
ReadError
PV
;;
esac
exit 0
}
VGChange()
{
echo "$(printf \\t)Which Operate You Want To do ?"
NextLine
echo "$(printf \\t)Extend Volumn Group$(printf \\t)...... 1
$(printf \\t)Reduce Volumn Group$(printf \\t)...... 2
$(printf \\t)Active Volumn Group$(printf \\t)...... 3
$(printf \\t)Inactive Volumn Group$(printf \\t)...... 4"
GetChoice
case $CHOICE in
"1")
TITLE='Extend Volumn Group'
EchoTitle newline
COMMAND=vgextend
read -p "$(printf \\t)Which Volumn Group You Want To Extend?$(printf \\t)(Example: datavg)$(printf \\t)" VGVAR
NextLine
read -p "$(printf \\t)Which Physical Volumn You Want To Add?$(printf \\t)(Example: /dev/sdb)$(printf \\t)" PVVAR
PARAMETER="$VGVAR $PVVAR"
VGSTATUS=Extend
;;
"2")
TITLE='Reduce Volumn Group'
EchoTitle newline
COMMAND=vgreduce
read -p "$(printf \\t)Which Volumn Group You Want To Reduce ?$(printf \\t)(Example: datavg)$(printf \\t)" VGVAR
NextLine
read -p "$(printf \\t)Which Physical Volumn You Want To Remove ?$(printf \\t)(Example: /dev/sdb)$(printf \\t)" PVVAR
PARAMETER="$VGVAR $PVVAR"
VGSTATUS=Reduce
;;
"3")
TITLE='Active Volumn Group'
EchoTitle newline
COMMAND=vgchange
ChooseVG
ADDTIONAL_OPTIONS='-a y'
PARAMETER=$VGVAR
VGSTATUS=Active
;;
"4")
TITLE='Inactive Volumn Group'
EchoTitle newline
COMMAND=vgchange
ChooseVG
ADDTIONAL_OPTIONS='-a n'
PARAMETER=$VGVAR
VGSTATUS=Inactive
;;
* )
ErrorInput
ReadError
VG
;;
esac
}
VGShow()
{
echo "$(printf \\t)Which Volumn Group You Want To Show ?"
NextLine
echo "$(printf \\t)All Volumn Group$(printf \\t)...... 1
$(printf \\t)Single Volumn Group$(printf \\t)...... 2"
GetChoice
if [ $CHOICE -eq 1 ]
then clear
vgdisplay -v
elif [ $CHOICE -eq 2 ]
then EchoTitle newline
ChooseVG
PARAMETER=$VGVAR
clear
vgdisplay -v $PARAMETER
else
ErrorInput
ReadError
VG
fi
}
VGMerge()
{
echo "$(printf \\t)Merge Source Volumn Group To Destination Volumn Group!"
NextLine
read -p "$(printf \\t)Please Input Your Destination Volumn Group:$(printf \\t)" DestinationVG
NextLine
read -p "$(printf \\t)Please Input Your Source Volumn Group:$(printf \\t)" SourceVG
NextLine
COMMAND=vgmerge
PARAMETER="$DestinationVG $SourceVG"
}
VGMigrate()
{
echo "$(printf \\t)Which Operation Do You Want To Do?"
NextLine
echo "$(printf \\t)Export Volumn Group$(printf \\t)...... 1
$(printf \\t)Import Volumn Group$(printf \\t)...... 2"
GetChoice
NextLine
if [ $CHOICE -eq 1 ]
then TITLE='Export Volumn Group'
EchoTitle newline
COMMAND=vgexport
read -p "$(printf \\t)Which Volumn Group Do You Want To Export:$(printf \\t)" PARAMETER
NextLine
VGSTATUS=Export
elif [ $CHOICE -eq 2 ]
then TITLE='Import Volumn Group'
EchoTitle newline
COMMAND=vgimport
read -p "$(printf \\t)Which Volumn Group Do You Want To Import:$(printf \\t)" PARAMETER
NextLine
VGSTATUS=Import
else
ErrorInput
ReadError
VG
fi
}
VG()
{
UnsetVar
clear
TITLE='Volumn Group Menu'
EchoTitle
ShowVGMenu()
{
echo "
$(printf \\t)Volumn Group Menu
$(printf \\t)Input Number To Choose!
$(printf \\t)Create Volumn Group$(printf \\t)...... 1
$(printf \\t)Remove Volumn Group$(printf \\t)...... 2
$(printf \\t)Change Volumn Group$(printf \\t)...... 3
$(printf \\t)Show Volumn Group$(printf \\t)...... 4
$(printf \\t)Scan Volumn Group$(printf \\t)...... 5
$(printf \\t)Merge Volumn Group$(printf \\t)...... 6
$(printf \\t)Migrate Volumn Group$(printf \\t)...... 7
$(printf \\t)Exit $(printf \\t)...... 8"
NextLine
}
ShowVGMenu
GetChoice
case $CHOICE in
"1")
TITLE='Create Volumn Group'
EchoTitle newline
COMMAND=vgcreate
read -p "$(printf \\t)Please Input VGNAME You Want To Create:$(printf \\t)" VGNAME
NextLine
read -p "$(printf \\t)Please Input PVNAME(Member Of Volumn Group):$(printf \\t)" PVNAME
NextLine
read -p "$(printf \\t)Please Input Physical Extent Size:$(printf \\t)(Example: 16MB$(printf \\t)Default: 4MB)$(printf \\t)" PESIZE
NextLine
PARAMETER=" $VGNAME $PVNAME"
if [ -n "$PESIZE" ]
then ADDTIONAL_OPTIONS="-s $PESIZE"
fi
echo "$(printf \\t)Attention:$(printf \\t)Volumn Group $VGNAME Will Be Create And $PVNAME Will Be It Member"
VerifyOperate
;;
"2")
TITLE='Remove Volumn Group'
EchoTitle newline
COMMAND=vgremove
ChooseVG
PARAMETER=$VGVAR
echo "$(printf \\t)Attention:$(printf \\t)Volumn Group $PARAMETER Will Remove"
VerifyOperate
;;
"3")
TITLE='Change Volumn Group'
EchoTitle newline
VGChange
NextLine
echo "$(printf \\t)Attention:$(printf \\t)Volumn Group $VGVAR Will $VGSTATUS"
VerifyOperate
;;
"4")
TITLE='Show Volumn Group'
EchoTitle newline
VGShow
;;
"5")
TITLE='Scan Volumn Group'
clear
EchoTitle newline
vgscan
;;
"6")
TITLE='Merge Volumn Group'
EchoTitle newline
VGMerge
echo "$(printf \\t)Attention:$(printf \\t)Volumn Group $SourceVG Will Merge To $DestinationVG "
VerifyOperate
;;
"7")
TITLE='Migrate Volumn Group'
EchoTitle newline
VGMigrate
echo "$(printf \\t)Attention:$(printf \\t)Volumn Group $PARAMETER Will Be $VGSTATUS"
VerifyOperate
;;
"8")
LVMMain
;;
* )
ErrorInput
ReadError
VG
;;
esac
exit 0
}
VerifyLVType()
{
echo "$(printf \\t)Which Type You Want To Determine Size Of Logical Volumn?
$(printf \\t)LogicalExtentsNumber$(printf \\t)...... 1
$(printf \\t)LogicalVolumeSize $(printf \\t)...... 2"
GetChoice
if [ $CHOICE -eq 1 ]
then LVOPTION='-l'
read -p "$(printf \\t)Please Input Numbers Of Logical Extent:$(printf \\t)" LENUM
SIZE="$LVOPTION $LENUM"
elif [ $CHOICE -eq 2 ]
then LVOPTION='-L'
read -p "$(printf \\t)Please Input Size Of Logical Volumn:$(printf \\t)" LVSIZE
SIZE="$LVOPTION $LVSIZE"
else
ErrorInput
ReadError
LV
fi
}
Snapshot()
{
VerifyLVType
NextLine
COMMAND=lvcreate
read -p "$(printf \\t)Please Input Logical Volumn Name You Want To Create:$(printf \\t)" LVNAME
NextLine
ChooseLV
PARAMETER=$LVVAR
NextLine
LVNAMEVAR="-n $LVNAME"
ADDTIONAL_OPTIONS="-s $LVNAMEVAR $SIZE"
echo "$(printf \\t)Attention:$(printf \\t)Snapshot $LVNAME Will Create"
VerifyOperate
}
LVCreate()
{
COMMAND=lvcreate
read -p "$(printf \\t)Please Input Logical Volumn Name You Want To Create:$(printf \\t)" LVNAME
LVNAMEVAR="-n $LVNAME"
ChooseVG
PARAMETER=$VGVAR
echo "$(printf \\t)Which Type Of Logical Volumn You Want To Create?
$(printf \\t)Common Logical Volumn$(printf \\t)...... 1
$(printf \\t)Mirror Logical Volumn$(printf \\t)...... 2
$(printf \\t)Stripe Logical Volumn$(printf \\t)...... 3
$(printf \\t)exit $(printf \\t)...... 4"
NextLine
GetChoice
case $CHOICE in
"1")
TITLE='Create Common Logical Volumn'
EchoTitle newline
VerifyLVType
NextLine
ADDTIONAL_OPTIONS="$LVNAMEVAR $SIZE"
;;
"2")
TITLE='Create Mirror Logical Volumn'
EchoTitle newline
VerifyLVType
NextLine
read -p "$(printf \\t)How Many Mirror You Want To Create?$(printf \\t)" MIRRORNUM
MIRRORVAR="-m $MIRRORNUM"
ADDTIONAL_OPTIONS="$MIRRORVAR $LVNAMEVAR $SIZE"
;;
"3")
TITLE='Create Stripe Logical Volumn'
EchoTitle newline
VerifyLVType
NextLine
read -p "$(printf \\t)Please Input Stripe Size:$(printf \\t)" STRIPESIZE
NextLine
read -p "$(printf \\t)Please Input Stripe Width:$(printf \\t)" STRIPEWIDTH
STRIPEVAR="-i $STRIPEWIDTH -I $STRIPESIZE"
ADDTIONAL_OPTIONS="$STRIPEVAR $LVNAMEVAR $SIZE"
;;
"4")
LV
;;
* )
ErrorInput
ReadError
LV
;;
esac
}
LVChange()
{
echo "$(printf \\t)Active Logical Volumn$(printf \\t)...... 1
$(printf \\t)Inactive Logical Volumn$(printf \\t)...... 2
$(printf \\t)Rename Logical Volumn$(printf \\t)...... 3
$(printf \\t)Extend Logical Volumn$(printf \\t)...... 4
$(printf \\t)Reduce Logical Volumn$(printf \\t)...... 5"
GetChoice
case $CHOICE in
"1")
TITLE='Active Logical Volumn'
EchoTitle newline
COMMAND=lvchange
ADDTIONAL_OPTIONS='-a y'
ChooseLV
PARAMETER=$LVVAR
LVSTATUS=Active
;;
"2")
TITLE='Inactive Logical Volumn'
EchoTitle newline
COMMAND=lvchange
ADDTIONAL_OPTIONS='-a n'
ChooseLV
PARAMETER=$LVVAR
LVSTATUS=Inactive
;;
"3")
TITLE='Rename Logical Volumn'
EchoTitle newline
COMMAND=lvrename
ChooseLV
read -p "$(printf \\t)Please Input New Name Of The Logical Volumn:$(printf \\t)" LVNEWNAME
PARAMETER="$LVVAR $LVNEWNAME"
LVSTATUS=Rename
NextLine
;;
"4")
TITLE='Extend Logical Volumn'
EchoTitle newline
VerifyLVType
COMMAND=lvextend
ChooseLV
PARAMETER="$LVVAR"
LVSTATUS=Extend
ADDTIONAL_OPTIONS="$SIZE"
;;
"5")
TITLE='Reduce Logical Volumn'
EchoTitle newline
VerifyLVType
COMMAND=lvreduce
ChooseLV
PARAMETER="$LVVAR"
LVSTATUS=Reduce
ADDTIONAL_OPTIONS="$SIZE"
;;
* )
ErrorInput
ReadError
LV
;;
esac
}
LVShow()
{
echo "$(printf \\t)Which Logical Volumn You Want To Show ?"
NextLine
echo "$(printf \\t)All Logical Volumn$(printf \\t)...... 1
$(printf \\t)Single Logical Volumn$(printf \\t)...... 2"
GetChoice
if [ $CHOICE -eq 1 ]
then clear
lvdisplay
elif [ $CHOICE -eq 2 ]
then EchoTitle newline
ChooseLV
PARAMETER=$LVVAR
clear
lvdisplay $PARAMETER
else
ErrorInput
ReadError
LV
fi
}
LV()
{
UnsetVar
clear
TITLE='Logical Volumn Menu'
EchoTitle
ShowLVMenu()
{
echo "
$(printf \\t)Logical Volumn Menu
$(printf \\t)Input Number To Choose!
$(printf \\t)Create Logical Volumn$(printf \\t)...... 1
$(printf \\t)Remove Logical Volumn$(printf \\t)...... 2
$(printf \\t)Change Logical Volumn$(printf \\t)...... 3
$(printf \\t)Show Logical Volumn$(printf \\t)...... 4
$(printf \\t)Scan Logical Volumn$(printf \\t)...... 5
$(printf \\t)Create Snapshot $(printf \\t)...... 6
$(printf \\t)Exit $(printf \\t)...... 7"
NextLine
}
ShowLVMenu
GetChoice
case $CHOICE in
"1")
TITLE='Create Logical Volumn'
EchoTitle newline
LVCreate
NextLine
echo "$(printf \\t)Attention:$(printf \\t)Logical Volumn $LVNAME Will Create"
VerifyOperate
;;
"2")
TITLE='Remove Logical Volumn'
EchoTitle newline
COMMAND=lvremove
ChooseLV
PARAMETER=$LVVAR
echo "$(printf \\t)Attention:$(printf \\t)Logical Volumn $PARAMETER Will Remove"
VerifyOperate
;;
"3")
LVChange
echo "$(printf \\t)Attention:$(printf \\t)Logical Volumn $PARAMETER Will $LVSTATUS"
VerifyOperate
;;
"4")
TITLE='Show Logical Volumn'
EchoTitle newline
LVShow
;;
"5")
TITLE='Scan Logical Volumn'
clear
EchoTitle newline
lvscan
;;
"6")
TITLE='Create Snapshot'
EchoTitle newline
Snapshot
;;
"7")
LVMMain
;;
* )
ErrorInput
ReadError
LV
;;
esac
exit 0
}
LVMMain()
{
clear
TITLE='LVM Main Menu'
EchoTitle
echo "
$(printf \\t)LVM Main Menu
$(printf \\t)Input Number To Choose!
$(printf \\t)Physical Volumn Manager$(printf \\t)...... 1
$(printf \\t)Volumn Group Manager$(printf \\t)...... 2
$(printf \\t)Logical Volumn Manager$(printf \\t)...... 3
$(printf \\t)Exit $(printf \\t)...... 4"
GetChoice
NextLine
case $CHOICE in
"1")
PV
Status=0
;;
"2")
VG
Status=0
;;
"3")
LV
Status=0
;;
"4")
exit 0
;;
* )
ErrorInput
ReadError
LVMMain
;;
esac
ReturnToLVMMain()
{
if [ $Status -eq 0 ]
then NextLine
LVMMain
fi
}
ReturnToLVMMain
}
LVMMain
The End