【问题标题】:Script to extend LVM volume and resize the filesystem扩展 LVM 卷和调整文件系统大小的脚本
【发布时间】:2020-07-23 23:24:42
【问题描述】:

在我的服务器上,我必须不时扩展 lvm 卷以腾出更多空间。为此,我使用命令lvextendresize2fs。我想要一个命令,它将向我显示一个可供选择的 LVM 卷列表并要求增加大小。

请注意,使用 lvm 和 resize2fs 您可以可靠地调整文件系统的大小而不会中断任何内容。报告的可用大小刚刚增加。

【问题讨论】:

    标签: linux filesystems ext4 lvm ext3


    【解决方案1】:

    我最终编写并用于在线调整大小的脚本是:

    #!/bin/bash
    # 2020-04-11 Initial Version
    
    function error()
    {
       echo "*** $*"
       exit 1
    }
    
    if [ "$1" == "" ] ; then
        echo "Please select the partition from the list"
    
        i=1
        for j in /dev/mapper/kalypso*
        do
            echo "$i.$j"
            file[i]=$j
            i=$(( i + 1 ))
        done
    
        echo "Enter number"
        read input
        partition=${file[$input]}
        echo "You selected partition $partition"
    
        df -h $partition
        echo "Enter size to add, to $partition e.g. 50G:"
        read size
        echo "You choose to increase file $partition by $size"
        sudo lvextend -L +$size $partition || error "lvextend $partition failed"
        sudo resize2fs $partition || error "resize2fs $partition failed"
        echo "lvextend/resize2fs completed"
        df -h $partition | tail -n 1
    fi
    

    【讨论】:

      【解决方案2】:
      #! /bin/bash
      # This scripts gets five variables needed to expand a LVM
      # 1) disks (/dev/sd*)
      # 2) Logical Volume Name (lvdisplay | grep 'LV Name')
      # 3) Logical Volume Path (retrieved from LV Name on previous point)
      # 4) Device Mapper (mapping physical block devices onto higher-level virtual block devices)
      # 5) Volume Group Name (vgdisplay)
      # This scripts only outputs the required commands to extend LVM, no changes are made
      # Tested on Centos 7
      # Nuno Machado da Silva © 2021.09
      
      clear
      
      ########################################## STEP #1 - Get Avaiable disks to extend LVM based that disks are named /dev/sd* ##########################################
      
      disks_var=$(fdisk -l | grep '^Disk /dev/sd')
      SAVEIFS=$IFS   # Save current IFS
      IFS=$'\n'      # Change IFS to new line
      disks=($disks_var) # split to array $names
      IFS=$SAVEIFS   # Restore IFS
      
      echo -n "Choose new disk to expand LVM:"
      echo
      
      for (( i=0; i<${#disks[@]}; i++ ))
      do
          echo "$i: ${disks[$i]}"
      done
      
      read -r disk_option
      
      selectedDisk=$(echo ${disks[$disk_option]} | cut -d ":" -f1)
      selectedDisk=$(cut -d " " -f2 <<< "$selectedDisk")
      
      ########################################## STEP #2 + #3 - Get LV Name + LV Path ##########################################
      
      lvName_var=$(lvdisplay | grep 'LV Name')
      SAVEIFS=$IFS      # Save current IFS
      IFS=$'\n'         # Change IFS to new line
      lvs=($lvName_var) # split to array $names
      IFS=$SAVEIFS      # Restore IFS
      
      
      echo -n "Choose Logical Volume to expand:"
      echo
      
      for (( i=0; i<${#lvs[@]}; i++ ))
      do
          echo "$i: ${lvs[$i]}"
      done
      
      read -r lvName_option
      selectedLVName=$(echo ${lvs[$lvName_option]} | cut -d ":" -f1)
      selectedLVName=$(cut -d " " -f3 <<< "$selectedLVName")
      lvPath_var=$(lvdisplay | grep "$selectedLVName")
      selectedLVPath=$(echo $lvPath_var | cut -d " " -f3)
      lvPath_var=$(lvdisplay | grep "$selectedLVName")
      selectedLVPath=$(echo $lvPath_var | cut -d " " -f3)
      
      
      ########################################## STEP #4 - Get Device Mapper Path ##########################################
      
      devmapper_var=$(fdisk -l | grep '^Disk /dev/m')
      SAVEIFS=$IFS          # Save current IFS
      IFS=$'\n'             # Change IFS to new line
      dmp=($devmapper_var)  # split to array $names
      IFS=$SAVEIFS          # Restore IFS
      
      echo -n "Choose Device Mapper to extend:"
      echo
      for (( i=0; i<${#dmp[@]}; i++ ))
      do
      echo "$i: ${dmp[$i]}"
      done
      
      read -r dmp_option
      
      selectedDEVMAPPER=$(echo ${dmp[$dmp_option]} | cut -d ":" -f1)
      
      echo "1 selectedDEVMAPPER: $selectedDEVMAPPER"
      
      selectedDevMapperName=$(cut -d " " -f2 <<< "$selectedDEVMAPPER")
      
      echo "2 selectedDevMapperName: |$selectedDevMapperName|"
      
      
      
      ########################################## STEP #5 - Get Avaiable Volume Groups ##########################################
      
      vg_var=$(vgdisplay | grep 'VG Name')
      
      
      SAVEIFS=$IFS   # Save current IFS
      IFS=$'\n'      # Change IFS to new line
      vgs=($vg_var)  # split to array $names
      IFS=$SAVEIFS   # Restore IFS
      
      
      echo -n "Choose Volume Group to extend:"
      echo
      
      for (( i=0; i<${#vgs[@]}; i++ ))
      do
          echo "$i: ${vgs[$i]}"
      done
      
      read -r vg_option
      
      selectedVG=$(echo ${vgs[$vg_option]} | cut -d ":" -f2)
      selectedVG=$(cut -d " " -f3 <<< "$selectedVG")
      
      
      ########################################## COMMANDS OUTPUT ##########################################
      echo "List of commands:"
      echo
      echo "------------------------------------------------------------------------------"
      echo "pvcreate $selectedDisk"
      echo "vgextend $selectedVG $selectedDisk"
      echo "lvm lvextend -l +100%FREE $selectedDevMapperName"
      echo "xfs_growfs $selectedDevMapperName"
      echo "------------------------------------------------------------------------------"
      

      【讨论】:

        猜你喜欢
        • 2019-08-17
        • 2011-10-17
        • 1970-01-01
        • 2010-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-07
        • 2011-07-30
        相关资源
        最近更新 更多