Step by Step: how to resize a Linux VM OS disk in Azure (ARM)

Update 06/18/2018: This article has been superseded by this one from Azure support: https://blogs.msdn.microsoft.com/linuxonazure/2017/04/03/how-to-resize-linux-osdisk-partition-on-azure

The default OS disk size for most Linux distros in Azure is 30GB. While Linux makes it easy to add other disks as mount points, you may wish to increase the size of the OS disk using the steps in this article.

Here’s what you need to do. I used a CentOS 6.8 Linux VM from the Azure Marketplace in this example. Default filesystem is ext4. With CentOS/RHEL 7.x, the default file system is XFS. On Ubuntu it is not necessary to do steps 2-11 as it automatically will resize the disk on boot.

Note: Before proceeding it’s highly recommended that you backup your Azure VM first. You can do this using Azure Backup or use AzCopy to make a copy of your VHD.

1) Resize the OS disk using these PowerShell cmdlets or the Azure CLI. The VM needs to be in stopped (deallocated) state to run these commands

PowerShell:

$rg = “YourResourceGroupName”

$vmName = “YourVMName”

$vm = Get-AzureRmVM -ResourceGroupName $rg -Name $vmName

$vm.StorageProfile[0].OSDisk[0].DiskSizeGB = 127  # change the size as required

Update-AzureRmVM –ResourceGroupName $rg -VM $vm

Azure CLI:

az vm update --resource-group YourResourceGroupName --name YourVMName --set storageProfile.osDisk.diskSizeGB=1024

2) Start your Linux VM. Login to your Azure VM using SSH:
image

As you can see the OS disk is 30GB.

3) Run this command: sudo fdisk /dev/sda

4) Type “u” to change the units to sectors.

5) Type “p” to list the current partition details. Note the starting sector (e.g. 2048).

6) Once you are in fdisk, delete the partition (note: you aren’t deleting the data, just altering the partition table). Type “d” and then select the partition (if required as it will choose partition 1 if it’s the only partition).

7) Create a new partition with “n”. Type p to create a primary partition. Type 1 to create the first partition (or another partition number, if required). Use the same starting sector from step 5 and use the desired end sector or accept the default end sector to use the entire disk.

8) Type “a” and select partition 1 to mark the boot partition as active. Type “p” to to ensure all settings are correct:

image

9) Write the partition with “w”. You will get a warning that says: WARNING: Re-reading the partition table failed with error 16: Device or resource busy. This is normal.

10) Reboot using “sudo reboot”

11) Once the VM is up and running, login to your Azure VM using SSH and type “sudo resize2fs /dev/sdaX” to resize the filesystem for CentOS/RHEL 6.x (where X is the partition number you created in step 7. In CentOS/RHEL 7.x the command is “xfs_growfs -d /dev/sdaX”. This may take some time to complete.

12) Verify the new size with df -h

image

Now go and enjoy your new bigger OS disk!

Updated 1/3/2017: Thanks to Terry Charles for noting that step 8 to mark the boot partition was inadvertently omitted. Also, thanks to rhelguy and Sherif Adel for the correct resize command for CentOS/RHEL 7.x.