Automate Extending Azure VM OS Disk Partition Size

Extend OS Disk Partition

In one of my earlier posts (Expand Azure VM OS Disk Size (With Managed Disk Support)) we looked at how to automate expanding the size of a Windows-based Azure VM’s OS Disk through PowerShell script. In this post, we will look at extending Azure VM OS Disk partition size. This does not apply to Linux based Azure RM VM’s.

After resizing the OS Disk of a Windows-based Azure VM when we RDP into the VM, we would not be able to Immediately see (or use) the newly expanded OS Disk size. This is so because the differential size by which we have expanded the OS disk would be still unallocated.

We will then need to manually extend the existing OS Disk partition to consume the new unallocated disk space. We can see and do this in the Disk Management applet within the Windows OS of the Azure VM.

Since we automated the process of resizing the OS Disk in the earlier post, we should Ideally also have this manual process of extending the OS Disk partition automated, without requiring to manually RDP into the Windows-based Azure VM to do the same. Doing this would save us a lot of time and manual effort for a single VM, and exponentially more if we had a bunch of VM’s we wanted to collectively increase the OS Disk size for.

Imagine you have a sudden requirement of having the OS Disk size of all Windows based Azure VM’s in your deployment from default 127 GB (or lower) to 300 GB. Let’s assume you have 100 VM’s in your deployment. How much time would it take to resize the OS Disk for each VM through the PowerShell Script from my earlier related post? – It would be way much faster and quicker than doing the same manually through the Azure Portal. Now after expanding the OS Disk sizes of all VM’s, you still do not have the increased size usable since it will be in an unallocated state. You will need to manually RDP into each of the VM’s to extend the OS Disk  partition to make the new space usable. Imagine doing this manually for 10/100/1000… VM’s!!!

When I looked across the web for any existing implementation examples of automated extending Azure VM OS Disk partition size, unfortunately, I could find none. Hence, I decided to build the same myself.

If we have to automate this task, we need to first create a PowerShell script for extending Azure VM OS Disk partition size by consuming the unallocated disk space available since we increased the OS Disk size earlier. To do so, we need to perform the following steps within our script:

Identify the OS and Boot/System disk (which will be always fixed for Azure VM’s on Windows [“C:\”] but could be different for on-premises VM’s)

  1. Find current size and logical number of the OS partition on the disk
  2. Find size of the unallocated space available (this is the additional size we extended on OS Disk) on the disk
  3. Find the maximum size allowed for the OS partition
  4. Resize the OS Partition to Include the entire Unallocated disk space

I have already created a ready script for extending Azure VM OS Disk partition size with any available unallocated disk space for a Windows machine – It can be a Windows-based Azure VM or an On-premises Windows VM, or a physical Windows server/client. The Intent was to create a script which applies to any kind of Windows OS machine. The script is thoroughly tested against production deployments both on Azure and on-premises. You can find the same for your perusal in my GitHub repository here: GitHub: Extend Windows Os Disk Size or from my Microsoft PowerShell Gallery account here: PS Gallery: Extend-WinOSDiskSize

Now that we have a PowerShell script ready to extend the OS Disk partition, we need to run the script “within” the OS of the target Windows-based Azure VM’s. However, we also want to avoid the time and effort taken to RDP into each VM to run the script manually.

One of the best ways to achieve our objective would be to remotely execute this script on the target Windows-based Azure VM’s through PowerShell Remoting. This remote script execution can be Initiated either from another Windows-based Azure VM in the same Azure Subscription/VNet or from a Runbook in an Azure Automation account. This approach would enable us to target one or more VM’s, whose OS Disk sizes were increased, and automatically extending the underlying OS Disk partition size with the added capacity without manually doing physical RDP into the VM’s and executing the script.

We will see in another future post about how to enable and perform remote execution of PowerShell commands/scripts in Azure RM VM’s, which will help us to remotely execute this script for all target VM’s.

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


Be the first to comment

Leave a Reply

Your email address will not be published.