通过 Bicep 使用 VM 运行命令在 azure linux 机器上挂载文件共享

问题描述 投票:0回答:1

我正在尝试通过 Bicep 使用 VM Run Command 在 azure linux 机器上挂载文件共享。 部署成功,但未创建挂载点。在二头肌配置下方。这里存储帐户和 VM 在不同的资源组中

  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  '''        


     }
    }

非常感谢任何帮助。提前致谢。

azure-storage azure-virtual-machine azure-bicep
1个回答
0
投票

我在我的环境中尝试并得到以下结果:

您可以使用以下脚本将文件共享挂载到 Azure Linux VM:

测试二头肌

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 '''        

   }
  }
}

您可以从门户网站获取脚本部分:

Storage accounts -> your storage account -> file share ->connect-> linux -> select script © and paste it like above 在你的二头肌文件中。

enter image description here

部署:

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

要检查文件共享是否已使用 Linux 虚拟机挂载,您可以使用以下命令:

命令:

df-h

输出:

下面的屏幕截图表明它已成功安装到您的 Linux VM。

enter image description here

如果您需要检查文件共享中的文件,您可以使用命令

cd /mnt/your file share name
ls

输出: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.