Shell指令碼一鍵擴充LVM新分區

來源:互聯網
上載者:User

標籤: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新分區

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.