DEV Community

Cover image for Azure Applied Skills: Attaching a Data Disk to a Virtual Machine
John Ogbonna
John Ogbonna

Posted on

Azure Applied Skills: Attaching a Data Disk to a Virtual Machine

Introduction

Virtual Machines (VMs) in Azure provide flexible computing resources, but sometimes the default storage isn't enough for your needs. Whether you're storing application data, logs, or databases, adding a data disk allows you to expand storage capacity while keeping your OS disk separate. This improves performance, security, and manageability.

In this guide, we will focus on attaching a data disk to an Azure VM using the Azure Portal. By the end of this exercise, you will have a new data disk added to your VM and ready for use.

What is a Data Disk in Relation to a VM?

A data disk in Azure is an additional storage disk that you can attach to a VM. Unlike the OS disk, which contains the operating system and system files, a data disk is used for:

  • Storing application data such as databases, logs, or media files.
  • Separating critical data from the OS disk for better security and performance.
  • Expanding storage capacity without modifying the existing VM configuration.
    Data disks are persistent, meaning they retain data even if the VM is restarted or deallocated. They can also be detached and reattached to other VMs as needed.

  • Requirements to Perform This Exercise
    Before you begin, make sure you have the following:

  • An Active Azure Subscription – You must have access to the Azure Portal.

  • Proper Permissions – You need Contributor or Owner access to create and manage resources.

  • Available Storage Quota – Ensure your subscription allows the creation of VMs and additional disks.
    With these in place, let’s start by creating a Linux Virtual Machine in Azure and then attach a data disk to expand its storage.

Creating the VM

  • Log into the Azure portal and search for Virtual Machines
  • Click create and select Azure Virtual machine
    Click create

  • Select or create resource group

  • Select an appropriate Region

  • Select appropriate availability zone options as needed

  • Select an Ubuntu Image
    Select an Ubuntu Image

  • Select a VM size, we are using Standard_B1s for this example

  • Create username and ssh key
    Create username and ssh key

  • Allow HTTPS and SSH access then click review + create
    Allow HTTPS and SSH access

  • Click create. Download the ssh key to your machine by following the prompt

Attaching data disk

  • Once deployed, click go to resource
    click go to resource

  • Click settings, then disks
    Click settings, then disks

  • Enter a name and customize settings as required. Click apply when done
    name and customize settings

  • The disk is now attached

Formatting the disk:

  • In the Azure portal, navigate to your newly created VM. Copy and note the Public IP address.

  • Open terminal and navigate to where you downloaded the ssh key for the VM. Enter:

ssh -i /path/to/private-key.pem <username>@<VM_Public_IP>
Enter fullscreen mode Exit fullscreen mode

If this does not work, enter

chmod 600 </path/to/private-key.pem>
Enter fullscreen mode Exit fullscreen mode

Then try the former command again

  • Example: Example

Run the following command to identify the newly attached disk:

lsblk
Enter fullscreen mode Exit fullscreen mode
  • You should see a new disk (e.g., /dev/sdb) that is not yet mounted.
    You should see a new disk

  • Use fdisk to create a partition:

sudo fdisk /dev/sdb1
Enter fullscreen mode Exit fullscreen mode

Press n (new partition) → p (primary partition) → Enter (default values) → Enter (default values) → w (write changes).
format disk

  • Format the partition. Ex:
sudo mkfs.ext4 /dev/sdb1
Enter fullscreen mode Exit fullscreen mode
  • Create a Mount Directory:
sudo mkdir /mnt/datadisk
Enter fullscreen mode Exit fullscreen mode
  • Mount the Disk:
sudo mount /dev/sdb1 /mnt/datadisk
Enter fullscreen mode Exit fullscreen mode
  • Make the Mount Permanent (Optional): To ensure the disk mounts automatically on reboot, add it to /etc/fstab:
echo "/dev/sdb1 /mnt/datadisk ext4 defaults,nofail 0 2" | sudo tee -a /etc/fstab
Enter fullscreen mode Exit fullscreen mode
  • Your data disk is now attached, formatted, and ready to use!

Terminating the VM and Data disk

  • Go to your VM and click on delete.
    click delete

  • Select Apply force delete. Check all options to avoid ongoing costs. Accept the conditions and click delete
    Accept the conditions

Top comments (0)