cloud init 上的 Azure 文件共享挂载脚本

Azure file share mount script on cloud init

我创建了一个 Azure file share 并连接到它,我在 azure 控制台中提供了一个脚本,如下所示

sudo mkdir /mnt/Totalvm
if [ ! -d "/etc/smbcredentials" ]; then
sudo mkdir /etc/smbcredentials
fi
if [ ! -f "/etc/smbcredentials/Totalcontainerstorage.cred" ]; then
    sudo bash -c 'echo "username=Totalcontainerstorage" >> /etc/smbcredentials/Totalcontainerstorage.cred'
    sudo bash -c 'echo "password=WPt39LGSSagFVeWbsNJ8HuhTaoPa1aiAZsOR3pBXnrOGjXFWVZj2BqooibIXvqbtjwbn4TLC4j+gJhOAk798pQ==" >> /etc/smbcredentials/Totalcontainerstorage.cred'
fi
sudo chmod 600 /etc/smbcredentials/Totalcontainerstorage.cred

sudo bash -c 'echo "//Totalcontainerstorage.file.core.windows.net/Totalvm /mnt/Totalvm cifs nofail,vers=3.0,credentials=/etc/smbcredentials/Totalcontainerstorage.cred,dir_mode=0777,file_mode=0777,serverino" >> /etc/fstab'
sudo mount -t cifs //Totalcontainerstorage.file.core.windows.net/Totalvm /mnt/Totalvm -o vers=3.0,credentials=/etc/smbcredentials/Totalcontainerstorage.cred,dir_mode=0777,file_mode=0777,serverino

我想在 azurecloudinit 中使用这个脚本。我该怎么做,如有任何帮助,我们将不胜感激。

cloud-init用于VM的创建时间。因此,您可以在通过 CLI 命令创建 VM 时使用它,如下所示:

az vm create \
    --resource-group myResourceGroupAutomate \
    --name myAutomatedVM \
    --image UbuntuLTS \
    --admin-username azureuser \
    --generate-ssh-keys \
    --custom-data cloud-init.txt

但是这里有个问题。 cloud-init 脚本 运行 作为“root”用户。所以你不需要使用 sudo 模式。您可以像这样直接使用自定义 user-data 格式:

$ cat mount_script.sh
#!/bin/bash
mkdir /mnt/Totalvm
if [ ! -d "/etc/smbcredentials" ]; then
mkdir /etc/smbcredentials
fi
if [ ! -f "/etc/smbcredentials/Totalcontainerstorage.cred" ]; then
    bash -c 'echo "username=Totalcontainerstorage" >> /etc/smbcredentials/Totalcontainerstorage.cred'
    bash -c 'echo "password=WPt39LGSSagFVeWbsNJ8HuhTaoPa1aiAZsOR3pBXnrOGjXFWVZj2BqooibIXvqbtjwbn4TLC4j+gJhOAk798pQ==" >> /etc/smbcredentials/Totalcontainerstorage.cred'
fi
chmod 600 /etc/smbcredentials/Totalcontainerstorage.cred

bash -c 'echo "//Totalcontainerstorage.file.core.windows.net/Totalvm /mnt/Totalvm cifs nofail,vers=3.0,credentials=/etc/smbcredentials/Totalcontainerstorage.cred,dir_mode=0777,file_mode=0777,serverino" >> /etc/fstab'
mount -t cifs //Totalcontainerstorage.file.core.windows.net/Totalvm /mnt/Totalvm -o vers=3.0,credentials=/etc/smbcredentials/Totalcontainerstorage.cred,dir_mode=0777,file_mode=0777,serverino

看看 cloud-init User-Data Script.