最近接到經理要求對RHEL7的LVM新分區擴容自動化,由於使用的是虛擬化平臺,故採用添加新磁盤的方式來作:bash
#!/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
目前暫時只能想到這個方式,但願能獲得指正優化這個腳本,加強其移植性和容錯性。ide