標籤:shell指令碼 lvm擴容 線上掃描
最近接到經理要求對RHEL7的LVM新分區擴容自動化,因為使用的是虛擬化平台,故採用添加新磁碟的方式來做:
#!/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_$HOSTNAME.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_$HOSTNAME.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
目前採用的添加新磁碟後,手動運行指令碼,個人認為可以添加條件判斷語句,放到crontab,這樣以後只需配合外部工具,客戶可以實現自動添加新磁碟和擴容。
目前暫時只能想到這個方式,希望能得到各位的指正,讓我能最佳化這個指令碼,增強其移植性和容錯性。
本文出自 “那美剋星” 部落格,請務必保留此出處http://yangqiao.blog.51cto.com/5542284/1925253
Shell指令碼一鍵擴充LVM新分區