Skip to content

Instantly share code, notes, and snippets.

@cherts
Last active October 24, 2022 08:07
Show Gist options
  • Select an option

  • Save cherts/2575631103c19a8bcd73f36f176fe82b to your computer and use it in GitHub Desktop.

Select an option

Save cherts/2575631103c19a8bcd73f36f176fe82b to your computer and use it in GitHub Desktop.
Resize LVM disk and partitions
#!/bin/bash
#
# Program: Resize LVM disk and partition <resize_disk.sh>
#
# Author: Mikhail Grigorev <sleuthhound at gmail dot com>
#
# Current Version: 1.0.2
#
# License:
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
DISK=sdb
PART=1
PV=${DISK}${PART}
VG=vmstorage
LV=vmstorage
MPOINT=/var/lib/vmstorage
echo "== BEFORE =================================================================="
if [ -d "${MPOINT}" ]; then
df -h ${MPOINT}
echo
else
echo "ERROR: Mount point '${MPOINT}' not found."
exit 1
fi
if [ -b "/dev/${DISK}" ]; then
lsblk /dev/${DISK}
echo
fi
echo "== RESIZE =================================================================="
if [ -b "/dev/${DISK}" ]; then
echo "Rescan device '${DISK}'..."
echo 1 > /sys/block/${DISK}/device/rescan
else
echo "ERROR: Disk '${DISK}' not found."
exit 1
fi
if [ -b "/dev/${DISK}${PART}" ]; then
echo "Resize partition '${PART}' on disk '${DISK}'..."
growpart /dev/${DISK} ${PART}
echo "Resize physical volume '${PV}'..."
pvresize /dev/${PV}
else
echo "Resize physical volume '${DISK}'..."
pvresize /dev/${DISK}
fi
if [ -b "/dev/${VG}/${LV}" ]; then
echo "Resize logical volume '/dev/${VG}/${LV}'..."
lvextend -r -l +100%FREE /dev/${VG}/${LV}
else
echo "ERROR: Logical volume '/dev/${VG}/${LV}' not found."
exit 1
fi
echo "== AFTER ==================================================================="
df -h ${MPOINT}
echo
lsblk /dev/${DISK}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment