Recently received the manager request to the RHEL7 LVM new partition expansion automation, because uses the virtualization platform, therefore uses to add the new disk the way to do:
#!/bin/bash
# #在线扫描新磁盘
# #Step1: Recognize New hard disk##
Logdir=~/disk_log
if [!-D $Logdir];then
Mkdir-p $Logdir
Fi
scsihost= ' Ls-l/sys/class/scsi_host/| grep "Host" | Tr-s "" | Cut-d ""-f9 "
For host in $SCSIHost
Do
sudo echo "---" | sudo tee/sys/class/scsi_host/$host/scan >/dev/null 2>&1
Done
If [$?-eq 0];then
echo "' Date +"%y/%m/%d%T%Z%:::z "': Step1, SCSI Disk rescanning is successful" >> $Logdir/log_$hostname.txt
Else
echo "' Date +"%y/%m/%d%T%Z%:::z "': Step1, SCSI Disk rescanning is faied, Script is interrupted" >> $Logdir/ Log_$hostname.txt
Fi
# #创建新的磁盘分区
# #Step2: Create New Disk partition##
newsd= ' sudo fdisk-l | grep "DISK/DEV/SD" | Sort | Tail-1 | Cut-d ""-f2 | Cut-d ":"-f1 '
sudo fdisk $NewSD <<eof
N
P
1
T
8e
Wq
Eof
If [$?-eq 0];then
echo "' Date +"%y/%m/%d%T%Z%:::z "': Step2, SCSI Disk Partition Creation is successful" >> $Logdir/LOG_$HOSTN AME.txt
Else
echo "' Date +"%y/%m/%d%T%Z%:::z "': Step2, SCSI Disk Partition Creation is faied, Script is interrupted" >> $Logdir/log_$hostname.txt
Exit
Fi
# #LVM在线扩容
# #Step3: Extend Disk by lvm##
newpv= ' sudo fdisk-l | grep ^ $NewSD | Cut-d ""-f1 "
vg= ' sudo vgs | Tail-1 | awk ' {print$1} '
lvroot= ' sudo lvscan | grep ACTIVE | grep root | Cut-d "'"-f2 '
sudo pvcreate $NewPV && sudo vgextend $VG $NewPV
If [$?-eq 0];then
echo "' Date +"%y/%m/%d%T%Z%:::z "': Step3, VG exention is successful" >> $Logdir/log_$hostname.txt
sudo lvextend-r-l +100%free $LVRoot
Else
echo "' Date +"%y/%m/%d%T%Z%:::z "': Step3, VG exention is Failed" >> $Logdir/log_$hostname.txt
Exit
Fi
If [$?-eq 0];then
echo "' Date +"%y/%m/%d%T%Z%:::z "': Step3, LV and File System extention is successful" >> $Logdir/LOG_$HOSTN AME.txt
Else
echo "' Date +"%y/%m/%d%T%Z%:::z "': Step3, LV and File System extention is faied, Script is interrupted" >> $Logdir/log_$hostname.txt
Exit
Fi
Exit 0
With the addition of new disks, manually run the script, personally think you can add conditional judgment statement, put to crontab, so that only with external tools, customers can automatically add new disks and expansion.
Currently can only think of this way, hope to get everyone's correction, so I can optimize the script, enhance its portability and fault tolerance.
This article is from the "http://yangqiao.blog.51cto.com/5542284/1925253" blog, please be sure to keep this source.
Shell script expands LVM new partition with one click