DEV Community

Cover image for How to Create and Attach a Disk to Your Azure Virtual Machine
JICDev
JICDev

Posted on

How to Create and Attach a Disk to Your Azure Virtual Machine

When working with Azure Virtual Machines (VMs), storage plays a critical role. By default, every VM comes with:

  • An OS disk (for the operating system).
  • A temporary disk (for temporary data).

But often, you’ll need extra storage — maybe for databases, logs, or application data. This is where data disks come in. In this post, we’ll walk through how to create and attach a new disk to your VM in the Azure Portal.

Step 1: Navigate to Your Virtual Machine
Believing that you already have an Azure account and have a VM created. If not, learn how to create one here

In the left-hand menu, select Virtual Machines. Choose the VM you want to attach the disk to.

Step 2: Add a New Disk

In the VM’s navigation menu, scroll down and click Disks.

Click + Create and attach a new disk. Iin the blue box. However if you already have a Data Disk previously created, you can easily attach it with the "attach existing disk" just beside.

After clicking "Create and attach a new disk" you will see the disk information pop out as in the red box in the image below:

** Step 3: Configure the New Disk**
This where you fill in the modalities of the disk. You’ll need to specify:

  • Disk name → Something descriptive (e.g., TestDk).
  • Storage type → As preferred and available. Depending on needs.
  • Size → Choose according to your needs (e.g., 8GB).
  • Encryption → Platform-managed key
  • Hosting Caching → Read/Write (SO that it is readable and editable}

Click Appy Below:

There you have it... You have created and attached your first data disk.

However, you would need to optimise/Initialise your disk into the VM. How do you do so? Follow the steps below:

🔹 Step 5: Initialize/Optimise the Disk Inside the VM

The disk is attached but not yet usable. You need to initialize/Optimise and format it from inside the VM. Depending on you VM registration method. This can be changed by clicking edit setting in the connect section.

If you used pass word. Then you can easily log into you machine. However, if it was through SSH Key, then you would have to download .pem file and load form there. In this case we use password. If you have forgotten your password, just like i did!!! You can reset it by clicking the reset password.

Open your Windows PowerShell
You would have to login into you vm as seen below:

Ref... The yellow boxes are my inputs. While the red arrows were the errors i encountered.

I.e, My Vm wasn't running, so i had to go back to azure to connect it. Therefore, your marching must be running before you can connect it on your PowerShell.

The other error was I forgot my password, which I showed you how to reset above. It everything woks right, you should see the welcome info meaning you are successfully in.

Now unto optimising the disk. Us the commands below to do so.

1. Check the disk

> sudo lsblk -f
Enter fullscreen mode Exit fullscreen mode

This shows all disks. Look for /dev/sdc1 (or similar).

  1. We would have to Format the data disk (TestDk) (only if it’s a new/empty disk)
sudo mkfs.ext4 -F /dev/sdc1

Enter fullscreen mode Exit fullscreen mode


Creates an ext4 filesystem. Skip this if the disk already has data. like in my case I already have it available.

  1. Now we have to Create a mount point. First you have to create the directory. Do so with command:
sudo mkdir -p /mnt/TestDk
Enter fullscreen mode Exit fullscreen mode

  1. Now Mount the disk with:
sudo mount /dev/sdc1 /mnt/TestDk
Enter fullscreen mode Exit fullscreen mode

To confirm you are doing everything right. Imput the command:

df -h | grep TestDk
Enter fullscreen mode Exit fullscreen mode

REF:--- Breakdown:

  • /dev/sdc1 → the attached disk partition.
  • 16G → total size of the disk.
  • 36K → space used (basically empty).
  • 15G → available free space.
  • 1% → usage percentage.
  • /mnt/TestDk → the folder where it’s mounted.

You should see /dev/sdc1 mounted to /mnt/TestDk.

NB: Usually after mounting a disk. and optimising it on linux, usuually, at the reboot of the VM, the Data Disk disconnect. To avoid that, we would apply a command that make sure and the start of the VM, the Data disk boots with it. You achieve that with step 5.

  1. Make it persistent (auto-mount after reboot)
sudo blkid /dev/sdc1
Enter fullscreen mode Exit fullscreen mode

If you, got the information as in the image above, then you are on the right part.

Then you can confirm using

df -h | grep TestDk
Enter fullscreen mode Exit fullscreen mode

If you do get the disk information above, then you might want to check how many disk mounted using :

mount | grep sdc1
Enter fullscreen mode Exit fullscreen mode

If you have more than one as the above, you would have to unmount 1. Use

sudo umount -l  /mnt
Enter fullscreen mode Exit fullscreen mode

to unmount it

Thereafter, reconfirm, then run

df -h | grep TestDk
Enter fullscreen mode Exit fullscreen mode

There you go!!! You have add a disk and optimised it for your Linux VM.

Will you like to know the challenges i went through try to do this for the first time? Click here and how I solved them.

Top comments (0)