Edit

Share via


Expand virtual hard disks on a Linux VM

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

This article covers expanding operating system (OS) disks and data disks for a Linux virtual machine (VM). You can add data disks to provide more storage space, and you can also expand an existing data disk. The default virtual hard disk size for the 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 a GUID Partition Table (GPT).

Warning

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

Identify an Azure data disk object within the operating system

When you expand a data disk that has several data disks on the VM, it might be difficult to relate the Azure logical unit numbers (LUNs) to the Linux devices. If the OS disk needs expansion, it's 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 you can see, for example, that 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 by using the device path or the (preferred) UUID in the fstab. Note the Type column, which indicates filesystem format. 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 you look 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.

Important

This limitation doesn't apply to Premium SSD v2 or Ultra Disks.

If a Standard HDD, Standard SSD, or Premium SSD disk is 4 TiB or less, deallocate your VM and detach the disk before you expand 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.

  • Is supported only for data disks.
  • Isn't supported for shared disks.
  • Must be installed and use one of the following options:
  • Isn't available on some classic VMs. Use this script to get a list of classic VM products that support expanding without downtime.

Expand with Ultra Disks and Premium SSD v2

Expanding Ultra Disks and Premium SSD v2 disks without downtime has the following additional limitations:

  • You can't expand a disk while a background copy of data is also occurring on that disk. An example is when a disk is being backfilled from snapshots.
  • You can expand VMs by using NVMe controllers with Ultra Disks or Premium SSD v2 disks without downtime with this public preview. Because this release is a public preview, you should use it only to test the functionality of expanding without downtime. Don't expand VMs in production.

In the following regions, you can expand VMs that are using NVMe controllers with Ultra Disks or Premium SSD v2 disks without downtime. Use either the Azure portal, the Azure CLI, or the Azure PowerShell module:

  • Southeast Asia
  • Brazil South
  • Canada Central
  • Germany West Central
  • Central India (not currently supported on V6 VMs)

In the following regions, you can expand VMs that are using NVMe controllers with Ultra Disks or Premium SSD v2 disks without downtime. Use only the Azure CLI or an Azure PowerShell module. You can't currently use the Azure portal:

  • East Asia
  • West Central US (not currently supported on V6 VMs)

Allow up to 10 minutes for the correct size to be reflected in Windows VMs and Linux VMs. For Linux VMs, you must perform a Linux rescan function. For a Windows VM that doesn't have a workload, you must perform a Windows rescan function. You can rescan immediately, but if the time is within 10 minutes, you might need to rescan again to display the correct size.

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.

In the following samples, replace the placeholder parameter names like myResourceGroup and myVM with your own values.

Important

If your disk meets the requirements in Expand without downtime, you can skip steps 1 and 3.

Shrinking an existing disk isn't supported and might result in data loss.

After you expand the disks, expand the volume in the OS 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
    

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

    When you expand a managed disk, the updated size is rounded up to the nearest managed disk size.

  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

You can use many tools to perform partition resizing. The tools detailed in the remainder of this article are the same ones that certain automated processes use, such as cloud-init. As described here, the growpart tool with the gdisk package provides universal compatibility with GPT disks because older versions of some tools such as fdisk didn't support GPT.

Detect a changed disk size

If you used the previously mentioned procedure to expand a data disk without downtime, the reported disk size doesn't change until the device is rescanned. Rescanning normally happens only during the boot process. To call this rescan on demand, use the following procedure. When you use the methods in this article, notice that in this example 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 changes if a different disk device is 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 to increase the size of a volume at the OS level. If the expanded disk is a data disk, use the previous guidance to identify the data disk device. Follow these instructions as a guideline. Substitute the data disk device (for example, /dev/sda), partition numbers, volume names, mount points, and filesystem formats, as necessary.

Consider all Linux OS guidance as generic and that it might apply on any distribution, but it generally matches the conventions of the named marketplace publisher. See the Red Hat documentation for the package requirements on any distribution based on Red Hat or that claims Red Hat compatibility.

Increase the size of the OS disk

The following instructions apply to endorsed Linux distributions.

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 use all free contiguous space on the root disk by cloud-init. A small amount of free space must be available for the resize operation. In this case, the sequence is to:

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

As shown in the following example, the OS disk was resized from the portal to 100 GB. The /dev/sda1 filesystem 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:~#

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