Script to extend LVM volume and resize the filesystem

3.9k Views Asked by At

On my server, I have to extend lvm volumes from time to time in order to make more space. For this, I use commands lvextend and resize2fs. I would like to have a command which will present me a list of LVM volumes to choose from and ask for the size increase.

Note that using lvm and resize2fs you can reliably resize filesystems without interrupting anything. The reported free size just goes up.

2

There are 2 best solutions below

0
Floyd On

The script I finally wrote and use for online resizing is:

#!/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
1
Nuno Machado da Silva On
#! /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 "------------------------------------------------------------------------------"