hero-testdrive-graphic.png

Blog

How to Attach and Mount VHD Files to Azure Virtual Machines

Posted by Gali Kovacs on Jul 9, 2017 8:58:29 AM

Azure Virtual Machines
Microsoft Azure has two types of virtual hard disks (VHDs): unmanaged and managed.

Unmanaged disks are maintained by the end user in their own storage accounts. These unmanaged disks are bound by storage account limits, which leads to needing several storage accounts when the deployment has to scale.

Managed disks are part of the Azure Managed Disks service. This disk type overcomes the storage account limit obstacle by having Microsoft manage the storage accounts for you. There are a number of other advantages that Azure managed disks have over unmanaged disks.

This article will discuss methods you can use to attach and mount managed and unmanaged VHD blob storage to Azure virtual machines (VMs).

All the steps in this article are compatible with Azure Resource Manager (ARM), but if you need to create storage accounts for Azure Service Manager (ASM), make sure to use the “Storage Accounts (classic) option” in the new portal.

Attaching Unmanaged and Managed Disks to Windows VMs: Step by Step

Here is the step-by-step procedure for adding disks to a Windows VM through the Azure Resource Manager (ARM) portal.

Step 1:

Open up the Azure Resource Manager portal and browse over to the “Virtual Machine” blade.

Step 2:

Go to “Settings” and click on “Disks.” On the blade that opens up, click on the “+ Add data disk” option.

 

Azure Resource Manager

 

If you are adding an unmanaged disk, a dialog box like the one shown below will open:

 

attach unmanaged disc

 

With managed disks, the dialog box that opens will display the options seen below:

Managed Discs

 

Step 3:

This next step varies depending on whether you are using an existing VHD or creating a new one.

Existing VHD: For unmanaged disks, select “Existing blob” under the “Source type” drop-down menu. In the “Source blob” field, browse for the blob container and then select the VHD that you want to attach.

 

Exhisting VHD

 

For managed disks, select the managed disk from the drop-down menu under “Name” as shown below:

 

managing disks

 

New VHD: For unmanaged disks, select “New (empty disk)” in the “Source type” field. Next, select the Blob container where you want to store the VHD .

 

azure managed disks

 

For managed disks, click on the “Create disk” option under the “Name” drop-down menu.

 

disks virtual machines

 

In the appropriate fields, specify the name, size and resource group of the disk to be created:

 

creating managed disks

 

Step 4:

Clicking on “Save” will attach the data disk to the VM. When you connect to the VM through RDP, you can see the attached disk under the “Disk Management” console.

If it is a new disk, you can initialize and format the attached VHD. Otherwise, if it is a VHD containing your data, right click and bring it online.

 

disc management

 

Attach Unmanaged and Managed Disks to a Linux VM

Here are the steps to add disks to a Linux VM. This example will use an Ubuntu distro.

Step 1:

Browse to the Ubuntu VM using the Azure portal.

Step 2:

Under “Settings,” click on “Disks.” On the blade that opens up, select the option “+ Add data disk.”

 

managing disks

 

For attaching managed disks, you’ll follow the same actions described in Step 2 in the above section on attaching disks to Windows VMs.

For unmanaged disks, you’ll see the same options as in the image below:

 

azure virtual machines

 

Step 3:

First, select “Existing blob” in the “Source type” field. Next, browse the “Storage container” menu and select the VHD to be attached.

For instructions on how to add new managed and unmanaged disks, please follow the same steps given in Step 3 of the Windows VM section above.

 

unmanaged disks

 

Step 4:

Clicking on “Save” will add the data VHD. Now, connect to the Linux VM through SSH.

 

Linux VM

 

Step 5:

If this is a disk with existing data, skip ahead to Step 8. Otherwise, start with this step. To list the disks use this command: 

“dmesg | grep SCSI”

 

sdc drives

 

In this case, it is the sdc drive that was newly attached. You can view more details on that drive by using this command:

sudo smartctl -d scsi -a -i /dev/sdc

 

managings disks

 

Step 6:

Now that we have identified the SCSI disk, let’s format and mount it. Use the fdisk command to partition your disk. In this example, one primary partition encompassing the whole drive has been created:

 

SCSI Disks

 

Step 7:

After the partition is created, you’ll need to write the file system (in this example ext4):

 

File system

 

Step 8:

After the file system has been written, assign a directory and mount the device to the directory.

 

device directory

 

Step 9:

Now that we have the drive set up, you can read and write files from it.

 

steps for virtual machines

 

Using Powershell and Azure CLI to Attach Disks

For PCs running on Linux or Mac (or even Windows using WSL), you can use Azure Command Line Interface (CLI) to create and manage Azure resources. Having said that, this blog post’s author prefers to connect to Azure CLI through Azure Cloud Shell directly on the Azure portal. Here are some of the commands that you would run to attach disks to an Azure VM. 

Managaed disks: The following command would add a new 20GB managed disk named “netappman2” to the VM “netappmanvm” in the resource group “netapptests”:

az vm disk attach -g netapptests --vm-name netappmanvm --disk netappman2 --new --size-gb 20 

Unmanaged disks: This command will add a new 20GB unmanaged disk named “netappunman3” of to the VM “netappwintest” and place it under the same storage account as the VM:

az vm unmanaged-disk attach -g netapptests -n netappunman3 --vm-name netappwintest --new --size-gb 20 

For more command lines, take a look at the full list of Azure CLI commands here.

Microsoft Powershell is another programmatic tool that can be used to attach VHDs to virtual machines. Start by logging into the Azure RM account: 

Login-AzureRmAccount

The following cmdlets would get the information of the VM “netappwintest” and attach an unmanaged disk named netappunman3 with 20GB capacity: 

$VM = Get-AzureRmVM -ResourceGroupName netapptests -Name netappwintest 

Add-AzureRmVMDataDisk -VM $VirtualMachine -Name "netappunman3" -VhdUri "https://dataunman2.blob.core.windows.net/netappunman1/netappunman3.vhd" -LUN 0 -Caching ReadOnly -DiskSizeinGB 20 -CreateOption Empty

Update-AzureRmVM -ResourceGroupName "netapptests" -VM $VirtualMachine

Final Note

Although managed disks have a number of features and functionalities that unmanaged disks just don’t offer, make sure you understand that there are a few gotchas involved with using managed disks.

For instance, the pricing for standard managed disks is based on provisioned size: that means you’re paying for disk space you may not wind up using. 

One last note: if you are using standard disks, don’t forget to turn on TRIM on the Windows and Linux VMs after you attach VHD disks. Trim discards unused blocks on the disk and helps save on costs if you create large files and then delete them. You can do this in Windows by running the fsutil command and in Linux by using the fstrim command.

 

 Azure Migration

 

Related Articles:

Topics: Azure

>