cwx 发表于 2018-8-26 13:04:38

Shell脚本一键扩展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 > $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


页: [1]
查看完整版本: Shell脚本一键扩展LVM新分区