Expand Azure VM OS Disk Size (With Managed Disk Support)

This post refers to the ARM model within Azure and discusses how to expand Azure VM OS Disk size for both Managed and Unmanaged disks. This applies to both Windows and Linux based Azure VM’s, but In this post, we will consider only Windows-based Azure VM’s.

Local storage for any Azure VM is comprised of an OS disk, a temporary ephemeral disk, and one or more optional data disks. This applies to all types of Azure VMs.

The OS disk and temporary ephemeral disk are provisioned by default for all Azure VM’s when they are created. You cannot create an Azure VM without an OS disk and an ephemeral temporary disk. However, data disks are optional, and you have the choice of creating and attaching one or more data disks (depending upon the VM type) to an Azure VM either during provisioning time or later.

The default OS disk size for an Azure VM is 127 GB as of the time of this post. However, depending on the VM type and disk type, you can have smaller OS disk sizes (see here: Microsoft Blog: New Smaller Windows Server IaaS Image).

You can change (only expand) the Azure VM OS disk size Manually from within the portal. However, there are times when you may want to automatically expand the Azure VM OS Disk through a script/tool/ application, or do so for a large number of Azure VM’s at once. You won’t be able to reduce the Azure VM OS disk size though because Azure does not support the same as of now

You can quickly expand the OS disk size of any existing Azure VM, irrespective of the Installed OS (Windows Linux), easily through PowerShell. However, this operation will require a restart of the VM and there is no way to avoid the same.

The Information is originally given in the Microsoft Docs for expanding Azure VM OS disk size (Microsoft Docs – Expand OS Disk had some mistakes in the PowerShell code and did not address the way to expand if the OS disk chosen was a Managed Disk. it only presented the way to expand an Unmanaged OS disk size.

However, I forked and edited the original Microsoft Docs article on GitHub, adding documentation sections for expanding the OS disk size if it were a Managed disk. I also made some minor corrections in the existing PowerShell code. My pull request got approved by Microsoft without any changes and merged with the original article. Now when you refer to the Microsoft Docs link mentioned above, you will see a fully updated article with my changes around Managed Disks Incorporated. You can see my changes and approved/merged pull request here: GitHub Pull Request: Microsoft Docs

I also created a ready script for expanding Azure VM OS Disk sizes for both Managed and Unmanaged disks. It is thoroughly tested against production deployments. You can find the same for your perusal in my GitHub repository here: GitHub: Expand-AzureRMVMOSDisk.ps1 or from my account on Microsoft PowerShell Gallery here: PS Gallery: Expand-AzureRMVMOSDiskRunbook

My script linked above takes a Resource Group name and VM name as parameters. It enforces that you are not able to specify an OS disk size more than the upper limit as of now for an OS disk (2TB), and will work for all Azure VM’s having OS disk sizes of 30 GB (which is the minimum default for specific cases) or more.

Since the OS disk expansion operation requires the VM to be in a stopped state, script checks if the Azure VM in context is already stopped or running. If it is running then it will be stopped. Once the operation is completed, Azure VM will be restarted asynchronously (using PS Job) if it were stopped in the script earlier, and not be restarted if it was already in a stopped state when we performed the operation. The Azure VM restart is done asynchronously so that you do not have to wait for the length of time it takes to start an Azure VM (which can be minutes long) and let that happen in the background.

[The below section is specific for Windows-based Azure VM’s only. For Linux-based Azure VM’s I will add specific Information at a later time]

After you resize the OS Disk of a Windows-based Azure VM, when you RDP into the VM, you won’t be able to see the newly expanded OS Disk size. This is so because the differential size by which you have expanded the OS disk would be still unallocated within the Windows OS. You will then need to manually extend the existing OS Disk partition to consume the new unallocated disk space. You can see this in the Disk Management applet within the Windows OS of the Azure VM. In another post, we will see how to do this extending of OS Disk partition automatically through a PowerShell script, without requiring to manually RDP into the VM.

Hope you found this blog post useful. If you have any queries/feedback, please feel free to mention it in the comments section below.

Proadvisor247
Logo