0

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.

windowws
  • 373
  • 1
  • 8
  • 20

1 Answers1

1

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.

Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • Thanks a million, it worked. How can i add other commands i want to run after this script in `cloud-init` – windowws Aug 24 '20 at 06:54
  • 1
    @windowws If you mean run other commands in the cloud-init, just add the command below, it's a shell script so you can add more commands. – Charles Xu Aug 24 '20 at 07:06