Attach a Managed Disk to a Linux Virtual Machine

This article explains how to attach an existing managed disk to a Linux virtual machine (VM) running in Azure. You can attach both data disks and operating system disks.

Prerequisites

Steps to Attach a Disk

Using Azure Portal

  1. Navigate to your Virtual Machine resource in the Azure portal.
  2. In the VM menu, select Disks under Settings.
  3. Click Attach existing disks.
  4. In the Disk name dropdown, select the managed disk you want to attach.
  5. If the disk is not encrypted, you can choose the Host caching option. For data disks, Read/write is generally recommended for performance. For OS disks, Read-only is often preferred.
  6. Click Save. Azure will update the VM configuration to attach the disk.

Using Azure CLI

To attach a data disk:


az vm disk attach --resource-group <YourResourceGroupName> --vm-name <YourVMName> --name <YourDiskName> --lun <LogicalUnitNumber>
        

Replace placeholders with your actual resource group name, VM name, disk name, and the desired logical unit number (LUN). A LUN is an identifier for the disk on the VM.

To attach an OS disk, use the --os-type parameter:


az vm disk attach --resource-group <YourResourceGroupName> --vm-name <YourVMName> --name <YourDiskName> --lun <LogicalUnitNumber> --os-type linux
        

Using Azure PowerShell

To attach a data disk:


$vm = Get-AzVM -ResourceGroupName "<YourResourceGroupName>" -Name "<YourVMName>"
$disk = Get-AzDisk -ResourceGroupName "<YourResourceGroupName>" -DiskName "<YourDiskName>"
Add-AzVMDataDisk -VM $vm -Name "<YourDiskName>" -CreateOption Attach -ManagedDiskId $disk.Id -Lun <LogicalUnitNumber>
Update-AzVM -ResourceGroupName "<YourResourceGroupName>" -VM $vm
        

To attach an OS disk, you would typically detach and reattach the OS disk using the VM's OS disk ID.

After Attaching the Disk

Once the disk is attached at the Azure resource level, you need to make it accessible within the Linux operating system. This involves recognizing the disk and mounting it.

Steps within the Linux VM

  1. Connect to your VM using SSH.
  2. Identify the new disk. You can use commands like lsblk or sudo fdisk -l. The new disk will likely appear as /dev/sdc, /dev/sdd, and so on.
  3. Check if the disk has a partition table and file system.
    • If the disk is new and empty, you'll need to create a partition and format it.
    • If the disk already has data, you can skip formatting but still need to mount it.
  4. Create a partition (if needed). Use fdisk or parted. For example, to create a primary partition on /dev/sdc:
    
    sudo fdisk /dev/sdc
    # Then follow the prompts to create a new partition (e.g., n for new, p for primary, 1 for partition number, accept defaults for start/end).
    # Write changes using w.
                    
  5. Format the disk (if needed). Assuming the new partition is /dev/sdc1:
    
    sudo mkfs.ext4 /dev/sdc1  # Or mkfs.xfs
                    
  6. Create a mount point. This is the directory where the disk will be accessible.
    
    sudo mkdir /datadrive
                    
  7. Mount the disk.
    
    sudo mount /dev/sdc1 /datadrive
                    
  8. Configure automatic mounting on boot. Edit the /etc/fstab file. First, get the UUID of your new partition:
    
    sudo blkid /dev/sdc1
                    
    Then, add a line to /etc/fstab (e.g., using sudo nano /etc/fstab):
    
    UUID=<your_partition_uuid>  /datadrive  ext4  defaults,nofail  0  2
                    
    The nofail option is important to ensure the VM boots even if the disk is not available.
  9. Verify the mount.
    
    df -h
                    
    You should see your mounted disk listed.

Tip: If you are attaching a disk that was previously used as an OS disk, it may already have partitions and a file system. In such cases, you can often skip the partitioning and formatting steps and proceed directly to mounting.

Note: The device name (e.g., /dev/sdc) might change between reboots. Using the UUID in /etc/fstab ensures consistent mounting.

Important: Always use the nofail option in your /etc/fstab entry for data disks. This prevents your VM from failing to boot if the disk is detached or unavailable.

Congratulations! You have successfully attached and mounted a managed disk to your Linux VM.

For more advanced scenarios, such as attaching premium SSDs or Ultra Disks, or for specific disk configurations, please refer to the official Azure documentation.