semimemi
Posts: 10
Joined: Sun Mar 16, 2014 8:14 pm

mount a cifs share using fstab

Fri Dec 07, 2018 12:30 pm

Hello!

I can mount a NAS folder using the command:

Code: Select all

sudo mount -t cifs -o user=USERNAME,password=PASSWORD,rw,file_mode=0777,dir_mode=0777 //192.168.0.10/myFolder /home/pi/myFolder
As I want to keep this folder connected I want to do the same thing using fstab. I edit the file using

Code: Select all

sudo nano fstab
But no matter what I try - I tried many different combinations like

Code: Select all

//192.168.0.10/myFolder /home/pi/myFolder cifs username=USERNAME,password=PASSWORD
when I run

Code: Select all

sudo mount -a -v
I always get

Code: Select all

/proc                    : already mounted
/boot                    : already mounted
/                        : ignored
So it seems like anything I enter in fstab is completely ignored. What am I doing wrong?
Any help is appreciated!

n67
Posts: 939
Joined: Mon Oct 30, 2017 4:55 pm

Re: mount a cifs share using fstab

Fri Dec 07, 2018 1:31 pm

Please don't use fstab for this. It will only lead to pain & suffering.

Instead, arrange, via one of the many "How do I run something automatically on startup?" techniques, to have your mount command run automatically on startup.

It is much safer to leave fstab alone.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
RaTTuS
Posts: 10242
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: mount a cifs share using fstab

Fri Dec 07, 2018 1:49 pm

use systemd auto mount system - wait to after network is up
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
thagrol
Posts: 1464
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: mount a cifs share using fstab

Fri Dec 07, 2018 1:54 pm

Using a non fstab approach won't necessarily help. It certainly won't help when runing "sudo mount -a"

Try this in fstab:

Code: Select all

 //192.168.0.10/myFolder /home/pi/myFolder cifs _netdev,auto,user=USERNAME,password=PASSWORD,rw,file_mode=0777,dir_mode=0777 0 0
You might want to use "uid=pi,gid=pi" in place of, or in addtions to, "file_mode=0777,dir_mode=0777"
Note to self: don't feed the trolls

If I've asked you a question, please answer it. I'm unlikely to be able to help without that information.

semimemi
Posts: 10
Joined: Sun Mar 16, 2014 8:14 pm

Re: mount a cifs share using fstab

Sat Dec 08, 2018 9:59 am

n67 wrote:
Fri Dec 07, 2018 1:31 pm
Please don't use fstab for this. It will only lead to pain & suffering.

Instead, arrange, via one of the many "How do I run something automatically on startup?" techniques, to have your mount command run automatically on startup.

It is much safer to leave fstab alone.
All right. I am trying this using cron. I start this python script:

Code: Select all

#!/usr/bin/python3
import ds,os,time,datetime
ds.logfile ("startup.py started")
i=0
response=1
while (response != 0):
	i+=1
	response = os.system("ping -c 1 " + "storage713")	
	time.sleep (0.1)
	print ("iteration " + str(i))
ds.logfile("connected after " + str(i) + " tries")
os.system ("sudo mount -t cifs -o user=XXXX,password=XXXX,rw,file_mode=0777,dir_mode=0777 //192.168.0.10/XXXXX")
ds.logfile("NAS connected")
My crontab entry is

Code: Select all

@reboot sudo python /home/pi/Desktop/python/startup.py
The script works when I start it from terminal. I know it is executed also by cron because I get my log entries after booting. Still the drives do not connect when it is started by cron. How is this possible?

DirkS
Posts: 9804
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: mount a cifs share using fstab

Sat Dec 08, 2018 10:13 am

semimemi wrote:
Sat Dec 08, 2018 9:59 am
n67 wrote:
Fri Dec 07, 2018 1:31 pm
Please don't use fstab for this. It will only lead to pain & suffering.

Instead, arrange, via one of the many "How do I run something automatically on startup?" techniques, to have your mount command run automatically on startup.

It is much safer to leave fstab alone.
All right. I am trying this using cron. I start this python script:
Please disregard @n67's comments.
There is no particular reason *not* to use fstab, although there are better methods (particularly systemd mounts)

semimemi
Posts: 10
Joined: Sun Mar 16, 2014 8:14 pm

Re: mount a cifs share using fstab

Sat Dec 08, 2018 10:20 am

DirkS wrote:
Sat Dec 08, 2018 10:13 am
There is no particular reason *not* to use fstab, although there are better methods (particularly systemd mounts)
The thing is just: I dont find good information for fstab so I'm trying a different approach. With the new apporach I feel so close to the solution so I hoped somenone might give me a hint..

DirkS
Posts: 9804
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: mount a cifs share using fstab

Sat Dec 08, 2018 10:36 am

semimemi wrote:
Sat Dec 08, 2018 10:20 am
DirkS wrote:
Sat Dec 08, 2018 10:13 am
There is no particular reason *not* to use fstab, although there are better methods (particularly systemd mounts)
The thing is just: I dont find good information for fstab so I'm trying a different approach. With the new apporach I feel so close to the solution so I hoped somenone might give me a hint..
See e.g. @thagrol's post above. He is one of the people I know is knowledgeable and gives good advice.

semimemi
Posts: 10
Joined: Sun Mar 16, 2014 8:14 pm

Re: mount a cifs share using fstab

Sat Dec 08, 2018 11:10 am

thagrol wrote:
Fri Dec 07, 2018 1:54 pm

Try this in fstab:

Code: Select all

 //192.168.0.10/myFolder /home/pi/myFolder cifs _netdev,auto,user=USERNAME,password=PASSWORD,rw,file_mode=0777,dir_mode=0777 0 0
This does not have any effect. Is there a way to debug this stuff? Because at the moment I only see that there is nothing connected after reboot.

DirkS
Posts: 9804
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: mount a cifs share using fstab

Sat Dec 08, 2018 11:17 am

semimemi wrote:
Sat Dec 08, 2018 11:10 am
This does not have any effect. Is there a way to debug this stuff? Because at the moment I only see that there is nothing connected after reboot.
Not sure if this works, but try

Code: Select all

sudo systemctl status home-pi-myFolder.mount
(maybe use .automount instead of .mount)

User avatar
rpdom
Posts: 14104
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: mount a cifs share using fstab

Sat Dec 08, 2018 11:19 am

Ok, I can see some potential issues there.
semimemi wrote:
Sat Dec 08, 2018 9:59 am
All right. I am trying this using cron. I start this python script:

Code: Select all

#!/usr/bin/python3
My crontab entry is

Code: Select all

@reboot sudo python /home/pi/Desktop/python/startup.py
Your code is set to use python3, but in the crontab you have just python, which I believe still runs python2.

The crontab has "sudo python", which will run the script as the root user, but you have another sudo command in the script. That may cause issues, I'm not sure. However, it is pointless.
The script works when I start it from terminal. I know it is executed also by cron because I get my log entries after booting. Still the drives do not connect when it is started by cron. How is this possible?
How do you run it from the terminal? What command do you use?

If the script is set up as executable ("ls -l /home/pi/Desktop/python/startup.py" shows -rwxr-xr-x or something with the "x" options set), then I would use a crontab entry like

Code: Select all

@reboot /home/pi/Desktop/python/startup.py

semimemi
Posts: 10
Joined: Sun Mar 16, 2014 8:14 pm

Re: mount a cifs share using fstab

Sat Dec 08, 2018 11:29 am

rpdom wrote:
Sat Dec 08, 2018 11:19 am
How do you run it from the terminal? What command do you use?
I can run it with just

Code: Select all

/home/pi/Desktop/python/startup.py
and I can add sudo or python. Either way works in the terminal. Neither of them works with crontab.
rpdom wrote:
Sat Dec 08, 2018 11:19 am
If the script is set up as executable ("ls -l /home/pi/Desktop/python/startup.py" shows -rwxr-xr-x or something with the "x" options set), then I would use a crontab entry like

Code: Select all

@reboot /home/pi/Desktop/python/startup.py
I tried that. Same effect. I get log entries but no NAS connection:(

User avatar
thagrol
Posts: 1464
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: mount a cifs share using fstab

Sat Dec 08, 2018 1:22 pm

After making the change to fstab I suggested above did you try "sudo mount -av" or "sudo mount /home/pi/myFolder"?

Is there anything in the share? A mount point will look the same whether or not something is mounted there if the share is empty.

Have you looked at the output from"mount" after a reboot? If the mount has succeeded it will be listed there.

Have you looked in syslog or the output of "dmesg" for errors?

Have you set your Pi/raspbian to "Wait for Network at Boot"? See the "Boot Options" section of "sudo raspi-config". It's possible that the mount is being attempted before the network is up.

Are you running your script from root's crontab? Or you user's?

Are you capturing output and errors from your python script? Jobs started via cron have all their output discarded so you have to explictly send it somewhere. Try adding "> /tmp/my_mount.log 2>&1" to the end of your crontab entry (before any trailing & if you have one). You can then examine /tmp/my_mount.log for errors. Your crontab entry will looke something like this:

Code: Select all

@reboot /path/to/my_script.py > /tmp/my_mount.log 2>&1

Given that your script works from the terminal, my money is on the network not being ready when it's run from crontab.
Note to self: don't feed the trolls

If I've asked you a question, please answer it. I'm unlikely to be able to help without that information.

User avatar
thagrol
Posts: 1464
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: mount a cifs share using fstab

Sat Dec 08, 2018 1:29 pm

DirkS wrote:
Sat Dec 08, 2018 10:36 am
See e.g. @thagrol's post above. He is one of the people I know is knowledgeable and gives good advice.
Thanks. I try to.
Note to self: don't feed the trolls

If I've asked you a question, please answer it. I'm unlikely to be able to help without that information.

semimemi
Posts: 10
Joined: Sun Mar 16, 2014 8:14 pm

Re: mount a cifs share using fstab

Sat Dec 08, 2018 1:33 pm

thagrol wrote:
Sat Dec 08, 2018 1:22 pm
Given that your script works from the terminal, my money is on the network not being ready when it's run from crontab.
You won. I added a 30seconds delay to my python script and it worked.

Now I will find a workaround checking in the script if the connection was successful and repeating it if not. This won't be too difficult. Thank you so much for your kind help!

User avatar
thagrol
Posts: 1464
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: mount a cifs share using fstab

Sun Dec 09, 2018 5:10 am

Glad you got it working but there are some issues with your approach that you should be aware of:
  1. By using a non-standard approach (i.e. by not using fstab) you're making it hard for a third party to understand what's happening should you need someone else to administrate your Pi. Or when you come back to it in a year and have forgotten what you did.
  2. You can't use "mount -a" to mount the share.
  3. You also can't use "mount /home/pi/myFolder" or "mount //192.168.0.10/myFolder"
  4. Should you need to manually mount the share, you either have to locate and run your script or type the full mount command. Mount cannot look up the missing options in fstab.
  5. As for your script, having looked at it in more detail:
    • It shouldn't work as you've not specified a mount point in your mount command.
    • You don't need to use python, you could do it in a bash script with less overhead.
    • When started from cron, output from your print statements will be discarded.
    • If the mount fails your log will show "NAS connected"
    • You've made no provision for the case where the network never comes up or the machine that goes ping doesn't respond. In both of those cases, it will sit pinging away 10 times a second forever.
    • Adding a 30 second start delay should have made no difference. Well, except for reducing the reported number of tries.
Sorry if the above seems harsh and/or picky, I used to test software for a living and old habits die hard....

If you set "Wait for Network on Boot" in raspi-config to "Yes" and add a suitable fstab entry as I suggested above including "_netdev" it should work, it does on several of my linux boxes (yeah, yeah, I know). "_netdev" tells systemd to schedule the mount after network is up.

If you really don't want to use fstab, do as RaTTuS suggests and use a systemd mount unit.

Other options are to use fstab but backstop it with a simple entry like this is root's crontab:

Code: Select all

@reboot	sleep 30 && mount /home/pi/myFolder
Or use the "noauto" option in fstab with the above crontab entry, your script modified to the shorter mount command, or a systemd service.
Note to self: don't feed the trolls

If I've asked you a question, please answer it. I'm unlikely to be able to help without that information.

semimemi
Posts: 10
Joined: Sun Mar 16, 2014 8:14 pm

Re: mount a cifs share using fstab

Sun Dec 09, 2018 4:01 pm

thagrol wrote:
Sun Dec 09, 2018 5:10 am
Glad you got it working but there are some issues with your approach that you should be aware of:
[...]
1..4:
I am aware of these disadvantages. For me as a non expert it is nearly impossible to make this work. I spent several hours in trying to find my error but without any debugging possibility I am just bruteforcing: Combining different parameters and hoping one combination will work. I am sure if you were sitting in front of my screen you would find the error within a minute but I cannot. And I am not willing to spend another weekend in finding the solution .

5:
You are right: My script has several weaknesses. But unlike the fstab thing here I know what I am doing and I can adjust everything. Of course I will modify it in a way that makes more sense. I am already saving the log to a temp file. I write "NAS connected" only if it was successful. And I will do some more modifications like stopping after a certain number of tries etc.
And you are right, the way I posted my code it wouldn't work. Seems like I deleted an argument by accident.

The code I am using at the moment is this one:

Code: Select all

#!/usr/bin/python3

import os,time,datetime
i=0
response=1
while response != 0:
	i+=1
	response = os.system("ping -c 1 " + "192.168.0.10")	
	time.sleep (1)
	print ("iteration " + str(i))

success = 1
while success != 0:
	success = os.system ("sudo mount -t cifs -o user=XXXX,password=XXXX,rw,file_mode=0777,dir_mode=0777 //192.168.0.10/XXXX /home/pi/Desktop/XXXXX")
	if success == True:
		break;
	time.sleep(1)

print("NAS connected")

User avatar
thagrol
Posts: 1464
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: mount a cifs share using fstab

Sun Dec 09, 2018 6:06 pm

If you're happy working with those limitiations, carry on. I'd suggest a simpler python script though:

Code: Select all

#!/usr/bin/env python3

import os, sys, time

start_delay = 0
max_retries = 5
retry_delay = 5
mount_command = 'sudo mount -t cifs -o user=XXXX,password=XXXX,rw,file_mode=0777,dir_mode=0777 //192.168.0.10/XXXX /home/pi/Desktop/XXXXX'

time.sleep(start_delay)
attempts = 0
while attempts <= max_retries and os.system(mount_command) != 0:
    attempts += 1
    time.sleep(retry_delay)
if attempts > max_retries:
    sys.exit('NAS mount failed after %s attempts.' % max_retries)
else:
    print('NAS mount succeded.')
The above is untested as my setup doesn't match yours but assuming I've not made any typos should be ok. And I dare say there are probably things I've not catered for in it too. No exception handling and should really use the subprocess module instead of os.system for two. Oh and it won't exit until retry_delay seconds after the last attempt has failed.

I've left out the ping as the mount will fail with a non-zero exit code if the network is down. sys.exit is used to both return a non-zero exit code from the script and to write an error message to stderr. That's the bit redirected by "2>" in a shell command or crontab entry.

It's the exit code of the spawned process (your mount command) that gets returned by os.system. The convention is 0 for success, non-zero for a failure, though different commands may use different values to return information about the failure.
Note to self: don't feed the trolls

If I've asked you a question, please answer it. I'm unlikely to be able to help without that information.

Return to “Networking and servers”