首页 > 代码库 > 【LVM】LVM自动扩容脚本

【LVM】LVM自动扩容脚本

当新增物理磁盘时,自动扩容到:/dev/vg0/data
例如,如下是原始的:
  1. [root@localhost ~]# pvs
  2. PV VG Fmt Attr PSize PFree
  3. /dev/sdb vg0 lvm2 a-- 10.00g 0
  4. /dev/sdc vg0 lvm2 a-- 10.00g 0
  5. [root@localhost ~]# vgs
  6. VG #PV #LV #SN Attr VSize VFree
  7. vg0 2 1 0 wz--n- 19.98g 0
  8. [root@localhost ~]# lvs
  9. LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
  10. data vg0 -wi-ao---- 19.98g
  11. [root@localhost ~]# df -Th
  12. Filesystem Type Size Used Avail Use% Mounted on
  13. /dev/mapper/vg0-data ext3 19G 52M 18G 1% /data
如果添加了物理磁盘,该脚本会自动比对哪些物理磁盘没有进行LVM扩展,并将未LVM扩展的磁盘扩展到/dev/vg0/data。
比如,新添加了两块物理磁盘,分别为/dev/sdd 和/dev/sde,执行该脚本后的结果:
  1. [root@localhost ~]# pvs
  2. PV VG Fmt Attr PSize PFree
  3. /dev/sdb vg0 lvm2 a-- 10.00g 0
  4. /dev/sdc vg0 lvm2 a-- 10.00g 0
  5. /dev/sdd vg0 lvm2 a-- 15.00g 0
  6. /dev/sde vg0 lvm2 a-- 15.00g 0
  7. [root@localhost ~]# vgs
  8. VG #PV #LV #SN Attr VSize VFree
  9. vg0 4 1 0 wz--n- 49.98g 0
  10. [root@localhost ~]# lvs
  11. LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
  12. data vg0 -wi-ao---- 49.98g
  13. [root@localhost ~]# df -Th
  14. Filesystem Type Size Used Avail Use% Mounted on
  15. /dev/mapper/vg0-data ext3 50G 52M 47G 1% /data

自动LVM扩展脚本如下:

  1. #!/bin/bash
  2. #################################################
  3. # Create by ssslinppp
  4. # 201699 15:16:58
  5. ##################################################
  6. log_file_extendLvm="/data/logs/log_file_extendLvm.log"
  7. echo "====================================================" > $log_file_extendLvm
  8. echo "Begin to extendLVM at `date "+%Y-%m-%d %H:%M:%S"`" >> $log_file_extendLvm
  9. vg_name="vg0"
  10. lv_name="/dev/vg0/data"
  11. fdisk_info="/data/fdisk_info"
  12. pvs_info="/data/pvs_info"
  13. diffSet="/data/diffSet"
  14. displayInfo()
  15. {
  16. echo "---cmd: df -Th--------------------- " >> $log_file_extendLvm
  17. df -Th /data >> $log_file_extendLvm 2>&1
  18. echo "---cmd: pvs------------------------ " >> $log_file_extendLvm
  19. pvs >> $log_file_extendLvm 2>&1
  20. echo "---------------------------- " >> $log_file_extendLvm
  21. }
  22. getUnLvmDisk()
  23. {
  24. fdisk -l | grep /dev/[sh]d.: | awk -F ‘ ‘ ‘{print $2}‘ | awk -F ‘:‘ ‘{print $1}‘ > $fdisk_info
  25. pvs | grep /dev/[sh]d. | awk -F ‘ ‘ ‘{print $1}‘ > $pvs_info
  26. #get Difference set
  27. sort $fdisk_info $pvs_info $pvs_info | uniq -u > $diffSet
  28. echo "-----------------------------" >> $log_file_extendLvm
  29. echo "fdisk_info is :" >> $log_file_extendLvm
  30. cat $fdisk_info >> $log_file_extendLvm
  31. echo "pvs_info is :" >> $log_file_extendLvm
  32. cat $pvs_info >> $log_file_extendLvm
  33. echo "diffSet is:" >> $log_file_extendLvm
  34. cat $diffSet >> $log_file_extendLvm
  35. echo "----------------------------" >> $log_file_extendLvm
  36. rm -rf $fdisk_info
  37. rm -rf $pvs_info
  38. }
  39. extendLV()
  40. {
  41. # example: change "sdb" to "/dev/sdb"
  42. dev_disk=`echo $dev_disk|sed ‘/dev/!s/^/\/dev\//‘`
  43. if [ ! -e $dev_disk ];then
  44. echo "[ERROR] $dev_disk do not exists" >> $log_file_extendLvm
  45. echo "error"
  46. exit -1
  47. fi
  48. if [ `pvs | grep $dev_disk | wc -l` = "1" ];then
  49. echo "[ERROR] $dev_disk(as PE) has already extend to $vg_name" >> $log_file_extendLvm
  50. echo "error"
  51. exit -2
  52. fi
  53. # step 1 :extend vg
  54. echo "step 1- exec cmd: vgextend vg0 $dev_disk" >> $log_file_extendLvm
  55. vgextend -f vg0 $dev_disk >> $log_file_extendLvm 2>&1
  56. # step 2 :extend lv
  57. echo "step 2- exec cmd: lvextend -l 100%VG $lv_name" >> $log_file_extendLvm
  58. lvextend -l 100%VG $lv_name >> $log_file_extendLvm 2>&1
  59. # step 3: resize2fs /dev/vg0/data
  60. echo "step 3- exec cmd: resize2fs $lv_name" >> $log_file_extendLvm
  61. resize2fs $lv_name >> $log_file_extendLvm 2>&1
  62. }
  63. # display info
  64. echo "Before extendLVM..." >> $log_file_extendLvm
  65. displayInfo
  66. getUnLvmDisk
  67. cat $diffSet | while read dev_disk
  68. do
  69. if [ $dev_disk != "/dev/sda" ] && [ `echo $dev_disk | wc -L` == 8 ];then
  70. # exec extendLVM
  71. extendLV
  72. fi
  73. done
  74. # display info
  75. echo "After extendLVM... " >> $log_file_extendLvm
  76. displayInfo
  77. echo "extendLVM finish success" >> $log_file_extendLvm
  78. echo "success"



来自为知笔记(Wiz)


附件列表

     

    【LVM】LVM自动扩容脚本