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.
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
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.
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.
lsblk or sudo fdisk -l. The new disk will likely appear as /dev/sdc, /dev/sdd, and so on.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.
/dev/sdc1:
sudo mkfs.ext4 /dev/sdc1 # Or mkfs.xfs
sudo mkdir /datadrive
sudo mount /dev/sdc1 /datadrive
/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.
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.