This article is contributed. See the original author and article here.

Background


Azure Batch supports mounting Azure file share with a Azure Batch pool. Our official document only has sample for C#. In this Blog, we will include the following content:



  • Mount Azure file share via Azure PowerShell for Windows and Linux.


  • How to access the mounting files for tasks. 




  • How Azure Batch agent implements mounting.



  • Troubleshoot the failure of mounting.


  • Access to the mounting drive manually.



  • Manually mount Azure file share via RDP/SSH.


 


Pre-requirement



  • Prepare an Azure Batch account.

  • Prepare a Azure Storage account with Azure Fileshare in the same region as the Batch account.

  • Prepare Azure PowerShell or use Azure CloudShell from Portal.


 


Steps:



  1. Log in to your subscription in Azure PowerShell:


 

Connect-AzAccount -Subscription "<subscriptionID>"

 


Bin_Yang_0-1617061748662.png



  1. Get the context for your Azure Batch account:


 

$context = Get-AzBatchAccount -AccountName <batch-account-name>

 



  1. For Windows, the following script will create an Azure Batch pool with these settings:



  • Azure File share mounting disk “S”

  • Image: “WindowsServer”, “MicrosoftWindowsServer”, “2016-Datacenter”, “latest”

  • VM size: STANDARD_D2_V2

  • Dedicated Compute Nodes: 1

  • Low priority Compute Nodes: 0


 

$fileShareConfig = New-Object -TypeName "Microsoft.Azure.Commands.Batch.Models.PSAzureFileShareConfiguration" -ArgumentList @("<Storage-Account-name>", https://<Storage-Account-name>.file.core.windows.net/batchfileshare1, "S", "Storage-Account-key")

$mountConfig = New-Object -TypeName "Microsoft.Azure.Commands.Batch.Models.PSMountConfiguration" -ArgumentList @($fileShareConfig)

$imageReference = New-Object -TypeName "Microsoft.Azure.Commands.Batch.Models.PSImageReference" -ArgumentList @("WindowsServer", "MicrosoftWindowsServer", "2016-Datacenter", "latest")

$configuration = New-Object -TypeName "Microsoft.Azure.Commands.Batch.Models.PSVirtualMachineConfiguration" -ArgumentList @($imageReference, "batch.node.windows amd64")

New-AzBatchPool -Id "<Pool-Name>" -VirtualMachineSize "STANDARD_D2_V2" -VirtualMachineConfiguration $configuration -TargetDedicatedComputeNodes 1 -MountConfiguration @($mountConfig) -BatchContext $Context

 



  1. For Linux, the following script will create an Azure Batch pool with these settings:



  • Azure File share mounting disk “S”

  • Image: “ubuntuserver”, “canonical”, “18.04-lts”, “latest”

  • VM size: STANDARD_D2_V2

  • Dedicated Compute Nodes: 1

  • Low priority Compute Nodes: 0


 

$fileShareConfig = New-Object -TypeName "Microsoft.Azure.Commands.Batch.Models.PSAzureFileShareConfiguration" -ArgumentList @("<Storage-Account-name>", https://<Storage-Account-name>.file.core.windows.net/batchfileshare1, "S", "<Storage-Account-key>", "-o vers=3.0,dir_mode=0777,file_mode=0777,sec=ntlmssp")

$mountConfig = New-Object -TypeName "Microsoft.Azure.Commands.Batch.Models.PSMountConfiguration" -ArgumentList @($fileShareConfig)

$imageReference = New-Object -TypeName "Microsoft.Azure.Commands.Batch.Models.PSImageReference" -ArgumentList @("ubuntuserver", "canonical", "18.04-lts", "latest")

$configuration = New-Object -TypeName "Microsoft.Azure.Commands.Batch.Models.PSVirtualMachineConfiguration" -ArgumentList @($imageReference, "batch.node.ubuntu 18.04")



New-AzBatchPool -Id "<Pool-Name>" -VirtualMachineSize "STANDARD_D2_V2" -VirtualMachineConfiguration $configuration -TargetDedicatedComputeNodes 1 -MountConfiguration @($mountConfig) -BatchContext $Context

 


How to access the mount files


For Windows:


  1. We use the Drive path directly to access the files. 

  2. For example, I have a file out.txt under path S:folder1 as shown below:


Bin_Yang_4-1617073161450.png



  1. The task can access the file by the following command:


 

cmd /c "more S:folder1out.txt & timeout /t 90 > NULL"

 



  1. Then you will see the result in the output file:


Bin_Yang_7-1617073901328.png


 


For Linux: 


  1. We can use the environment variable “AZ_BATCH_NODE_MOUNTS_DIR” or the path directly.

  2. For example, here is the command of the task to access file via environment variable:


 

/bin/bash -c 'more $AZ_BATCH_NODE_MOUNTS_DIR/S/folder1/out.txt; sleep 20s'

 



  1. Here is the output file:


Bin_Yang_6-1617073839180.png


 


Troubleshoot the failure of mounting


How Azure Batch agent implements mounting:

For Windows:


Azure Batch uses cmdkey to add credential for Azure Batch account, then issue the mount command via “net use”.


net use g: <storage-account-name>.file.core.windows.net<fileshare> /u:AZURE<storage-account-name> <storage-account-key>


 


For Linux:


Batch agent installs package cifs-utils ,then will issue the mount command.


 


How to check the mounting logs


  1. When mounting failed, you may observe the following error:


Bin_Yang_9-1617088704938.png



  1. We are not able to find more detailed information about the error via Azure Batch Portal or Azure Batch Explorer. So we need to RDP/SSH to the node and check related log files.

  2. For Windows, we can connect to the node via Remote Desktop as shown below(the screenshot is from Batch Explorer):


Bin_Yang_10-1617088704962.png



  1. The log file “fshare-S.log” is under path D:batchtasksfsmounts.

  2. In this sample, we can find the following messages:


CMDKEY: Credential added successfully.


System error 86 has occurred.


The specified network password is not correct.


You can refer to this document to troubleshoot Azure Files problems in Windows:


https://docs.microsoft.com/en-us/azure/storage/files/storage-troubleshoot-windows-file-connection-problems


 



  1. For Linux, we can connect to the node via SSH as shown below:


Bin_Yang_11-1617088704973.png



  1. You can find the log file “fshare-S.log” under /mnt/batch/tasks/fsmounts.

  2. In this sample, the account key was wrong and the message is “permission denied”.


Bin_Yang_12-1617088704977.png


You can refer to the following document to troubleshoot Azure Files problems in Linux:


https://docs.microsoft.com/en-us/azure/storage/files/storage-troubleshoot-linux-file-connection-problems


 


If you are not able to RDP/SSH, you can check the batch logs directly.



  1. Navigate to the node and click “upload batch logs” as shown below:


Bin_Yang_13-1617088960311.png


 



  1. You will be able to download the logs from the selected Azure Storage account:


Bin_Yang_14-1617089059387.png



  1. You can check the log named “agent-debug” and will see some error messages.

  2. You will find the same error messages as shown below.


..20210322T113107.448Z.00000000-0000-0000-0000-000000000000.ERROR.agent.mount.filesystems.basefilesystem.basefilesystem.py.run_cmd_persist_output_async.59.2912.MainThread.3580.Mount command failed with exit code: 2, output:


CMDKEY: Credential added successfully.


System error 86 has occurred.


 


The specified network password is not correct.



  1. It’s the same to get the logs for Linux.


 


Manually mount Azure File share


If you are not able identify the cause of failure, you can RDP/SSH to the node and manually mount the Azure File share to narrow down this issue.


 


Here are the detailed steps:



  1. You can create a pool without mounting configuration by the following command(let’s take Windows for example).


 

$imageReference = New-Object -TypeName "Microsoft.Azure.Commands.Batch.Models.PSImageReference" -ArgumentList @("WindowsServer", "MicrosoftWindowsServer", "2016-Datacenter", "latest")

$configuration = New-Object -TypeName "Microsoft.Azure.Commands.Batch.Models.PSVirtualMachineConfiguration" -ArgumentList @($imageReference, "batch.node.windows amd64")

New-AzBatchPool -Id "<Pool-Name>" -VirtualMachineSize "STANDARD_D2_V2" -VirtualMachineConfiguration $configuration -TargetDedicatedComputeNodes 1  -BatchContext $Context

 



  1. After the node reaches idle status, you can connect to the node via RDP.

  2. You can go to the Azure File Share Portal and get the Azure PowerShell command for mount as shown below:


Bin_Yang_15-1617089201199.png



  1. Then you can enter the command inside the node and mount the Azure File share.


Bin_Yang_16-1617089201217.png



  1. If there is any connection issue, you may observe the error message:


Bin_Yang_17-1617089201233.png



  1. You can troubleshoot Networking related issue in this way.


 


Access to the mounting drive manually.


When the mounting is successful, you can manually access the drive S directly in Linux in the following path:


/mnt/batch/tasks/fsmounts/S


 


However, you will get access denied when accessing the drive S in Windows:


Bin_Yang_18-1617090674910.png



  1. This is because Azure Batch agent only grants access for Azure Batch tasks in Windows.

  2. When RDP to the node, the user account doesn’t have access to the mounting drive.

  3. You need to use cmdkey to add the credential for yourself in cmd:


 

cmdkey /add:"<storage-account-name>.file.core.windows.net" /user:"Azure<storage-account-name>" /pass:"<storage-account-key>"

 



  1. After the credential is added, you will be able to access the S drive directly.

Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.