Extend-Resize Microsoft Linux Azure Data Disk & OS Disk

Extend-Resize Microsoft Linux Azure Data Disk & OS Disk

To extend-resize Microsoft Linux Azure Data Disk & OS Disk,  the Powershell method can be used for both Classic and ARM models. By default, the OS disk will be 30 GB for Virtual Machines, which may not be sufficient as the ore data is started to get saved into the OS drive.

PLEASE MAKE SURE THAT, YOU ARE HAVING A VALID BACKUP AVAIALABLE FOR YOUR VIRTUAL MACHINE BEFORE EXECUTING THE BELOW STEPS

Extend Data Disk using Powershell – Classic Mode

To extend a Data disk in an Azure Virtual Machine in Classic Mode, we need to perform the below steps,

  1. Open the Windows Powershell ISE and execute the below script
# Authenticate to Azure Account
 
Add-AzureAccount

# Select Azure Subscription
 
$subscription = (Get-AzureSubscription).SubscriptionName | Out-GridView ` 
        -Title "Select Azure Subscription" `
         -PassThru
 
Select-AzureSubscription -SubscriptionName $subscription -Current

# Now select the Azure Storage Account
 
$storageAccount = (Get-AzureStorageAccount).Label | Out-GridView `
        -Title "Select Azure Storage Account" `
        -PassThru
 
Set-AzureSubscription -SubscriptionName $subscription -CurrentStorageAccountName $storageAccount

# Select Azure VM
 
$vm = Get-AzureVM | Out-GridView -Title "Select your virtual machine" `
         -PassThru

# Select Data Disk to resize
 
$disk = $vm | Get-AzureDataDisk | Out-GridView `
        -Title "Select the data disk to resize" -PassThru
 
$datadiskName = $disk.DiskName

# Specify new Data Disk size in GB, which should be larger than the current disk size
 
do {
 
    $size = 
        Read-Host -Prompt "Specify the new size in GB"

}

until ( $size -gt $disk.LogicalDiskSizeInGB )

# Stop and Deallocate VM prior to resizing data disk
 
$vm | Stop-AzureVM -Force

# Resize Data Disk to Larger Size
 
Update-AzureDisk -Label "$datadiskName" -DiskName "$datadiskName" -ResizedSizeInGB $size

# Start your Virtual Machine

$vm | Start-AzureVM

Extend Data Disk using powershell – ARM Mode

To extend a Data disk in an Azure Virtual Machine in ARM Mode, we need to perform the below steps,

  1. Open the Windows Powershell ISE and execute the below script
# Authenticate to Azure Account
 
Login-AzureRmAccount

# Select Azure Subscription

$subscription = (Get-AzureRmSubscription).SubscriptionName | Out-GridView ` 
        -Title "Select Azure RM Subscription" `
         -PassThru
 
Select-AzureRmSubscription -SubscriptionName $subscription -Current

$rgName = 'Enter your Resource Group Name'
$vmName = 'Enter your VM Name'

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

Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName

$vm.StorageProfile.DataDisks[0].DiskSizeGB = <Enter your Disk Size in GB>

# Example,  $vm.StorageProfile.DataDisks[0].DiskSizeGB = 1023
# In the above example, Data Disk is the first one and new size is 1023 GB

Update-AzureRmVM -ResourceGroupName $rgName -VM $vm

Start-AzureRmVM -ResourceGroupName $rgName -Name $vmName

Extend OS Disk using Powershell – Classic Mode

To extend a OS disk in an Azure Classic Mode Virtual Machine, we need to perform the below steps,

Open the Windows Powershell ISE and execute the below script

# Authenticate to Azure Account
 
Add-AzureAccount

# Select Azure Subscription
 
$subscription = (Get-AzureSubscription).SubscriptionName | Out-GridView ` 
        -Title "Select Azure Subscription" `
         -PassThru
 
Select-AzureSubscription -SubscriptionName $subscription -Current

# Now select the Azure Storage Account
 
$storageAccount = (Get-AzureStorageAccount).Label | Out-GridView `
        -Title "Select Azure Storage Account" `
        -PassThru
 
Set-AzureSubscription -SubscriptionName $subscription -CurrentStorageAccountName $storageAccount

# Get the Azure VM OS Disk Details

Get-AzureVM -ServiceName “<Enter your Cloud Service Name>” -Name “<Enter your Virtual Machine Name>” | Get-AzureOSDisk

# Select Azure VM
 
$vm = Get-AzureVM | Out-GridView -Title "Select your virtual machine" `
         -PassThru

# Select Data Disk to resize
 
$disk = $vm | Get-AzureOSDisk | Out-GridView `
        -Title "Select the OS disk to resize" -PassThru
 
$osdiskName = $disk.DiskName

# Specify new Data Disk size in GB, which should be larger than the current disk size
 
do {
 
    $size = 
        Read-Host -Prompt "Specify the new size in GB"

}

until ( $size -gt $disk.LogicalDiskSizeInGB )

# Stop and Deallocate VM prior to resizing OS disk
 
$vm | Stop-AzureVM -Force

# Resize OS Disk to a Larger Size
 
Update-AzureDisk -Label "$osdiskName" -DiskName "$osdiskName" -ResizedSizeInGB $size

# Start your Virtual Machine

$vm | Start-AzureVM

Extend OS Disk using Powershell – ARM Mode

To extend a OS disk in an Azure ARM mode Virtual Machine, we need to perform the below steps,

Open the Windows Powershell ISE and execute the below script

# Authenticate to Azure Account
 
Login-AzureRmAccount

# Select Azure Subscription

$subscription = (Get-AzureRmSubscription).SubscriptionName | Out-GridView ` 
        -Title "Select Azure RM Subscription" `
         -PassThru
 
Select-AzureRmSubscription -SubscriptionName $subscription -Current

$rgName = 'Enter your Resource Group Name'
$vmName = 'Enter your VM Name'

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

Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName

$vm.StorageProfile.OSDisk.DiskSizeGB = <Enter your Disk Size in GB>

Update-AzureRmVM -ResourceGroupName $rgName -VM $vm

Start-AzureRmVM -ResourceGroupName $rgName -Name $vmName

Extend the File System Volume Size of Linux VM

Login to your Azure VM using SSH via putty or terminal

By default, in Azure the linux virtual machines will have an OS disk of size 30GB. To resize the OS disk(root drive), please execute the below command

1) Run the command,

sudo fdisk /dev/sda

Extend-Resize Microsoft Linux Azure Data Disk & OS Disk2) Press the letter ‘u’ to change the units to sectors.

Extend-Resize Microsoft Linux Azure Data Disk & OS Disk3) Now type the letter ‘p’ to list the partition infomormation. Note the starting sector (e.g. 2048).

Extend-Resize Microsoft Linux Azure Data Disk & OS Disk4) Now we can proceed with deleting the partition table. From the fdisk window, delete the partition.  Press the letter ‘d’ and then select the partition by entering the partition number. By default, it will choose the number  1(Assuming you are modifying OS drive).

In reality, you are not deleting the DATA, but rather modifying the partition table

Extend-Resize Microsoft Linux Azure Data Disk & OS Disk5) Now we need to create a new partition. To create a new partition press the letter ‘n’.

6) Type the letter ‘p’ to create a primary partition.

7) Now you enter the partition number. Type 1 to create the first partition (or another partition number, if required). Use the same starting sector from step 3 and enter a end sector value of your wish or just accept the default end sector value to select the entire disk.

Extend-Resize Microsoft Linux Azure Data Disk & OS Disk8) Type the letter ‘p’ to to ensure all settings are correct. It will print the values to screen.

9)  To save your changes and write to the disc, press ‘w’.

Note : You may get a warning that says:

WARNING: Re-reading the partition table failed with error 16: Device or resource busy. 

Ignore the message, since it is not critical

10) Reboot the vitual machine using by typing the command,

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.

Extend-Resize Microsoft Linux Azure Data Disk & OS Disk12) Verify the new size with df -h

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.