Easy LVM volume extension

We use linux's Logical Volume Manager (LVM) volumes extensively on our dedicated servers. Our backup server in particular often needs volumes to be extended as space requirements increase. This is a simple process involving only two or three commands, but it's important that we avoid mistakes and keep things as consistent as possible - we like to follow the KISS principle. To that end, we created a little script to help us think less and get more done.

The script

Without further ado, the script:

#!/bin/bash

# Barney Desmond <barney@anchor.net.au> 2008-08-07
# For simpler filesystem growth, performing an lvextend and resize2fs

usage()
{
        echo "Expected two arguments, LV name and no. of gb to increase by. Eg."
        echo "  $0 LVNAME NUMGIGS"
        exit 2
}


if [ $# -ne 2 ]
then
        usage
fi

LVNAME="$1"
MOREGIGS="$2"



HOSTNAME=`hostname --short`
FQDN=`hostname --fqdn`
echo -n "Looking for LVM mounts in /dev... "
if [ -d "/dev/${HOSTNAME}" ]
then
        echo "found /dev/${HOSTNAME}"
        LVDIR="/dev/${HOSTNAME}/"
elif [ -d "/dev/${FQDN}" ]
then
        echo "found /dev/${FQDN}"
        LVDIR="/dev/${FQDN}/"
else
        echo "can't find /dev/${HOSTNAME} or /dev/${FQDN}, bailing..."
        exit 1
fi


if [ ! -L "${LVDIR}${LVNAME}" ]
then
        echo "Can't find an LV at ${LVDIR}${LVNAME}, please check the name of the volume. Bailing..."
        exit 2
fi


# test if the no. of gigs is numeric, probably imperfect test...
if ! expr "${MOREGIGS}" + 0 > /dev/null 2>/dev/null
then
        echo "No. of gigs \"${MOREGIGS}\" was not numeric, bailing..."
        exit 2
fi


echo "Ready to go, will lvextend ${LVDIR}${LVNAME} by ${MOREGIGS}gb"
echo

echo "Starting lvextend..."
lvextend -L "+${MOREGIGS}G" "${LVDIR}${LVNAME}"
if [ $? -ne 0 ]
then
        echo "Error during lvextend, bailing..."
        exit 1
fi

echo "Starting resize2fs..."
resize2fs -p "${LVDIR}${LVNAME}"

echo "All done!"
df -h | grep "${LVNAME}"

Explanation and discussion

The script usually lives at /usr/local/sbin/inflate.sh for easy access. It does a quick sanity-check of the request and performs the necessary steps. The call to lvextend can sometimes take a little while on a heavily loaded system, so the ability to "fire and forget" is very useful. This is particularly appreciated by bleary-eyed sysadmins who are woken at all hours of the morning when something goes wrong.

A typical invocation:

root@chikane:~# inflate.sh himeko 1
Looking for LVM mounts in /dev... found /dev/chikane
Ready to go, will lvextend /dev/chikane/himeko by 1gb

Starting lvextend...
  Extending logical volume himeko to 16.00 GB
  Logical volume himeko successfully resized
Starting resize2fs...
resize2fs 1.40.8 (13-Mar-2008)
Filesystem at /dev/chikane/himeko is mounted on /backup/himeko; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/chikane/himeko to 4194304 (4k) blocks.
The filesystem on /dev/chikane/himeko is now 4194304 blocks long.

All done!
/dev/mapper/chikane-himeko
                      16G  12G   4G  75% /backup/himeko

The script should work fine on Redhat-type and Debian-type systems equally. It relies on a consistent naming convention for volumes and volume groups, but this should work fine for most cases. If not, it can be easily tweaked to suit your particular environment. We're considering using this in an automated fashion in future, in response to alerts from our monitoring systems. This would be supremely convenient, perhaps with an email to let the sysadmins know that it's been triggered so they can keep an eye on overall free space.


Related articles

Other pages in similar categories