Expand virtual hard disks on a Linux VM

Applies to: ✔️ Linux VMs ✔️ Flexible scale sets

This article describes how to expand managed disks for a Linux virtual machine (VM). You can add data disks to provide for additional storage space, and you can also expand an existing data disk. The default virtual hard disk size for the operating system (OS) is typically 30 GB on a Linux VM in Azure. This article covers expanding either OS disks or data disks.

Warning

Always make sure that your filesystem is in a healthy state, your disk partition table type (GPT or MBR) will support the new size, and ensure your data is backed up before you perform disk expansion operations. For more information, see the Azure Backup quickstart.

Identify Azure data disk object within the operating system

In the case of expanding a data disk when there are several data disks present on the VM, it may be difficult to relate the Azure LUNs to the Linux devices. If the OS disk needs expansion, it will be clearly labeled in the Azure portal as the OS disk.

Start by identifying the relationship between disk utilization, mount point, and device, with the df command.

linux:~ # df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
/dev/sda1                 xfs        97G  1.8G   95G   2% /
<truncated>
/dev/sdd1                 ext4       32G   30G  727M  98% /opt/db/data
/dev/sde1                 ext4       32G   49M   30G   1% /opt/db/log

Here we can see, for example, the /opt/db/data filesystem is nearly full, and is located on the /dev/sdd1 partition. The output of df will show the device path regardless of whether the disk is mounted by device path or the (preferred) UUID in the fstab. Also take note of the Type column, indicating the format of the filesystem. This will be important later.

Now locate the LUN which correlates to /dev/sdd by examining the contents of /dev/disk/azure/scsi1. The output of the following ls command will show that the device known as /dev/sdd within the Linux OS is located at LUN1 when looking in the Azure portal.

linux:~ # ls -alF /dev/disk/azure/scsi1/
total 0
drwxr-xr-x. 2 root root 140 Sep  9 21:54 ./
drwxr-xr-x. 4 root root  80 Sep  9 21:48 ../
lrwxrwxrwx. 1 root root  12 Sep  9 21:48 lun0 -> ../../../sdc
lrwxrwxrwx. 1 root root  12 Sep  9 21:48 lun1 -> ../../../sdd
lrwxrwxrwx. 1 root root  13 Sep  9 21:48 lun1-part1 -> ../../../sdd1
lrwxrwxrwx. 1 root root  12 Sep  9 21:54 lun2 -> ../../../sde
lrwxrwxrwx. 1 root root  13 Sep  9 21:54 lun2-part1 -> ../../../sde1

Expand an Azure Managed Disk

Expand without downtime

You now may be able to expand your managed disks without deallocating your VM.

This feature has the following limitations:

  • Only available with particular VM SKUs.
  • Only supported for data disks.
  • If a disk is 4 TiB or less, you can't expand it beyond 4 TiB without deallocating the VM. If a disk is already greater than 4 TiB, you can expand it without deallocating the VM.
  • Not supported for Ultra disks or Premium SSD v2 disks.
  • Not supported for shared disks.
  • Install and use either:

Use the following PowerShell script to determine which VM SKUs it's available with:

Connect-AzAccount
$subscriptionId="yourSubID"
$location="desiredRegion"
Set-AzContext -Subscription $subscriptionId
$vmSizes=Get-AzComputeResourceSku -Location $location | where{$_.ResourceType -eq 'virtualMachines'}

foreach($vmSize in $vmSizes){
    foreach($capability in $vmSize.Capabilities)
    {
       if(($capability.Name -eq "EphemeralOSDiskSupported" -and $capability.Value -eq "True") -or ($capability.Name -eq "PremiumIO" -and $capability.Value -eq "True") -or ($capability.Name -eq "HyperVGenerations" -and $capability.Value -match "V2"))
        {
            $vmSize.Name
       }
   }
}

To register for the feature, use the following command:

az feature register --namespace Microsoft.Compute --name LiveResize

It may take a few minutes for registration to take complete. To confirm that you've registered, use the following command:

az feature show --namespace Microsoft.Compute --name LiveResize

Expand Azure Managed Disk

Make sure that you have the latest Azure CLI installed and are signed in to an Azure account by using az login.

This article requires an existing VM in Azure with at least one data disk attached and prepared. If you do not already have a VM that you can use, see Create and prepare a VM with data disks.

In the following samples, replace example parameter names such as myResourceGroup and myVM with your own values.

Important

If you've enabled LiveResize and your disk meets the requirements in Expand without downtime, you can skip step 1 and 3.

  1. Operations on virtual hard disks can't be performed with the VM running. Deallocate your VM with az vm deallocate. The following example deallocates the VM named myVM in the resource group named myResourceGroup:

    az vm deallocate --resource-group myResourceGroup --name myVM
    

    Note

    The VM must be deallocated to expand the virtual hard disk. Stopping the VM with az vm stop does not release the compute resources. To release compute resources, use az vm deallocate.

  2. View a list of managed disks in a resource group with az disk list. The following example displays a list of managed disks in the resource group named myResourceGroup:

    az disk list \
        --resource-group myResourceGroup \
        --query '[*].{Name:name,Gb:diskSizeGb,Tier:accountType}' \
        --output table
    

    Expand the required disk with az disk update. The following example expands the managed disk named myDataDisk to 200 GB:

    az disk update \
        --resource-group myResourceGroup \
        --name myDataDisk \
        --size-gb 200
    

    Note

    When you expand a managed disk, the updated size is rounded up to the nearest managed disk size. For a table of the available managed disk sizes and tiers, see Azure Managed Disks Overview - Pricing and Billing.

  3. Start your VM with az vm start. The following example starts the VM named myVM in the resource group named myResourceGroup:

    az vm start --resource-group myResourceGroup --name myVM
    

Expand a disk partition and filesystem

Note

While there are many tools that may be used for performing the partition resizing, the tools detailed in the remainder of this document are the same tools used by certain automated processes such as cloud-init. As described here, the growpart tool with the gdisk package provides universal compatibility with GUID Partition Table (GPT) disks, as older versions of some tools such as fdisk did not support GPT.

The remainder of this article describes uses the OS disk for the examples of the procedure for increasing the size of a volume at the OS level. If the disk which needs to be expanded is a data disk, use the previous guidance for identifying the data disk device, and follow these instructions as a guideline, substituting the data disk (for example /dev/sda), partition numbers, volume names, mount points, and filesystem formats, as necessary.

Increase the size of the OS disk

The following instructions apply to endorsed Linux distributions.

Note

Before you proceed, make a full backup copy of your VM, or at a minimum take a snapshot of your OS disk.

On Ubuntu 16.x and newer, the root partition and filesystems will be automatically expanded to utilize all free contiguous space on the root disk by cloud-init, provided there is a small bit of free space for the resize operation. For this circumstance the sequence is simply

  1. Increase the size of the OS disk as detailed previously
  2. Restart the VM, and then access the VM using the root user account.
  3. Verify that the OS disk now displays an increased file system size.

As shown in the following example, the OS disk has been resized from the portal to 100 GB. The /dev/sda1 file system mounted on / now displays 97 GB.

user@ubuntu:~# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  314M     0  314M   0% /dev
tmpfs          tmpfs      65M  2.3M   63M   4% /run
/dev/sda1      ext4       97G  1.8G   95G   2% /
tmpfs          tmpfs     324M     0  324M   0% /dev/shm
tmpfs          tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs          tmpfs     324M     0  324M   0% /sys/fs/cgroup
/dev/sda15     vfat      105M  3.6M  101M   4% /boot/efi
/dev/sdb1      ext4       20G   44M   19G   1% /mnt
tmpfs          tmpfs      65M     0   65M   0% /run/user/1000
user@ubuntu:~#