Shell脚本一键扩展LVM新分区

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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新分区的主要内容,如果未能解决你的问题,请参考以下文章

linux 根分区LVM扩容

linux LVM 实战运用

LVM分区扩容(以根分区为例)

Red Hat Enterprise 6.5磁盘分区,LVM管理及磁盘配额设置

Linux Centos7 根目录扩容实操(lvm模式)

CentOS的GPT分区+LVM挂载