DEV Community

1suleyman
1suleyman

Posted on

Exercise 03: Access Storage for an Azure Linux Virtual Machine

Introduction

In this exercise, I learned how to integrate an Azure Linux virtual machine (VM) with Azure storage. This involved creating and attaching a data disk, accessing an Azure file share, and using AzCopy to transfer files from Azure Blob Storage to the VM. The goal was to explore how Azure storage solutions can be utilized for VM data management.

Scenario

The task was to set up storage for the Linux VM, specifically:

  1. Add a data disk to the VM.
  2. Access an Azure file share from the VM.
  3. Copy files from Azure Blob Storage to the VM's data disk using AzCopy.

Skills Practiced

  • Creating and configuring a Linux virtual machine using the Azure CLI.
  • Attaching and mounting a data disk to the VM.
  • Accessing an Azure file share from a Linux VM.
  • Using AzCopy to transfer files from Azure Blob Storage to a VM.

Step-by-Step Guide

Step 1: Create a Virtual Machine and Add a Data Disk

1️⃣ Sign in to the Azure Portal

I logged into the Azure portal and navigated to Cloud Shell in the top right corner.

2️⃣ Create a Virtual Machine using the Azure CLI

I created a new Linux virtual machine using the following CLI command:

   az vm create --name vm3 --resource-group rg1 --image Ubuntu2204 --admin-username adminuser --generate-ssh-keys --location eastus
Enter fullscreen mode Exit fullscreen mode

3️⃣ Attach a Data Disk to the VM

After creating the VM, I attached a 4 GB data disk using the following command:

   az vm disk attach --resource-group rg1 --vm-name vm3 --name Disk1 --new --size-gb 4
Enter fullscreen mode Exit fullscreen mode

4️⃣ Verify the Data Disk

I verified the disk was successfully created by using the command:

   az disk list --output table
Enter fullscreen mode Exit fullscreen mode

Step 2: Connect to the Virtual Machine and Configure the Data Disk

1️⃣ Connect to the VM via SSH

I connected to the VM using SSH:

   ssh -i ~/.ssh/id_rsa.pem adminuser@<public_ip_address>
Enter fullscreen mode Exit fullscreen mode

2️⃣ Partition the Data Disk

Once connected to the VM, I used the following commands to format and partition the new data disk:

   lsblk -o NAME,SIZE,MOUNTPOINT
   sudo parted /dev/sdc --script mklabel gpt mkpart xfspart xfs 0% 100%
   sudo partprobe /dev/sdc
   sudo mkfs.xfs /dev/sdc1
Enter fullscreen mode Exit fullscreen mode

3️⃣ Create a Mount Point

I created a directory for the mount point:

   sudo mkdir /datadrive
   sudo mount /dev/sdc1 /datadrive
Enter fullscreen mode Exit fullscreen mode

4️⃣ Verify the Data Disk Mount

I verified the disk was mounted and there were no files present:

   df
   ls /datadrive
Enter fullscreen mode Exit fullscreen mode

Step 3: Access an Azure File Share from the Virtual Machine

1️⃣ Create a Storage Account and File Share

I created a storage account and a file share named share1:

  • Storage account name: az104bobstg1
  • Region: East US
  • File share name: share1

2️⃣ Grant the VM Access to the File Share

I enabled the system-assigned managed identity for the VM, which allowed it to authenticate to the Azure storage account.

3️⃣ Run the Connect Script

I generated a connection script in the Cloud Shell and ran it to mount the Azure file share on the VM:

   sudo mkdir -p /mnt/share1
   sudo mount -t cifs //<storage_account_name>.file.core.windows.net/share1 /mnt/share1 -o vers=3.0,username=<storage_account_name>,password=<account_key>,dir_mode=0777,file_mode=0777
Enter fullscreen mode Exit fullscreen mode

4️⃣ Verify the File Share

After mounting the file share, I checked the files inside:

   ls /mnt/share1
Enter fullscreen mode Exit fullscreen mode

Step 4: Copy a File from Azure Blob Storage to the Virtual Machine Data Disk

1️⃣ Create a Blob Storage Container and Upload a File

I created a Blob Storage container named data and uploaded a file, blobimage.png, to it.

2️⃣ Assign the VM the Storage Blob Data Contributor Role

I granted the VM the Storage Blob Data Contributor role to the storage account, allowing read and write access to the blob container.

3️⃣ Install AzCopy on the VM

I installed AzCopy on the VM to enable easy transfer of files from Blob Storage:

   wget https://aka.ms/downloadazcopy-v10-linux
   sudo tar xzf downloadazcopy-v10-linux
   sudo mkdir /opt/azcopy
   sudo cp ./azcopy_linux_amd64_*/azcopy /opt/azcopy/
Enter fullscreen mode Exit fullscreen mode

4️⃣ Log In Using Managed Identity

I logged into AzCopy using the managed identity of the VM:

   sudo /opt/azcopy/azcopy login --identity
Enter fullscreen mode Exit fullscreen mode

5️⃣ Copy the File from Blob Storage to the VM's Data Disk

I copied the file from the blob storage container to the VM's data disk:

   sudo /opt/azcopy/azcopy copy "<url_of_blob>" /datadrive
Enter fullscreen mode Exit fullscreen mode

6️⃣ Verify the File Transfer

I confirmed that the file was successfully copied to the data disk:

   ls /datadrive
Enter fullscreen mode Exit fullscreen mode

What I Overcame in the Process (Real-World Troubleshooting)

🔐 SSH Access Issue

When I tried to connect to the VM after a short break, I received the error: "Permission denied (publickey)". This was caused by a mismatch between the private key on the local machine and the key expected by the VM.

Solution: I regenerated a new SSH key pair and updated the VM with the correct public key using:

az vm user update --resource-group rg1 --name vm3 --username adminuser --ssh-key-value (Get-Content ~/.ssh/id_rsa.pub)
Enter fullscreen mode Exit fullscreen mode

📂 Mount Point Directory Missing

I encountered an error when trying to mount the Azure file share due to the missing mount point directory.

Solution: I manually created the directory using:

sudo mkdir -p /mnt/share1
Enter fullscreen mode Exit fullscreen mode

🧠 Takeaway

Troubleshooting these issues taught me:

  • How to manage SSH keys for secure access.
  • The importance of checking and creating directories before mounting drives.
  • How to use AzCopy to efficiently transfer data between Azure Blob Storage and a Linux VM.

Key Learnings

1️⃣ Azure Storage Solutions

  • Azure provides various storage options, including Blob Storage and File Shares, which are essential for managing data within virtual machines.
  • AzCopy is a great utility for transferring files efficiently between Azure and virtual machines.

2️⃣ Attaching and Mounting Data Disks

  • Adding and mounting data disks in Azure Linux VMs is a simple but crucial step for expanding storage.
  • Managing files on Azure requires configuring storage and ensuring that virtual machines can access and utilize the storage resources.

3️⃣ Managed Identity for Azure Storage Access

  • Enabling managed identity for the VM helps securely authenticate the VM to Azure storage without needing to manage credentials manually.

Conclusion

By completing this exercise, I gained valuable experience in Azure storage management, including working with Azure Blob Storage and Azure File Shares. I also learned how to attach and configure data disks in a Linux VM, use AzCopy for file transfers, and leverage managed identities for secure storage access. These skills are essential for managing data in the cloud and integrating storage solutions with virtual machines.

🚀 Stay tuned for more as I continue my journey with Azure!

Top comments (0)