Automatic unattended expansion of Linux root LVM volume to fill disk

While working on my vRealize Automation 8 project, I wanted to let users specify how large a VM's system drive should be and have vRA apply that without any further user intervention. For instance, if the template has a 60GB C: drive and the user specifies that they want it to be 80GB, vRA will embiggen the new VM's VMDK to 80GB and then expand the guest file system to fill up the new free space.

I'll get into the details of how that's implemented from the vRA side #soon, but first I needed to come up with simple scripts to extend the guest file system to fill the disk.

This was pretty straight-forward on Windows with a short PowerShell script to grab the appropriate volume and resize it to its full capacity:

1$Partition = Get-Volume -DriveLetter C | Get-Partition
2$Partition | Resize-Partition -Size ($Partition | Get-PartitionSupportedSize).sizeMax

It was a bit trickier for Linux systems though. My Linux templates all use LVM to abstract the file systems away from the physical disks, but they may have a different number of physical partitions or different names for the volume groups and logical volumes. So I needed to be able to automagically determine which logical volume was mounted as /, which volume group it was a member of, and which partition on which disk is used for that physical volume. I could then expand the physical partition to fill the disk, expand the volume group to fill the now-larger physical volume, grow the logical volume to fill the volume group, and (finally) extend the file system to fill the logical volume.

I found a great script here that helped with most of those operations, but it required the user to specify the physical and logical volumes. I modified it to auto-detect those, and here's what I came up with:

 1#!/bin/bash
 2# This will attempt to automatically detect the LVM logical volume where / is mounted and then 
 3# expand the underlying physical partition, LVM physical volume, LVM volume group, LVM logical
 4# volume, and Linux filesystem to consume new free space on the disk. 
 5# Adapted from https://github.com/alpacacode/Homebrewn-Scripts/blob/master/linux-scripts/partresize.sh
 6
 7extenddisk() {
 8    echo -e "\n+++Current partition layout of $disk:+++"
 9    parted $disk --script unit s print
10    if [ $logical == 1 ]; then
11        parted $disk --script rm $ext_partnum
12        parted $disk --script "mkpart extended ${ext_startsector}s -1s"
13        parted $disk --script "set $ext_partnum lba off"
14        parted $disk --script "mkpart logical ext2 ${startsector}s -1s"
15    else
16        parted $disk --script rm $partnum
17        parted $disk --script "mkpart primary ext2 ${startsector}s -1s"
18    fi
19    parted $disk --script set $partnum lvm on
20    echo -e "\n\n+++New partition layout of $disk:+++"
21    parted $disk --script unit s print
22    partx -v -a $disk
23    pvresize $pvname
24    lvextend --extents +100%FREE --resize $lvpath 
25    echo -e "\n+++New root partition size:+++"
26    df -h / | grep -v Filesystem
27}
28export LVM_SUPPRESS_FD_WARNINGS=1
29mountpoint=$(df --output=source / | grep -v Filesystem) # /dev/mapper/centos-root
30lvdisplay $mountpoint > /dev/null
31if [ $? != 0 ]; then
32    echo "Error: $mountpoint does not look like a LVM logical volume. Aborting."
33    exit 1
34fi
35echo -e "\n+++Current root partition size:+++"
36df -h / | grep -v Filesystem
37lvname=$(lvs --noheadings $mountpoint | awk '{print($1)}') # root
38vgname=$(lvs --noheadings $mountpoint | awk '{print($2)}') # centos
39lvpath="/dev/${vgname}/${lvname}" # /dev/centos/root
40pvname=$(pvs | grep $vgname | tail -n1 | awk '{print($1)}') # /dev/sda2
41disk=$(echo $pvname | rev | cut -c 2- | rev) # /dev/sda 
42diskshort=$(echo $disk | grep -Po '[^\/]+$') # sda
43partnum=$(echo $pvname | grep -Po '\d$') # 2
44startsector=$(fdisk -u -l $disk | grep $pvname | awk '{print $2}') # 2099200
45layout=$(parted $disk --script unit s print) # Model: VMware Virtual disk (scsi) Disk /dev/sda: 83886080s Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 2048s 2099199s 2097152s primary xfs boot 2 2099200s 62914559s 60815360s primary lvm
46if grep -Pq "^\s$partnum\s+.+?logical.+$" <<< "$layout"; then
47    logical=1
48    ext_partnum=$(parted $disk --script unit s print | grep extended | grep -Po '^\s\d\s' | tr -d ' ')
49    ext_startsector=$(parted $disk --script unit s print | grep extended | awk '{print $2}' | tr -d 's')
50else
51    logical=0
52fi
53parted $disk --script unit s print | if ! grep -Pq "^\s$partnum\s+.+?[^,]+?lvm\s*$"; then
54    echo -e "Error: $pvname seems to have some flags other than 'lvm' set."
55    exit 1
56fi
57if ! (fdisk -u -l $disk | grep $disk | tail -1 | grep $pvname | grep -q "Linux LVM"); then
58    echo -e "Error: $pvname is not the last LVM volume on disk $disk."
59    exit 1
60fi
61ls /sys/class/scsi_device/*/device/rescan | while read path; do echo 1 > $path; done
62ls /sys/class/scsi_host/host*/scan | while read path; do echo "- - -" > $path; done
63extenddisk

And it works beautifully within my environment. Hopefully it'll work for yours too in case you have a similar need!

More Scripts