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.

Comments

  • Anonymous
    June 07, 2016
    Great article! With CentOS >7 the filesystem used is XFS by default so the commands used to resize the disk is a little bit different.
  • Anonymous
    August 16, 2016
    Use 'xfs_growfs -d /dev/sdaX' instead of 'resize2fs /dev/sdaX' for RHEL 7.
  • Anonymous
    November 10, 2016
    HiIt is probably worth mentioning that some Linux distributions need to boot flag setting (by using the a command and partition number) before you write the configuration back and reboot
  • Anonymous
    November 16, 2016
    If you have and "XFS" file system, like by default in CentOS, you should use " xfs_growfs" instead of "resize2fs" command.I think this would be helpful to update in the document :)
  • Anonymous
    January 18, 2017
    Great!!!, thank you very much, plese, but to Centos 7 ???
  • Anonymous
    April 13, 2017
    This article is helpful, but what a pain for large customers to do this on all their virtual machines. It would be nice if Microsoft allowed users to initially provision a virtual machine with the disk size of the customer's choosing. (like AWS and Google)
  • Anonymous
    June 29, 2017
    Followed the step in rhel 7. after rebooting not able to ssh into the server.
    • Anonymous
      July 12, 2017
      Same issue here. My Centos 6.9 wont boot up after hit reboot command at Step 10.
    • Anonymous
      July 27, 2017
      The same here
  • Anonymous
    July 24, 2017
    You can avoid the rebooting step by using the partx tool to inform the kernel about the change.After increasing the partition. Run partx -u. And the grow of the fs does succeed partx -u /dev/sda2Before the change--------------------- xfs_info /dev/sda2meta-data=/dev/sda2 isize=512 agcount=4, agsize=1934016 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0data = bsize=4096 blocks=7736064, imaxpct=25 = sunit=0 swidth=0 blksnaming =version 2 bsize=4096 ascii-ci=0 ftype=1log =internal bsize=4096 blocks=3777, version=2 = sectsz=512 sunit=0 blks, lazy-count=1realtime =none extsz=4096 blocks=0, rtextents=0Grow the FS-----------------xfs_growfs -d /dev/sda2meta-data=/dev/sda2 isize=512 agcount=4, agsize=1934016 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0data = bsize=4096 blocks=7736064, imaxpct=25 = sunit=0 swidth=0 blksnaming =version 2 bsize=4096 ascii-ci=0 ftype=1log =internal bsize=4096 blocks=3777, version=2 = sectsz=512 sunit=0 blks, lazy-count=1realtime =none extsz=4096 blocks=0, rtextents=0data blocks changed from 7736064 to 12978944After the change-------------------xfs_info /dev/sda2meta-data=/dev/sda2 isize=512 agcount=7, agsize=1934016 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0data = bsize=4096 blocks=12978944, imaxpct=25 = sunit=0 swidth=0 blksnaming =version 2 bsize=4096 ascii-ci=0 ftype=1log =internal bsize=4096 blocks=3777, version=2 = sectsz=512 sunit=0 blks, lazy-count=1realtime =none extsz=4096 blocks=0, rtextents=0