Expand virtual hard disks on a Linux VM

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

This article covers expanding OS disks and data disks for a Linux virtual machine (VM). You can add data disks to provide for more 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. You can't expand the size of striped volumes.

An OS disk has a maximum capacity of 4,095 GiB. However, many operating systems are partitioned with master boot record (MBR) by default. MBR limits the usable size to 2 TiB. If you need more than 2 TiB, consider attaching data disks for data storage. If you do need to store data on the OS disk and require extra space, convert it to GUID Partition Table (GPT).

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

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

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 shows the device path whether the disk is mounted using the device path or the (preferred) UUID in the fstab. Also take note of the Type column, indicating the format of the filesystem. The format is important later.

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

sudo 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 can expand your managed disks without deallocating your VM. The host cache setting of your disk doesn't change whether or not you can expand a data disk without deallocating your VM.

This feature has the following limitations:

  • Only supported for data disks.
  • If a Standard HDD, Standard SSD, or Premium SSD disk is 4 TiB or less, deallocate your VM and detach the disk before expanding it beyond 4 TiB. If one of those disk types is already greater than 4 TiB, you can expand it without deallocating the VM and detaching the disk. This doesn't apply to Premium SSD v2 or Ultra 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 don't already have a VM that you can use, see Create and prepare a VM with data disks.

Note

Before you can use Azure CLI in Microsoft Azure operated by 21Vianet, please run az cloud set -n AzureChinaCloud first to change the cloud environment. If you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud again.

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.

Shrinking an existing disk isn’t supported and may result in data loss.

After expanding the disks, you need to expand the volume in the operating system to take advantage of the larger disk.

  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 doesn't 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,size:diskSizeGB,Tier:sku.tier}' \
        --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 Understand Azure Disk Storage 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 reported disk size doesn't change 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 find using the methods in this document that the data disk is currently /dev/sda and was resized from 256 GiB to 512 GiB.

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

    sudo 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 in the example. The disk identifier would change if a different disk device was resized.

    echo 1 | sudo tee /sys/class/block/sda/device/rescan
    
  3. Verify that the new disk size is now recognized

    sudo 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 based on Red Hat or claiming Red Hat compatibility.

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 are automatically expanded to utilize all free contiguous space on the root disk by cloud-init, provided there's a small bit of free space for the resize operation. In this case, 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 was resized from the portal to 100 GB. The /dev/sda1 file system mounted on / now displays 97 GB.

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 -Environment AzureChinaCloud
$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
       }
   }
}