windowws
windowws

Reputation: 377

Azure file share mount script on cloud init

I have create a Azure file share and to connect to that i have a script given in the azure console as follows

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

i want to use this script in cloudinit of azure. How can i do it, any help on this would be appreciated.

Upvotes: 0

Views: 1059

Answers (1)

Charles Xu
Charles Xu

Reputation: 31424

The cloud-init is used in the creation time of the VM. So you can use it when you create the VM via the CLI command like this:

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

But here is a problem. The cloud-init script run as ‘root’ user. So you do not need to use the sudo mode. And you can directly use the custom user-data format like this:

$ 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

Take a look at the cloud-init User-Data Script.

Upvotes: 1

Related Questions