PowerShell – Increase Linux HDD Size

When deploying a Linux VM from template, sometimes you’ll want to increase the standard HDD size to accommodate for application requirements.  Using the script below, that task can be automated using VMWare Tools.  The script assumes:

  1. The template has only 1 HDD attached
  2. LVM is used, and the Volume Group is called VolGroup00
  3. The HDD is increased to 500 GBs
  4. Two new partitions are created – 80 GBs and 16 GBs
  5. The root (or main) partition (“VolGroup00-LogVol03” in the script) will be extended to whatever free space is left
  6. A couple directories are made or modified to provide mountpoints for the new partitions, which are then added to fstab
  7. The script uses my wait-reboot function

If you need to modify the second portion of the script – where the partitions are customized – you can replace all instances of “`n” in the $Script2 variable with linebreaks to make it more readable.

$GuestPW = "cleverpassword"

$Script1 = "echo '1' > /sys/class/scsi_disk/0\:0\:0\:0/device/rescan`necho 'd`n2`nn`np`n2`n`n`n`n`nt`n2`n8e`nw' | fdisk /dev/sda`n`nshutdown -r now"

$Script2 = "pvresize /dev/sda2`n`nlvcreate -L16384 VolGroup00 -n LogVol06`n`nlvcreate -L81920 VolGroup00 -n LogVol07`n`nmkfs.ext3 /dev/mapper/VolGroup00-LogVol06`n`nmkfs.ext3 /dev/mapper/VolGroup00-LogVol07`n`nmkdir /opt/opsware`n`nrm -Rf /media`nmkdir /media`n`nmount /dev/mapper/VolGroup00-LogVol06 /opt/opsware`n`nmount /dev/mapper/VolGroup00-LogVol07 /media`n`necho '/dev/VolGroup00/LogVol06 /opt/opswareext3    defaults  1 2' >> /etc/fstab`necho '/dev/VolGroup00/LogVol07 /mediaext3    defaults  1 2' >> /etc/fstab`n`nlvextend -l +100%FREE /dev/mapper/VolGroup00-LogVol03`n`nresize2fs /dev/mapper/VolGroup00-LogVol03"


Get-HardDisk -vm $VM | Set-Harddisk -CapacityGB 500 -Confirm:$False

Write-Host "Extending partition and rebooting..."
Invoke-VMScript -vm $VM -scripttext $Script1 -ScriptType bash -GuestUser root -GuestPassword $GuestPW

wait-reboot $VM
#Give the OS time to adjust
sleep 60

Write-Host "Creating new partitions"
Invoke-VMScript -vm $VM -scripttext $Script2 -ScriptType bash -GuestUser root -GuestPassword $GuestPW

Leave a Reply