Search code examples
azure-storageazure-virtual-machineazure-bicep

Mounting fileshare on Azure Linux machine using VM Run Command through Bicep


I am trying to mount fileshare on Azure Linux machine using VM Run Command through Bicep. The deployment gets success but mount point did not get create. Below is the bicep config. Here storage account and VM is in different resource group

  resource vm 'Microsoft.Compute/virtualMachines@2021-03-01' existing = {
    name: 'rabolinuxvm'
  }

  resource customextensionscript 'Microsoft.Compute/virtualMachines/runCommands@2022-11- 
   01' = {
   name: 'samount'
   parent: vm
   location: 'eastus'
   properties: {
     source: {
       script: ''' 
       sudo mkdir /etc/share3                
       credentialRoot="/etc/smbcredentials"
       sudo mkdir -p "/etc/smbcredentials"
       storageAccountKey=$(az storage account keys list \
         --resource-group $resourceGroupName \
         --account-name $storageAccountName \
         --query "[0].value" --output tsv | tr -d '"')

       smbCredentialFile="$credentialRoot/$storageAccountName.cred"

       if [ ! -f $smbCredentialFile ]; then
          echo "username=$storageAccountName" | sudo tee $smbCredentialFile > /dev/null
          echo "password=$storageAccountKey" | sudo tee -a $smbCredentialFile > 
            /dev/null
       else
         echo "The credential file $smbCredentialFile already exists, and was not 
         modified."
       fi

       sudo chmod 600 $smbCredentialFile
       fileShareName="share4" 
       mntPath="/etc/$fileShareName"
       sudo mkdir -p $mntPath
       httpEndpoint=$(az storage account show \
         --resource-group $resourceGroupName \
         --name $storageAccountName \
         --query "primaryEndpoints.file" --output tsv | tr -d '"')

       smbPath=$(echo $httpEndpoint | cut -c7-${#httpEndpoint})$fileShareName

      if [ -z "$(grep $smbPath\ $mntPath /etc/fstab)" ]; then
         echo "$smbPath $mntPath cifs 
            nofail,credentials=$smbCredentialFile,serverino,nosharesock,actimeo=30" | 
            sudo tee -a /etc/fstab > /dev/null
      else
        echo "/etc/fstab was not modified to avoid conflicting entries as this Azure 
        file share was already present. You may want to double check /etc/fstab to 
        ensure the configuration is as desired."
      fi
      sudo mount -a  '''        


     }
    }

What can I try to resolve this?


Solution

  • I tried in my environment and got the below results:

    You can use the below scripts to mount the file share to Azure Linux VM:

    Test.bicep

    resource vm 'Microsoft.Compute/virtualMachines@2021-03-01' existing = {
      name: 'venkatlinux'
    }
    
    resource customextensionscript 'Microsoft.Compute/virtualMachines/runCommands@2022-11-01' = {
     name: 'venkat1'
     parent: vm
     location: 'eastus'
     properties: {
       source: {
         script: ''' 
         sudo mkdir /mnt/venkat
    if [ ! -d "/etc/smbcredentials" ]; then
    sudo mkdir /etc/smbcredentials
    fi
    if [ ! -f "/etc/smbcredentials/saibabatest123.cred" ]; then
        sudo bash -c 'echo "username=saibabatest123" >> /etc/smbcredentials/saibabatest123.cred'
        sudo bash -c 'echo "password="" >> /etc/smbcredentials/saibabatest123.cred'
    fi
    sudo chmod 600 /etc/smbcredentials/saibabatest123.cred
    
    sudo bash -c 'echo "//saibabatest123.file.core.windows.net/venkat /mnt/venkat cifs nofail,credentials=/etc/smbcredentials/saibabatest123.cred,dir_mode=0777,file_mode=0777,serverino,nosharesock,actimeo=30" >> /etc/fstab'
    sudo mount -t cifs //saibabatest123.file.core.windows.net/venkat /mnt/venkat -o credentials=/etc/smbcredentials/saibabatest123.cred,dir_mode=0777,file_mode=0777,serverino,nosharesock,actimeo=30 '''        
    
       }
      }
    }
    

    You can get the script section from the portal:

    Storage accounts -> your storage account -> file share ->connect-> linux -> select script &copy and paste it like above in your bicep file.

    enter image description here

    Deployments:

    DeploymentName          : test
    ResourceGroupName       : your resource grp name
    ProvisioningState       : Succeeded
    Timestamp               : 3/28/2023 6:41:29 AM
    Mode                    : Incremental
    TemplateLink            : 
    Parameters              : 
    Outputs                 : 
    DeploymentDebugLogLevel : 
     
    

    enter image description here

    To check the file share is mounted with Linux virtual machine you can use the below command:

    Command:

    df-h
    

    Output:

    The below screenshots tell that it was successfully mounted to your Linux VM.

    enter image description here

    If you need to check files inside the file share you can use the command

    cd /mnt/your file share name
    ls
    

    Output: enter image description here