Tutorial - Manage Azure disks with the Azure CLI

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

Azure virtual machines (VMs) use disks to store the operating system, applications, and data. When you create a VM, it is important to choose a disk size and configuration appropriate to the expected workload. This tutorial shows you how to deploy and manage VM disks. You learn about:

  • OS disks and temporary disks
  • Data disks
  • Standard and Premium disks
  • Disk performance
  • Attaching and preparing data disks
  • Disk snapshots

Default Azure disks

When an Azure virtual machine is created, two disks are automatically attached to the virtual machine.

Operating system disk - Operating system disks can be sized up to 2 TB, and hosts the VMs operating system. The OS disk is labeled /dev/sda by default. The disk caching configuration of the OS disk is optimized for OS performance. Because of this configuration, the OS disk should not be used for applications or data. For applications and data, use data disks, which are detailed later in this tutorial.

Temporary disk - Temporary disks use a solid-state drive that is located on the same Azure host as the VM. Temp disks are highly performant and may be used for operations such as temporary data processing. However, if the VM is moved to a new host, any data stored on a temporary disk is removed. The size of the temporary disk is determined by the VM size. Temporary disks are labeled /dev/sdb and have a mountpoint of /mnt.

Azure data disks

To install applications and store data, additional data disks can be added. Data disks should be used in any situation where durable and responsive data storage is desired. The size of the virtual machine determines how many data disks can be attached to a VM.

VM disk types

Azure provides two types of disks.

Standard disks - backed by HDDs, and delivers cost-effective storage while still being performant. Standard disks are ideal for a cost effective dev and test workload.

Premium disks - backed by SSD-based, high-performance, low-latency disk. Perfect for VMs running production workload. VM sizes with an S in the size name, typically support Premium Storage. For example, DS-series, DSv2-series, GS-series, and FS-series VMs support premium storage. When you select a disk size, the value is rounded up to the next type. For example, if the disk size is more than 64 GB, but less than 128 GB, the disk type is P10.


Premium SSD sizes  P1 P2 P3 P4 P6 P10 P15 P20 P30 P40 P50 P60 P70 P80
Disk size in GiB 4 8 16 32 64 128 256 512 1,024 2,048 4,096 8,192 16,384 32,767
Base provisioned IOPS per disk 120 120 120 120 240 500 1,100 2,300 5,000 7,500 7,500 16,000 18,000 20,000
**Expanded provisioned IOPS per disk N/A N/A N/A N/A N/A N/A N/A N/A 8,000 16,000 20,000 20,000 20,000 20,000
Base provisioned Throughput per disk 25 MB/s 25 MB/s 25 MB/s 25 MB/s 50 MB/s 100 MB/s 125 MB/s 150 MB/s 200 MB/s 250 MB/s 250 MB/s 500 MB/s 750 MB/s 900 MB/s
**Expanded provisioned throughput per disk N/A N/A N/A N/A N/A N/A N/A N/A 300 MB/s 600 MB/s 900 MB/s 900 MB/s 900 MB/s 900 MB/s
Max burst IOPS per disk 3,500 3,500 3,500 3,500 3,500 3,500 3,500 3,500 30,000* 30,000* 30,000* 30,000* 30,000* 30,000*
Max burst throughput per disk 170 MB/s 170 MB/s 170 MB/s 170 MB/s 170 MB/s 170 MB/s 170 MB/s 170 MB/s 1,000 MB/s* 1,000 MB/s* 1,000 MB/s* 1,000 MB/s* 1,000 MB/s* 1,000 MB/s*
Max burst duration 30 min 30 min 30 min 30 min 30 min 30 min 30 min 30 min Unlimited* Unlimited* Unlimited* Unlimited* Unlimited* Unlimited*
Eligible for reservation No No No No No No No No Yes, up to one year Yes, up to one year Yes, up to one year Yes, up to one year Yes, up to one year Yes, up to one year

*Applies only to disks with on-demand bursting enabled.
** Only applies to disks with performance plus (preview) enabled.

When you provision a premium storage disk, unlike standard storage, you are guaranteed the capacity, IOPS, and throughput of that disk. For example, if you create a P50 disk, Azure provisions 4,095-GB storage capacity, 7,500 IOPS, and 250-MB/s throughput for that disk. Your application can use all or part of the capacity and performance. Premium SSD disks are designed to provide low single-digit millisecond latencies and target IOPS and throughput described in the preceding table 99.9% of the time.

While the above table identifies max IOPS per disk, a higher level of performance can be achieved by striping multiple data disks. For instance, 64 data disks can be attached to Standard_GS5 VM. If each of these disks is sized as a P30, a maximum of 80,000 IOPS can be achieved. For detailed information on max IOPS per VM, see VM types and sizes.

Launch Azure Cloud Shell

Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account.

To open Cloud Shell, select Try it from the upper right corner of a code block. You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com/powershell. Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press enter to run it.

Create and attach disks

Data disks can be created and attached at VM creation time or to an existing VM.

Attach disk at VM creation

Create a resource group with the az group create command.

az group create --name myResourceGroupDisk --location eastus

Create a VM using the az vm create command. The following example creates a VM named myVM, adds a user account named azureuser, and generates SSH keys if they do not exist. The --datadisk-sizes-gb argument is used to specify that an additional disk should be created and attached to the virtual machine. To create and attach more than one disk, use a space-delimited list of disk size values. In the following example, a VM is created with two data disks, both 128 GB. Because the disk sizes are 128 GB, these disks are both configured as P10s, which provide maximum 500 IOPS per disk.

az vm create \
  --resource-group myResourceGroupDisk \
  --name myVM \
  --image Ubuntu2204 \
  --size Standard_DS2_v2 \
  --admin-username azureuser \
  --generate-ssh-keys \
  --data-disk-sizes-gb 128 128

Attach disk to existing VM

To create and attach a new disk to an existing virtual machine, use the az vm disk attach command. The following example creates a premium disk, 128 gigabytes in size, and attaches it to the VM created in the last step.

az vm disk attach \
    --resource-group myResourceGroupDisk \
    --vm-name myVM \
    --name myDataDisk \
    --size-gb 128 \
    --sku Premium_LRS \
    --new

Prepare data disks

Once a disk has been attached to the virtual machine, the operating system needs to be configured to use the disk. The following example shows how to manually configure a disk. This process can also be automated using cloud-init, which is covered in a later tutorial.

Create an SSH connection with the virtual machine. Replace the example IP address with the public IP of the virtual machine.

ssh azureuser@10.101.10.10

Partition the disk with parted.

sudo parted /dev/sdc --script mklabel gpt mkpart xfspart xfs 0% 100%

Write a file system to the partition by using the mkfs command. Use partprobe to make the OS aware of the change.

sudo mkfs.xfs /dev/sdc1
sudo partprobe /dev/sdc1

Mount the new disk so that it is accessible in the operating system.

sudo mkdir /datadrive && sudo mount /dev/sdc1 /datadrive

The disk can now be accessed through the /datadrive mountpoint, which can be verified by running the df -h command.

df -h | grep -i "sd"

The output shows the new drive mounted on /datadrive.

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        29G  2.0G   27G   7% /
/dev/sda15      105M  3.6M  101M   4% /boot/efi
/dev/sdb1        14G   41M   13G   1% /mnt
/dev/sdc1        50G   52M   47G   1% /datadrive

To ensure that the drive is remounted after a reboot, it must be added to the /etc/fstab file. To do so, get the UUID of the disk with the blkid utility.

sudo -i blkid

The output displays the UUID of the drive, /dev/sdc1 in this case.

/dev/sdc1: UUID="33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e" TYPE="xfs"

Note

Improperly editing the /etc/fstab file could result in an unbootable system. If unsure, refer to the distribution's documentation for information on how to properly edit this file. It is also recommended that a backup of the /etc/fstab file is created before editing.

Open the /etc/fstab file in a text editor as follows:

sudo nano /etc/fstab

Add a line similar to the following to the /etc/fstab file, replacing the UUID value with your own.

UUID=33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e   /datadrive  xfs    defaults,nofail   1  2

When you are done editing the file, use Ctrl+O to write the file and Ctrl+X to exit the editor.

Now that the disk has been configured, close the SSH session.

exit

Take a disk snapshot

When you take a disk snapshot, Azure creates a read only, point-in-time copy of the disk. Azure VM snapshots are useful to quickly save the state of a VM before you make configuration changes. In the event of an issue or error, VM can be restored using a snapshot. When a VM has more than one disk, a snapshot is taken of each disk independently of the others. To take application consistent backups, consider stopping the VM before you take disk snapshots. Alternatively, use the Azure Backup service, which enables you to perform automated backups while the VM is running.

Create snapshot

Before you create a snapshot, you need the ID or name of the disk. Use az vm show to show the disk ID. In this example, the disk ID is stored in a variable so that it can be used in a later step.

osdiskid=$(az vm show \
   -g myResourceGroupDisk \
   -n myVM \
   --query "storageProfile.osDisk.managedDisk.id" \
   -o tsv)

Now that you have the ID, use az snapshot create to create a snapshot of the disk.

az snapshot create \
    --resource-group myResourceGroupDisk \
    --source "$osdiskid" \
    --name osDisk-backup

Create disk from snapshot

This snapshot can then be converted into a disk using az disk create, which can be used to recreate the virtual machine.

az disk create \
   --resource-group myResourceGroupDisk \
   --name mySnapshotDisk \
   --source osDisk-backup

Restore virtual machine from snapshot

To demonstrate virtual machine recovery, delete the existing virtual machine using az vm delete.

az vm delete \
--resource-group myResourceGroupDisk \
--name myVM

Create a new virtual machine from the snapshot disk.

az vm create \
    --resource-group myResourceGroupDisk \
    --name myVM \
    --attach-os-disk mySnapshotDisk \
    --os-type linux

Reattach data disk

All data disks need to be reattached to the virtual machine.

Find the data disk name using the az disk list command. This example places the name of the disk in a variable named datadisk, which is used in the next step.

datadisk=$(az disk list \
   -g myResourceGroupDisk \
   --query "[?contains(name,'myVM')].[id]" \
   -o tsv)

Use the az vm disk attach command to attach the disk.

az vm disk attach \
   –g myResourceGroupDisk \
   --vm-name myVM \
   --name $datadisk

Next steps

In this tutorial, you learned about VM disks topics such as:

  • OS disks and temporary disks
  • Data disks
  • Standard and Premium disks
  • Disk performance
  • Attaching and preparing data disks
  • Disk snapshots

Advance to the next tutorial to learn about automating VM configuration.