Solved mount_smbfs works at cli, fails in rc script

I'm just getting to grips with FreeBSD so I may well be missing something very obvious so apologies in advance for that!

So I'm trying to mount a Windows share at boot time. I've created an rc script in /usr/local/etc/rc.d/ as I gather this is the proper way to do it, but it fails.

My /usr/local/etc/rc.d/mounts script is this:

Code:
#!/bin/sh

# PROVIDE: mounts
# REQUIRE: NETWORKING

. /etc/rc.subr

name="mounts"
rcvar=`set_rcvar`
start_cmd="mount_all"
stop_cmd=":"

load_rc_config $name

mount_all()
{
  if checkyesno ${rcvar}
  then
    NETWORK_OK=$(ping -c 3 hq | grep ' 0.0% packet loss')

    if [ "x${NETWORK_OK}" == "x" ]
    then
        echo "$(date '+%a %d %T') $(basename $0) Could not ping hq, so hq_store not mounted." >> /var/log/messages
        exit 1;
    else
        mount_smbfs -N -I hq //administrator@hq/store /mounts/hq_store/
    fi
  fi
}

run_rc_command "$1"

I have put details into /root/.nsmbrc as follows:

Code:
[HQ:USERNAME]
addr=hq.laputa
user=username
password=foobar

Running the command:

Code:
mount_smbfs -N -I hq //administrator@hq/store /mounts/hq_store/

..at the command line as root works fine. When run in the script though I get the error:

Code:
[root@misspiggy /usr/local/etc/rc.d]# service mounts start
/usr/local/etc/rc.d/mounts: DEBUG: checkyesno: mounts_enable is set to YES.
/usr/local/etc/rc.d/mounts: DEBUG: run_rc_command: doit: mount_all
/usr/local/etc/rc.d/mounts: DEBUG: checkyesno: mounts_enable is set to YES.
mount_smbfs: unable to open connection: syserr = Authentication error

It seems like it's a mount_smbfs(8) thing where it's not reading the /root/.nsmbrc file for some reason. Any ideas?

Thanks in advance.

Chris
 
Actually it's a service(8) behavior and it's intentional. The environment is sanitized when things are started. See the excerpt from the man page.
Code:
ENVIRONMENT
     When used to run rc.d scripts the service command sets HOME to / and PATH
     to /sbin:/bin:/usr/sbin:/usr/bin which is how they are set in /etc/rc at
     boot time.

As far as a solution, I don't use enough SMBFS mounts to recommend one but keep the above in mind as you test solutions.
 
I would argue that creating an entry in /etc/fstab with the failok option is the proper way of doing what you want. With failok you are not dropped into single user mode if mounting the share fails (for example because the network is down).

You can also move /root/.nsmbrc to /etc/nsmb.conf. See nsmb.conf(5).

Here is a howto that helped me. Don't forget to add failok.
 
Back
Top