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:
$Partition = Get-Volume -DriveLetter C | Get-Partition$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:
MBR only
When I cobbled together this script I was primarily targeting the Enterprise Linux (RHEL, CentOS) systems that I work with in my environment, and those happened to have MBR partition tables. This script would need to be modified a bit to work with GPT partitions like you might find on Ubuntu.
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 print10 if [ $logical == 1 ]; then11 parted $disk --script rm $ext_partnum12 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 else16 parted $disk --script rm $partnum17 parted $disk --script "mkpart primary ext2 ${startsector}s -1s"18 fi19 parted $disk --script set $partnum lvm on20 echo -e "\n\n+++New partition layout of $disk:+++"21 parted $disk --script unit s print22 partx -v -a $disk23 pvresize $pvname24 lvextend --extents +100%FREE --resize $lvpath25 echo -e "\n+++New root partition size:+++"26 df -h / | grep -v Filesystem27}28export LVM_SUPPRESS_FD_WARNINGS=129mountpoint=$(df --output=source / | grep -v Filesystem) # /dev/mapper/centos-root30lvdisplay $mountpoint > /dev/null31if [ $? != 0 ]; then32 echo "Error: $mountpoint does not look like a LVM logical volume. Aborting."33 exit 134fi35echo -e "\n+++Current root partition size:+++"36df -h / | grep -v Filesystem37lvname=$(lvs --noheadings $mountpoint | awk '{print($1)}') # root38vgname=$(lvs --noheadings $mountpoint | awk '{print($2)}') # centos39lvpath="/dev/${vgname}/${lvname}" # /dev/centos/root40pvname=$(pvs | grep $vgname | tail -n1 | awk '{print($1)}') # /dev/sda241disk=$(echo $pvname | rev | cut -c 2- | rev) # /dev/sda42diskshort=$(echo $disk | grep -Po '[^\/]+$') # sda43partnum=$(echo $pvname | grep -Po '\d$') # 244startsector=$(fdisk -u -l $disk | grep $pvname | awk '{print $2}') # 209920045layout=$(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 lvm46if grep -Pq "^\s$partnum\s+.+?logical.+$" <<< "$layout"; then47 logical=148 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')50else51 logical=052fi53parted $disk --script unit s print | if ! grep -Pq "^\s$partnum\s+.+?[^,]+?lvm\s*$"; then54 echo -e "Error: $pvname seems to have some flags other than 'lvm' set."55 exit 156fi57if ! (fdisk -u -l $disk | grep $disk | tail -1 | grep $pvname | grep -q "Linux LVM"); then58 echo -e "Error: $pvname is not the last LVM volume on disk $disk."59 exit 160fi61ls /sys/class/scsi_device/*/device/rescan | while read path; do echo 1 > $path; done62ls /sys/class/scsi_host/host*/scan | while read path; do echo "- - -" > $path; done63extenddisk
And it works beautifully within my environment. Hopefully it'll work for yours too in case you have a similar need!