背景:sfyun主机磁盘空间已满,经常告警,需要进行清理但是/app空间内的东西都不可被删除。
措施:在sfyun上面对云主机进行热扩容,然后再对磁盘进行分区,扩充至对应的挂载点。
1、lsblk,fdisk -l查看对应的磁盘信息
新挂载的磁盘名称为/dev/vdc
2、对新扩容磁盘进行分区
# 将/dev/vdc扩容至分为两个250G大小的分区
fdisk /dev/vdc
n
3、格式化分区的文件系统
mkfs.xfs /dev/vdc1
mkfs.xfs /dev/vdc2
4、将格式化的分区扩充至原有的vg,扩容VG
pvcreate /dev/vdc1
pvcreate /dev/vdc2
vgextend VGapp /dev/vdc1
vgextend VolGroup00 /dev/vdc2
5、扩容逻辑卷LV
1、扩容/dev/VolGroup00/LVroot
lvresize -L +250G /dev/VolGroup00/LVroot
# 扩展完磁盘后使用lsblk查看的结果变了而使用df -h结果大小却没变,
# 这是因为前者查看的是磁盘的大小而后者查看的是文件系统的大小。
# 扩展文件系统的大小,xfs_growfs ,resize2fs
xfs_growfs /dev/VolGroup00/LVroot
2、扩容 /dev/VGapp/LVapp
lvresize -L +250G /dev/VGapp/LVapp
xfs_growfs /dev/VGapp/LVapp
6、等这些做完了后,扩容也就完成了,检查下扩容后的磁盘空间
at last,分享一个磁盘分区并挂载的脚本
#!/bin/bash
partition1=/data # 定义最终挂载的名称
partition2=/log # 定义最终挂载的名称
disk=
# 判断/log /data目录是否存在
if [[ -d "/log" ]] || [[ -d "/data" ]];then
echo "/log or /data had exist,please check!"
exit 0
fi
# 检查磁盘盘符及容量
all_size=0
DIVISOR=1073741824
DISK_NAME=`fdisk -l | grep "Disk /dev/vd" | awk -F '[ :]+' '{print $2}'`
echo "DISK_NAME:$DISK_NAME"
for disk_check in ${DISK_NAME[*]};do
echo "disk_check:$disk_check"
df_disk=`df -h | grep ${disk_check} | wc -l`
if [[ $df_disk -eq 0 ]];then
# 检查磁盘是否已经有分区
check_partion=`fdisk -l |grep "^${disk_check}" | wc -l`
if [ $check_partion -gt 0 ];then
lvm_partions=`fdisk -l |grep "^${disk_check}" | awk '$NF="LVM" {print $1}'`
if [ -n $lvm_partions ];then
for disk_partion in ${lvm_partions};do
new_disk_size=`fdisk -s $disk_partion`
new_disk_size_G=$(($new_disk_size*1024/$DIVISOR))
all_size=$(($all_size+$new_disk_size_G))
disk="$disk ${disk_partion}" # 将所有分区拼起来
done
fi
else
new_disk_size=`fdisk -l | grep "$disk_check" | awk -F '[ ,:]+' '{print $5}'`
new_disk_size_G=$(($new_disk_size/$DIVISOR))
all_size=$(($all_size+$new_disk_size_G))
fdisk ${disk_check} << EOF
n
p
1
t
8e
w
EOF
disk="$disk ${disk_check}1" # 将所有分区拼起来
fi
fi
done
echo "disk:$disk"
if [[ $all_size -eq 0 ]] || [[ -z "$disk" ]];then
echo "None empty disk is found!"
exit 0
else
echo "开始创建lvm逻辑卷"
pvcreate $disk
vgcreate VolGroupEs $disk
# 100G /log, 剩余给 /data
if [ $all_size -gt 1000 ];then
lvcreate -l 10%FREE -n LVlog VolGroupEs << EOF
y
EOF
else
if [ $all_size -le 100 ];then
lvcreate -l 100%FREE -n LVlog VolGroupEs << EOF
y
EOF
else
lvcreate -L 100G -n LVlog VolGroupEs << EOF
y
EOF
fi
fi
lvcreate -l 100%FREE -n LVdata VolGroupEs << EOF
y
EOF
mkfs.ext4 /dev/mapper/VolGroupEs-LVlog
mkfs.ext4 /dev/mapper/VolGroupEs-LVdata
mkdir -p $partition2
mkdir -p $partition1
grep "VolGroupEs-LVlog" /etc/fstab > /dev/null
if [ $? -gt 0 ];then
echo "/dev/mapper/VolGroupEs-LVlog $partition2 ext4 defaults 0 0" >> /etc/fstab
fi
grep "VolGroupEs-LVdata" /etc/fstab > /dev/null
if [ $? -gt 0 ];then
echo "/dev/mapper/VolGroupEs-LVdata $partition1 ext4 defaults 0 0" >> /etc/fstab
fi
mount -a
df -h
fi