Resize root disk on Azure VM

Posted under others On By xpk

Azure hard-codes disk size and it cannot be changed during deployment. Resizing root disk on Azure is often a required task. Here is how a root disk can be resized. I learned this the hard way.

Once an instance is launched, shut it down. I use Azure powershell (since azure-cli doesn’t support resizing disk at the time of writing).

It’s a good idea to first look at the partition table before we do anything. This is from a freshly deployed Oracle Linux 6.4. Note that the last 2014 sectors are not used:

Warning! Secondary partition table overlaps the last partition by
33 blocks!
You will need to delete this partition or resize it in another utility.
Disk /dev/sda: 20971520 sectors, 10.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 6CA434CD-5323-4D2A-8C42-51E92E80D8C6
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         1026047   500.0 MiB   8300  Linux filesystem
   2         1026048         5220351   2.0 GiB     8300  Linux filesystem
   3         5220352        20971519   7.5 GiB     8300  Linux filesystem

OK so we’re ready to shutdown the VM and expand the disk. Here, I’m increasing it to 60G. First, setup your azure power shell if this is the first time you run it

PS C:\> get-azurepublishsettingsfile
PS H:\> Import-AzurePublishSettingsFile '.\Microsoft Azure Enterprise-9-17-2015-credentials.publishsettings'

Then query the disk name, stop (deallocate) the VM and resize the azure disk. Start the VM up after.

PS H:\> get-azurevm -servicename "ott-web" -name "HKAZDWEB01" | get-AzureOSDisk

PS H:\>> get-azurevm -servicename "hkazdtest2" -name "HKAZDtest2" | stop-azurevm -force

PS H:\>> update-azuredisk -diskname "hkazdtest2-hkazdtest2-0-201509180651510733" -label " " -resizedSizeInGB 60
VERBOSE: 3:06:05 PM - Begin Operation: Update-AzureDisk

OperationDescription                    OperationId                             OperationStatus
--------------------                    -----------                             ---------------
Update-AzureDisk                        a2cbde6e-8f56-16f9-a479-c89c7236eb34    Succeeded
VERBOSE: 3:06:37 PM - Completed Operation: Update-AzureDisk

PS H:\>> get-azurevm -servicename "hkazdtest2" -name "HKAZDtest2" | start-azurevm

When the VM comes back, we can proceed to the usual partition table editing and filesystem resizing. But here is the fun part. Do not use all the space on the disk. I believe the very last 2014 sectors are used by Azure HyperV. If you include those sectors in your partition, your VM will no longer boot. It may not happen immediately, but it happened to me 5 times when there are some substantial write activities.

Here, I use fdisk to delete the 3rd partition and recreate it. Noticed that I deducted 256 sectors when I put in the ending sector of partition 3. I want to play safe so I used a bit more than 34s. It’s only 128KB, you’re not going to miss it.

Command (m for help): d
Partition number (1-4): 3

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First sector (63-125829119, default 63): 5220352
Last sector, +sectors or +size{K,M,G} (5220352-125829119, default 125829119): 125828864

When it’s done, gdisk no longer complains about partition table problem.

Disk /dev/sda: 125829120 sectors, 60.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): B6EB1FDB-D1AF-4EDF-B639-D692ED072612
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 125829086
Partitions will be aligned on 2048-sector boundaries
Total free space is 2236 sectors (1.1 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         1026047   500.0 MiB   8300  Linux filesystem
   2         1026048         5220351   2.0 GiB     8300  Linux filesystem
   3         5220352       125828864   57.5 GiB    8300  Linux filesystem

Proceed to resizing your filesystem online, and when it’s done, you’ll get a 60G root filesystem.

[[email protected] ~]# df -mhP /
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        57G  2.4G   52G   5% /

Remember to reboot the VM one more time. If it’s going to have problem, better happens sooner than later.

 1,220 total views,  2 views today

1 comment

  1. Spot on with this write-up, I honestly believe this site needs a great deal more attention.
    I’ll probably be back again to read through
    more, thanks for the info!

Leave a comment

Your email address will not be published. Required fields are marked *