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 may be able to expand your managed disks without deallocating your VM.

This feature has the following limitations:

  • Only supported for data disks.
  • If a disk is 4 TiB or less, you should deallocate your VM and detach the disk before expanding it beyond 4 TiB. If a disk is already greater than 4 TiB, you can expand it without deallocating the VM and detaching the disk.
  • Not supported for Ultra disks or Premium SSD v2 disks.
  • Not supported for shared disks.
  • Install and use either:
  • Not available on some classic VMs. Use this script to get a list of classic VM SKUs that support expanding without downtime.

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 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.

Detecting a changed disk size

If a data disk was expanded without downtime using the procedure mentioned previously, the disk size will not be changed until the device is rescanned, which normally only happens during the boot process. This rescan can be called on-demand with the following procedure. In this example we have detected using the methods in this document that the data disk is currently /dev/sda and has been resized from 256GB to 512GB.

  1. Identify the currently recognized size on the first line of output from fdisk -l /dev/sda

    root@linux:~# fdisk -l /dev/sda
    Disk /dev/sda: 256 GiB, 274877906944 bytes, 536870912 sectors
    Disk model: Virtual Disk
    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: 0x43d10aad
    
    Device     Boot Start       End   Sectors  Size Id Type
    /dev/sda1        2048 536870878 536868831  256G 83 Linux
    
  2. Insert a 1 character into the rescan file for this device. Note the reference to sda, this would change if a different disk device was resized.

    root@linux:~# echo 1 > /sys/class/block/sda/device/rescan
    
  3. Verify that the new disk size has been recognized

    root@linux:~# fdisk -l /dev/sda
    Disk /dev/sda: 512 GiB, 549755813888 bytes, 1073741824 sectors
    Disk model: Virtual Disk
    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: 0x43d10aad
    
    Device     Boot Start       End   Sectors  Size Id Type
    /dev/sda1        2048 536870878 536868831  256G 83 Linux
    

The remainder of this article uses the OS disk for the examples of the procedure for increasing the size of a volume at the OS level. If the expanded disk 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 device (for example /dev/sda), partition numbers, volume names, mount points, and filesystem formats, as necessary.

All Linux OS guidance should be viewed as generic and may apply on any distribution, but generally matches the conventions of the named marketplace publisher. Reference the Red Hat documents for the package requirements on any distribution claiming Red Hat compatibility, such as CentOS and Oracle.

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 of the OS disk 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:~#

Expanding without downtime classic VM SKU support

If you're using a classic VM SKU, it might not support expanding disks without downtime.

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
       }
   }
}