Created on
Azure Local – Extend data disk on virtual machine using code
Virtual machine data disks in Azure Local can be extended with Azure CLI. I’m a fan of every task that can be performed using code over GUI, and here is a simple script written in PowerShell with use of Azure CLI, to perform this task. The script will ask for all required inputs on execution, log into Azure CLI and perform the update if the new disk size is larger than the old one.
Hint: OS disks still have to be extended using Failover Cluster Manager, Hyper-V management tool or PowerShell from a management server or one of the Azure Local nodes.
# Script to extend an Azure Local VM data disk using Azure CLI
# Prerequisites: Azure CLI installed
# Run this script in PowerShell 7
# Azure Local cluster must be running 23H2 2504 (10.2504) or later to support this feature from Azure CLI.
$subscriptionId = read-host "Enter your Azure Subscription ID"
$rgVM = read-host "Enter the Resource Group name where the VM is located"
$vmname = read-host "Enter the VM name"
$diskname = read-host "Enter the disk name to be extended"
$diskSizeGB = read-host "Enter the new disk size in GB - remember to validate it is larger than current size"
#complete az device login
az login
az account set --subscription $subscriptionId
#Install Stack HCI VM Extension
az extension add --name stack-hci-vm
# Get current disk size
$diskinfo = az stack-hci-vm disk show --resource-group $VMrg --name $diskname --query "{id:id,name:name,diskSizeGb:properties.diskSizeGb}" | ConvertFrom-Json
Write-Host "Current disk size is: " $diskinfo.diskSizeGb " GB"
# Only extend if new size is larger than current size
if ($diskSizeGB -gt $diskinfo.diskSizeGb) {
#Extend disk
az stack-hci-vm disk update --name $diskname --resource-group $VMrg --size-gb $diskSizeGB
#Show VM disk info after extension
az stack-hci-vm disk show --resource-group $VMrg --name $diskname --query "{id:id,name:name,diskSizeGb:properties.diskSizeGb}"
Write-Host "Disk extension completed successfully - remember to extend the partition within the OS"
}
else {
Write-Host "New disk size must be larger than current size. Exiting script."
exit
}
If you need to know how to extend an OS disk using PowerShell (from a management server or Azure Local node), here is how:
# Get the VHD/VHDX File Path:
# Use the “Get-VMHardDiskDrive” command to find the path to the VHD/VHDX to expand
Get-VMHardDiskDrive -VMName “VMName”
# Expand the VHD/VHDX File:
# Use the “Resize-VHD” command to resize the disk. Replace “PathToVHD” and “NewSize” with your VMs disk path and new required size
Resize-VHD -Path "C:PathToVHDDiskName.vhdx" -SizeBytes NewSize
# Extend the volume within the VM using disk management or diskpart CMD tool