xen xcp-ng – How to mount a samba share mount on a VM

  linux, xcp-ng

First the samba share must exist.

Then, go to the linux box, and:

# be root
sudo su

Install cifs utils which lets you mount samba shares:

sudo apt-get install cifs-utils

Create a credentials file with the login/password to the samba share, and store this credentials file in /etc/

nano /etc/nas.credentials

Enter your network share login and password in this nas.credentials file:

username=mysambauser
password=mysecretpassword
domain=

Looks like you can use the “domain=” blank, it seems to work fine.

Create the directory where you would like this network share mounted, ex: /mnt/nas

mkdir /mnt/nas
# go to root's directory
cd /root

# create a shell script called mountSambaShares.sh
nano mountSambaShares.sh

with contents:

#!/bin/bash
mount -v -t cifs //nas.florida/vms/xen-servernamehere /mnt/nas -o vers=2.1,credentials=/etc/nas.credentials,uid=$(id -u youruseridhere),gid=$(id -g youruseridhere)

Make sure to get the exact path of the shared drive correctly.

In Windows, you can go to a command prompt and type “net use” to see the exact path to any mapped network drive. (ex: D: drive is \\192.168.1.20\shared1 )

If you want to mount the drive as a specific use, (for example the user will be called “motion”), you can do something like this:

#!/bin/bash
mount -v -t cifs //192.168.1.20/shared1 /mnt/nas -o vers=2.1,credentials=/etc/nas.credentials,uid=$(id -u motion),gid=$(id -g motion)

and you’ll see that the directory permissions belong to your specific user:

Make the file executable:

chmod u+x /root/mountSambaShares.sh

Try it out:

/root/mountSambaShares.sh

Go check to see if you can view the mounted share point, and the files from the share:

cd /mnt/nas
ls -la

If all that is good, add the mountSambaShares.sh bash file to run automatically every time the server is rebooted.

# make sure you're still root
sudo su

# edit the cron tab
crontab -e

Add the line to run the shell every time the server boots:

@reboot /root/mountSambaShares.sh

Enjoy.