Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

How to Resize Azure Managed Disks

·752 words·4 mins· 100 views · 5 likes ·
Azure CLI Get-AzDisk Get-AzSubscription Get-AzVM

In this post, I want to show you how to expand a managed operating system disk in an Azure virtual machine, using Azure PowerShell and Azure CLI. This tutorial assumes that you already have a Microsoft Azure account configured.

Azure PowerShell Workaround>

Azure PowerShell Workaround #

If you want to know how to install the PowerShell Azure module on your machine, check out this link. The simplest way to get started is to sign in interactively at the command line.

Login-AzAccount

This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account. If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task we will use the following commands:

Get-AzSubscription
Select-AzSubscription -Subscription "My Subscription"

Once you set your default subscription, you’re ready to start.

Set the variables>

Set the variables #

Here, we define the characteristics of our environment and the resource’s properties.

$ResourceGroupName = <String>
$VMName = <String>

$vm = Get-AzVM `
    -ResourceGroupName $ResourceGroupName `
    -Name $vmName
Get-AzVM
>

Get-AzVM
#

Deallocate the Virtual Machine>

Deallocate the Virtual Machine #

You must stop the VM before resizing the disk, this operation cannot be performed with the virtual machine running.

Stop-AzVM `
    -ResourceGroupName $ResourceGroupName `
    -Name $vmName
Obtain a reference to the managed OS disk>

Obtain a reference to the managed OS disk #

with the Get-AzDisk cmdlet, you will get the properties of the managed disk that you want to expand.

Get-AzDisk `
    -ResourceGroupName $ResourceGroupName `
    -DiskName $vm.StorageProfile.OsDisk.Name `
    | Select-Object Name,OsType,DiskSizeGB

$osDisk= Get-AzDisk `
    -ResourceGroupName $ResourceGroupName `
    -DiskName $vm.StorageProfile.OsDisk.Name
Azure Managed Disks
>

Azure Managed Disks
#

Update the Disk Size>

Update the Disk Size #

Set the new size of the managed OS disk and update the Disk with the Update-AzDisk cmdlet:

$osDisk.DiskSizeGB = <NewSize>
Update-AzDisk `
    -ResourceGroupName $ResourceGroupName `
    -Disk $osDisk  `
    -DiskName $osDisk.Name
Update-AzDisk
>

Update-AzDisk
#

Check the size of the Disk>

Check the size of the Disk #

Before starting the virtual machine, you can check the disk size using the following command:

Get-AzDisk `
    -ResourceGroupName $ResourceGroupName `
    -DiskName $vm.StorageProfile.OsDisk.Name `
    | Select-Object Name,DiskSizeGB

Get-AzDisk

Start-Up the Virtual Machine>

Start-Up the Virtual Machine #

Once the checks have been made, you can start the virtual machine using the Start-AzVM cmdlet

Start-AzVM `
    -ResourceGroupName $ResourceGroupName `
    -Name $vmName
Azure CLI Workaround>

Azure CLI Workaround #

You can use it in your browser with Azure Cloud Shell, or install it on your machine. If you want to know how to install the Azure CLI, check out this link. The way to get started is to sign in interactively at the command line.

az login

This command will bring up a dialog box prompting you for your email address and password associated with your Azure account. If you have more than one subscription associated with your mail account, you can choose the default subscription. to perform this task we will use the following commands:

az account list
az account set --subscription "Subscription Name"

Once you set your default subscription, you’re ready to start.

Deallocate the Virtual Machine>

Deallocate the Virtual Machine #

You must stop the VM before resizing the disk, this operation cannot be performed with the virtual machine running.

az vm deallocate \
--resource-group <ResourceGroup> \
--name <VMName>

az vm deallocate

Obtain a reference to the managed OS disk>

Obtain a reference to the managed OS disk #

with the following commands, you will get the properties of the managed disk that you want to expand.

az vm list \
--resource-group <ResourceGroup> \
--query "[*].{Name:name,DiskName:storageProfile.osDisk.name}" \
--output table

Az vm list

Update the Disk Size>

Update the Disk Size #

Set the new size of the managed OS disk and update the Disk with the following command:

az disk update \
--resource-group <ResourceGroup> \
--name <DiskName> \
--size-gb <NewSize>

managed disks

Check the size of the Disk>

Check the size of the Disk #

Before starting the virtual machine, you can check the disk size using the following command:

az disk list \
--resource-group <ResourceGroup> \
--query "[?name == '<DiskName>'].{Name:name, DiskSize:diskSizeGb}" \
--output table

Azure Managed Disks

Start-Up the Virtual Machine>

Start-Up the Virtual Machine #

Once the checks have been made, you can start the virtual machine using the following command:

az vm start \
--resource-group <ResourceGroup> \
--name <VMName>

Important: After expanding the os disk, you need to expand the volume within the OS to take advantage of the larger disk.

Thanks for reading my post. I hope you find it useful.

If you want to know more about Azure-managed disks, check out this link.