背景:sfyun主机磁盘空间已满,经常告警,需要进行清理但是/app空间内的东西都不可被删除。
    措施:在sfyun上面对云主机进行热扩容,然后再对磁盘进行分区,扩充至对应的挂载点。

    1、lsblk,fdisk -l查看对应的磁盘信息
    新挂载的磁盘名称为/dev/vdc
    2、对新扩容磁盘进行分区

    1. # 将/dev/vdc扩容至分为两个250G大小的分区
    2. fdisk /dev/vdc
    3. n

    3、格式化分区的文件系统

    1. mkfs.xfs /dev/vdc1
    2. mkfs.xfs /dev/vdc2

    4、将格式化的分区扩充至原有的vg,扩容VG
    image.png

    1. pvcreate /dev/vdc1
    2. pvcreate /dev/vdc2
    3. vgextend VGapp /dev/vdc1
    4. vgextend VolGroup00 /dev/vdc2

    5、扩容逻辑卷LV
    1、扩容/dev/VolGroup00/LVroot

    1. lvresize -L +250G /dev/VolGroup00/LVroot
    2. # 扩展完磁盘后使用lsblk查看的结果变了而使用df -h结果大小却没变,
    3. # 这是因为前者查看的是磁盘的大小而后者查看的是文件系统的大小。
    4. # 扩展文件系统的大小,xfs_growfs ,resize2fs
    5. xfs_growfs /dev/VolGroup00/LVroot

    2、扩容 /dev/VGapp/LVapp

    1. lvresize -L +250G /dev/VGapp/LVapp
    2. xfs_growfs /dev/VGapp/LVapp

    6、等这些做完了后,扩容也就完成了,检查下扩容后的磁盘空间
    image.png

    at last,分享一个磁盘分区并挂载的脚本

    1. #!/bin/bash
    2. partition1=/data # 定义最终挂载的名称
    3. partition2=/log # 定义最终挂载的名称
    4. disk=
    5. # 判断/log /data目录是否存在
    6. if [[ -d "/log" ]] || [[ -d "/data" ]];then
    7. echo "/log or /data had exist,please check!"
    8. exit 0
    9. fi
    10. # 检查磁盘盘符及容量
    11. all_size=0
    12. DIVISOR=1073741824
    13. DISK_NAME=`fdisk -l | grep "Disk /dev/vd" | awk -F '[ :]+' '{print $2}'`
    14. echo "DISK_NAME:$DISK_NAME"
    15. for disk_check in ${DISK_NAME[*]};do
    16. echo "disk_check:$disk_check"
    17. df_disk=`df -h | grep ${disk_check} | wc -l`
    18. if [[ $df_disk -eq 0 ]];then
    19. # 检查磁盘是否已经有分区
    20. check_partion=`fdisk -l |grep "^${disk_check}" | wc -l`
    21. if [ $check_partion -gt 0 ];then
    22. lvm_partions=`fdisk -l |grep "^${disk_check}" | awk '$NF="LVM" {print $1}'`
    23. if [ -n $lvm_partions ];then
    24. for disk_partion in ${lvm_partions};do
    25. new_disk_size=`fdisk -s $disk_partion`
    26. new_disk_size_G=$(($new_disk_size*1024/$DIVISOR))
    27. all_size=$(($all_size+$new_disk_size_G))
    28. disk="$disk ${disk_partion}" # 将所有分区拼起来
    29. done
    30. fi
    31. else
    32. new_disk_size=`fdisk -l | grep "$disk_check" | awk -F '[ ,:]+' '{print $5}'`
    33. new_disk_size_G=$(($new_disk_size/$DIVISOR))
    34. all_size=$(($all_size+$new_disk_size_G))
    35. fdisk ${disk_check} << EOF
    36. n
    37. p
    38. 1
    39. t
    40. 8e
    41. w
    42. EOF
    43. disk="$disk ${disk_check}1" # 将所有分区拼起来
    44. fi
    45. fi
    46. done
    47. echo "disk:$disk"
    48. if [[ $all_size -eq 0 ]] || [[ -z "$disk" ]];then
    49. echo "None empty disk is found!"
    50. exit 0
    51. else
    52. echo "开始创建lvm逻辑卷"
    53. pvcreate $disk
    54. vgcreate VolGroupEs $disk
    55. # 100G /log, 剩余给 /data
    56. if [ $all_size -gt 1000 ];then
    57. lvcreate -l 10%FREE -n LVlog VolGroupEs << EOF
    58. y
    59. EOF
    60. else
    61. if [ $all_size -le 100 ];then
    62. lvcreate -l 100%FREE -n LVlog VolGroupEs << EOF
    63. y
    64. EOF
    65. else
    66. lvcreate -L 100G -n LVlog VolGroupEs << EOF
    67. y
    68. EOF
    69. fi
    70. fi
    71. lvcreate -l 100%FREE -n LVdata VolGroupEs << EOF
    72. y
    73. EOF
    74. mkfs.ext4 /dev/mapper/VolGroupEs-LVlog
    75. mkfs.ext4 /dev/mapper/VolGroupEs-LVdata
    76. mkdir -p $partition2
    77. mkdir -p $partition1
    78. grep "VolGroupEs-LVlog" /etc/fstab > /dev/null
    79. if [ $? -gt 0 ];then
    80. echo "/dev/mapper/VolGroupEs-LVlog $partition2 ext4 defaults 0 0" >> /etc/fstab
    81. fi
    82. grep "VolGroupEs-LVdata" /etc/fstab > /dev/null
    83. if [ $? -gt 0 ];then
    84. echo "/dev/mapper/VolGroupEs-LVdata $partition1 ext4 defaults 0 0" >> /etc/fstab
    85. fi
    86. mount -a
    87. df -h
    88. fi