Skip to content

Instantly share code, notes, and snippets.

@salekseev
Created February 17, 2016 16:30
Show Gist options
  • Save salekseev/628859a121bedd616264 to your computer and use it in GitHub Desktop.
Save salekseev/628859a121bedd616264 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script will check if there is unallocated space in the volume group
# and will attempt to expand logical volume and file system using resize2fs
# Author: Stas Alekseev <[email protected]>
fs_mount="/var"
vg_minfree_mb="100000"
lv_maxsize_mb="27000"
warn () {
echo "$0:" "$@" >&2
}
die () {
rc=$1
shift
warn "$@"
exit $rc
}
show_help() {
cat << EOF
Usage: ${0##*/} [-h] [-f filesystem_mountpoint] [-v vg_minsize] [-l lv_maxsize]
This script will check if there is unallocated space in the volume group
and will attempt to expand logical volume and file system using resize2fs.
-h display this help and exit
-f filesystem_mountpoint expand this filesystem (default: ${fs_mount})
-v vg_minsize minimum number of megabytes availabe in volume group (default: ${vg_minfree_mb})
-l lv_maxsize expand only if logical volume is less then ${lv_maxsize_mb} (default) megabytes
EOF
}
while getopts "hf:v:l:" opt; do
case "$opt" in
h)
show_help
exit 0
;;
f) fs_mount=$OPTARG
;;
v) vg_minfree_mb=$OPTARG
;;
l) lv_maxsize_mb=$OPTARG
;;
'?')
show_help >&2
exit 1
;;
esac
done
if [[ $UID != 0 ]]; then
echo "Root access not obtained! Exiting."
exit 1
fi
fs_actual_mount=$( cat /proc/mounts | awk '{print $2,$1,$3}' | grep -E "^$fs_mount " ) ||
die 1 "$fs_mount mount not found, check if it's mounted."
fs_device=$( echo "$fs_actual_mount" | awk '{print $2}' )
lvs=$( lvs -o vg_name,vg_size,vg_free,lv_name,lv_size,lv_dm_path --units m --separator "|" --noheadings --nosuffix | grep "$fs_device" ) ||
die 1 "Logical volume for $fs_mount not found."
vg_free=$( echo "$lvs" | awk 'BEGIN { FS = "|" } ; { print $3 }' | cut -d. -f1 )
lv_size=$( echo "$lvs" | awk 'BEGIN { FS = "|" } ; { print $5 }' | cut -d. -f1 )
if [ "$vg_free" -lt "$vg_minfree_mb" ]
then
die 1 "Not enough space in Volume Group"
fi
if [ "$lv_size" -gt "$lv_maxsize_mb" ]
then
die 0 "Logical Volume for $fs_mount is already $lv_size MB which is bigger then $lv_maxsize_mb MB maximum size for this script."
fi
fs_type=$(echo $fs_actual_mount | awk '{print $3}')
if [[ "$fs_type" != "ext4" && "$fs_type" != "ext3" ]]
then
die 1 "I do not know how to exand this filesystem: $fs_type"
fi
lvextend -r -L ${lv_maxsize_mb}M $fs_device || die 1 "Failed to extend Logical Volume for $fs_mount"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment