最近接到经理要求对RHEL7的LVM新分区扩容自动化,因为使用的是虚拟化平台,故采用添加新磁盘的方式来做:
#!/bin/bash ##在线扫描新磁盘##Step1: Recognize New Hard Disk##Logdir=~/disk_log if [ ! -d $Logdir ];then mkdir -p $Logdirfi 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.txtelse echo "`date +"%Y/%m/%d %T %Z %:::z"`: Step1 -> SCSI Disk Rescanning is Faied, Script is Interrupted" >> $Logdir/log_$HOSTNAME.txtfi ##创建新的磁盘分区##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 <<EOFnp1 t8ewqEOF if [ $? -eq 0 ];then echo "`date +"%Y/%m/%d %T %Z %:::z"`: Step2 -> SCSI Disk Partition Creation is Successful" >> $Logdir/log_$HOSTNAME.txtelse echo "`date +"%Y/%m/%d %T %Z %:::z"`: Step2 -> SCSI Disk Partition Creation is Faied, Script is Interrupted" >> $Logdir/log_$HOSTNAME.txt exitfi ##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 $LVRootelse echo "`date +"%Y/%m/%d %T %Z %:::z"`: Step3 -> VG Exention is Failed" >> $Logdir/log_$HOSTNAME.txt exitfi if [ $? -eq 0 ];then echo "`date +"%Y/%m/%d %T %Z %:::z"`: Step3 -> LV and File System Extention is Successful" >> $Logdir/log_$HOSTNAME.txtelse echo "`date +"%Y/%m/%d %T %Z %:::z"`: Step3 -> LV and File System Extention is Faied, Script is Interrupted" >> $Logdir/log_$HOSTNAME.txt exitfi exit 0
目前采用的添加新磁盘后,手动运行脚本,个人认为可以添加条件判断语句,放到crontab,这样以后只需配合外部工具,客户可以实现自动添加新磁盘和扩容。
目前暂时只能想到这个方式,希望能得到各位的指正,让我能优化这个脚本,增强其移植性和容错性。
时间: 2024-10-12 20:17:59