ssslinppp

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
概要说明:
  • /dev/mapper/vg0-data挂载在目录/data下;
  • 当新增物理磁盘时,需要自动对/dev/mapper/vg0-data磁盘时进行扩容(自动化脚本);
当新增物理磁盘时,自动扩容到:/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

新增物理磁盘:
新添加了两块物理磁盘,分别为/dev/sdd 和/dev/sde,希望执行自动扩容脚本后,可以对/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. /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扩展脚本如下:
  • 该脚本会自动比对哪些物理磁盘没有进行LVM扩展,并将未扩展的磁盘扩展到/dev/vg0/data;
  • 不会扩展sda和hda磁盘;
  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. pvscan | grep /dev/[sh]d. | awk -F ' ' '{print $2}' > $pvs_info
  27. #get Difference set
  28. # sort $fdisk_info $pvs_info $pvs_info | uniq -u > $diffSet
  29. grep -F -v -f $pvs_info $fdisk_info > $diffSet
  30. echo "-----------------------------" >> $log_file_extendLvm
  31. echo "fdisk_info is :" >> $log_file_extendLvm
  32. cat $fdisk_info >> $log_file_extendLvm
  33. echo "pvs_info is :" >> $log_file_extendLvm
  34. cat $pvs_info >> $log_file_extendLvm
  35. echo "diffSet is:" >> $log_file_extendLvm
  36. cat $diffSet >> $log_file_extendLvm
  37. echo "----------------------------" >> $log_file_extendLvm
  38. rm -rf $fdisk_info
  39. rm -rf $pvs_info
  40. }
  41. extendLV()
  42. {
  43. # example: change "sdb" to "/dev/sdb"
  44. dev_disk=`echo $dev_disk|sed '/dev/!s/^/\/dev\//'`
  45. if [ ! -e $dev_disk ];then
  46. echo "[ERROR] $dev_disk do not exists" >> $log_file_extendLvm
  47. echo "error"
  48. exit -1
  49. fi
  50. if [ `pvs | grep $dev_disk | wc -l` = "1" ];then
  51. echo "[ERROR] $dev_disk(as PE) has already extend to $vg_name" >> $log_file_extendLvm
  52. echo "error"
  53. exit -2
  54. fi
  55. # step 1 :extend vg
  56. echo "step 1- exec cmd: vgextend vg0 $dev_disk" >> $log_file_extendLvm
  57. vgextend -f vg0 $dev_disk >> $log_file_extendLvm 2>&1
  58. # step 2 :extend lv
  59. echo "step 2- exec cmd: lvextend -l 100%VG $lv_name" >> $log_file_extendLvm
  60. lvextend -l 100%VG $lv_name >> $log_file_extendLvm 2>&1
  61. # step 3: resize2fs /dev/vg0/data
  62. echo "step 3- exec cmd: resize2fs $lv_name" >> $log_file_extendLvm
  63. resize2fs $lv_name >> $log_file_extendLvm 2>&1
  64. }
  65. # display info
  66. echo "Before extendLVM..." >> $log_file_extendLvm
  67. displayInfo
  68. getUnLvmDisk
  69. cat $diffSet | while read dev_disk
  70. do
  71. if [ $dev_disk != "/dev/sda" ] && [ $dev_disk != "/dev/hda" ] && [ `echo $dev_disk | wc -L` == 8 ];then
  72. # exec extendLVM
  73. # echo "+++++++++++++++++ [$dev_disk]"
  74. extendLV
  75. fi
  76. done
  77. # display info
  78. echo "After extendLVM... " >> $log_file_extendLvm
  79. displayInfo
  80. echo "extendLVM finish success" >> $log_file_extendLvm
  81. echo "success"







附件列表

     

    posted on 2016-09-09 20:39  ssslinppp  阅读(2239)  评论(0编辑  收藏  举报