Expanding an unmanaged disk on Azure linux VM

I have been working on a new project where our VMs are hosted on Microsoft Azure cloud. The default OS disk size for most Linux distros in Azure is 30GB. We needed more space so instead of adding a new virtual disk I ‘ve found out that it is possible to expand the OS disk. When the VMs were initially launched they were launched with unmanaged disks.

You should have in mind that if you need to resize the OS disk that:

  • You will need to stop the VM in order to resize the disk
  • You can not shrink the disk once you had it expand. Disk size reduction is not supported

In the following example the disk of VM will be resized from 30GB to 300GB.

According to official Azure documentation

expand managed disks for a Linux VM with the Azure CLI 2.0. You can also expand the unmanaged OS disk with the Azure CLI 1.0.

Microsoft implies that you can expand unmanaged OS disks only with the Azure CLI 1.0

According to Azure CLI 1.0 instructions for expanding disks for linux VMs when you finish the process they say that you only need to start the server again and you will see the expanded disk without further actions needed. But this does not work.
Fortunately there was a blog post with what needed to be done after you launch the VM.

Install azure cli version 1.0

$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
$ sudo apt-get install -y build-essential nodejs
$ sudo npm install -g azure-cli

 
In order to use the azure cli you will need to login to azure executing

$ azure login
$ azure config mode arm

Stop VM

$ azure vm deallocate -g myResourceGroup -n myVM


info: Executing command vm deallocate
+ Looking up the VM "myVM"
info: vm deallocate command OK

Resize disk on azure

$ azure vm set -g myResourceGroup -n myVM --new-os-disk-size 300


info: Executing command vm set
+ Looking up the VM "myVM"
+ Updating VM "myVM"
info: vm set command OK

Start the VM

$ azure vm start -g myResourceGroup -n myVM


info: Executing command vm start
+ Looking up the VM "myVM"
+ Starting the virtual machine "myVM"
info: vm start command OK

ssh on the VM and execute as root

# fdisk /dev/sda


Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p
Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x92ba6522

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 62914559 62912512 30G 83 Linux

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-629145599, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-629145599, default 629145599):

Created a new partition 1 of type 'Linux' and of size 300 GiB.
Partition #1 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: n

Command (m for help): p

Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x92ba6522

Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 629145599 629143552 300G 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy

Reboot the vm so the partition table is re-read. When the VM is back online login/ssh and as root execute

# resize2fs /dev/sda1


resize2fs 1.43.4 (31-Jan-2017)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 19
The filesystem on /dev/sda1 is now 78642944 (4k) blocks long.

Links

https://docs.microsoft.com/en-us/azure/virtual-machines/linux/expand-disks

https://docs.microsoft.com/en-us/azure/virtual-machines/linux/expand-disks-nodejs

https://docs.microsoft.com/en-us/azure/virtual-machines/linux/expand-disks-nodejs

https://blogs.msdn.microsoft.com/cloud_solution_architect/2016/05/24/step-by-step-how-to-resize-a-linux-vm-os-disk-in-azure-arm/

https://docs.microsoft.com/en-us/azure/xplat-cli-connect

https://docs.microsoft.com/en-us/azure/cli-install-nodejs

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s