Mount a remote share at boot with systemd

Post your tutorials and howtos here.
User avatar
Arjen Balfoort
Site Admin
Posts: 9254
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Mount a remote share at boot with systemd

Postby Arjen Balfoort » 19 Jul 2014 13:38

With the upcoming ISOs using systemd, I thought it would come in handy to create a tutorial on how to mount a remote share the systemd way, and even execute a script after a successful mount.

Explanation of the place holders:
  • [my-mount]: path to where you want to mount the remote share with the forward slashes replaced with a minus sign. E.g: mount to /media/mynas becomes media-mynas.
  • [my/mount]: the path where you want to mount the remote share.
  • [//remote/share]: network path to the share you want to mount. E.g.: //192.168.1.1/myshare
  • [type]: cifs or nfs.
  • [myuser]: user to own the mount.
  • [mygroup]: group to use the mount.
  • [mountuser]/[mountuserpwd]: user credentials used to mount the remote share.
Create the following files:

/lib/systemd/system/[my-mount].automount

Code: Select all

[Unit]
Description=My Automount Point
DefaultDependencies=no
Wants=network-online.target
After=network-online.target
 
[Automount]
Where=[my/mount]
 
[Install]
WantedBy=multi-user.target
/lib/systemd/system/[my-mount].mount

Code: Select all

[Unit]
Description=Remote Data
DefaultDependencies=no
Wants=network-online.target
After=network-online.target

[Mount]
What=[//remote/share]
Where=[my/mount]
Type=[type]
Options=credentials=/root/.[my-mount].credentials,rw,iocharset=utf8,uid=[myuser],gid=[mygroup],_netdev
StandardOutput=syslog
StandardError=syslog 
/root/.[my-mount].credentials

Code: Select all

username=[mountuser]
password=[mountuserpwd]
Set permissions for this file:

Code: Select all

chmod 640 /root/.[my-mount].credentials
If you want to execute a script or application when the remote share is mounted, create the following file:

/lib/systemd/system/[my-mount].service

Code: Select all

[Unit]
Description=Do something with the mounted share
DefaultDependencies=no
Wants=[my-mount].automount rsync.service 
After=[my-mount].automount rsync.service 

[Service]
Type=oneshot
ExecStart=/bin/bash -c 'path/to/mybash'
ExecStop=/usr/bin/killall rsync

[Install]
WantedBy=multi-user.target
Mind that you have to include all services in "Wants", and "After" needed for the script/application. In this case I wanted to rsync files to the remote share. So, I included rsync.service. ExecStop is not always needed, but in this case shut down takes a lot of time if I don't kill rsync before the actual shut down process starts.

Finally, enable and start the mount, and the service:

Code: Select all

sudo systemctl enable [my-mount].automount
sudo systemctl start [my-mount].automount
sudo systemctl enable [my-mount].service
sudo systemctl start [my-mount].service
Starting the service (last command) will run the script/application you set in the .service file.

Check the moun/share:

Code: Select all

sudo systemctl status [my-mount].automount
sudo systemctl status [my-mount].service
[IMPORTANT]
This will only work with wired connections. If you have a wireless connection, use autofs.


SolydXK needs you!
Development | Testing | Translations

User avatar
Arjen Balfoort
Site Admin
Posts: 9254
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Mount a remote share at boot with systemd

Postby Arjen Balfoort » 06 Aug 2014 18:30

I had the unmount issue on a laptop. So, I adapted the [Unit] section of the .automount, and .mount files.


SolydXK needs you!
Development | Testing | Translations

User avatar
Arjen Balfoort
Site Admin
Posts: 9254
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Mount a remote share at boot with systemd

Postby Arjen Balfoort » 06 Aug 2014 21:25

Unfortunately, that only worked once, but I'll work on it until it's solved.


SolydXK needs you!
Development | Testing | Translations

User avatar
Zill
Posts: 1850
Joined: 13 Aug 2013 14:28
Location: Lincolnshire, UK

Re: Mount a remote share at boot with systemd

Postby Zill » 07 Aug 2014 10:37

I have not used systemd but with sysvinit I found that this problem was easily resolved by using autofs rather than specifying remote servers in fstab. See http://forums.solydxk.nl/viewtopic.php? ... nfs#p39104

The main difference is that the remote server is mounted and unmounted automatically as required, rather than just being mounted once at boot but, functionally, this is unlikely to make any difference to users.

It would be interesting to know if autofs also works well with systemd and, if so, this should work out of the box without needing any additional scripts etc.

User avatar
Arjen Balfoort
Site Admin
Posts: 9254
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Mount a remote share at boot with systemd

Postby Arjen Balfoort » 07 Aug 2014 12:39

I've read a lot of posts that autofs didn't solve this issue with systemd, but I'm going to try nonetheless.


SolydXK needs you!
Development | Testing | Translations

yoast
Posts: 138
Joined: 20 Jan 2014 09:54

Re: Mount a remote share at boot with systemd

Postby yoast » 07 Aug 2014 21:32

It may be wise to explicitly limit "READ" rights of the .credentioals files to ROOT only.

I still use the FSTAB+credentials files and it does work fine for me. My system does not hang if the NAS is down before I switch off the laptop, though I do not know why. You could run sudo mount -a before you shut down.

User avatar
Arjen Balfoort
Site Admin
Posts: 9254
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Mount a remote share at boot with systemd

Postby Arjen Balfoort » 08 Aug 2014 06:48

Do you use systemd?


SolydXK needs you!
Development | Testing | Translations

yoast
Posts: 138
Joined: 20 Jan 2014 09:54

Re: Mount a remote share at boot with systemd

Postby yoast » 08 Aug 2014 09:41

I do not know whether I use sysvinit or systemd I am on SolydK 64 HE with all updates/patches etc run. How can I tell?

I'll be happy to switch, but I do not see a need. Could it be related to the fact that my connections are wired?

User avatar
zerozero
Posts: 5373
Joined: 10 Feb 2013 23:37
Location: West Midlands, England
Contact:

Re: Mount a remote share at boot with systemd

Postby zerozero » 08 Aug 2014 10:02

yoast wrote:I do not know whether I use sysvinit or systemd I am on SolydK 64 HE with all updates/patches etc run. How can I tell?
share the output of

Code: Select all

inxi -Ixx
bliss of ignorance

User avatar
Arjen Balfoort
Site Admin
Posts: 9254
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Mount a remote share at boot with systemd

Postby Arjen Balfoort » 08 Aug 2014 14:01

Indeed, this is only an issue when you have a wireless connection.
That's why I didn't have any problems on my main machine, but had problems on my laptop.

I'll update the OP.


SolydXK needs you!
Development | Testing | Translations

yoast
Posts: 138
Joined: 20 Jan 2014 09:54

Re: Mount a remote share at boot with systemd

Postby yoast » 08 Aug 2014 14:43

Output of inxi -Ixx
Processes: 248 Uptime: 2:37 Memory: 3778.9/7824.5MB
Init: SysVinit v: 2.88 runlevel: 2 default: 2 Gcc sys: 4.9.0 alt: 4.4/4.6/4.7/4.8
Client: Shell (bash 4.3.111 running in konsole) inxi: 2.1.28
I'm not sure if you noticed my comment about making .smbcredentials readable to root only. It is good advice and used https://wiki.ubuntu.com/MountWindowsSharesPermanently as
Change the permissions of the file to prevent unwanted access to your credentials:
chmod 600 ~/.smbcredentials

User avatar
Arjen Balfoort
Site Admin
Posts: 9254
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: Mount a remote share at boot with systemd

Postby Arjen Balfoort » 08 Aug 2014 14:54

I've updated the OP, also with the permissions on the credentials file, but we cross posted.
I've added read permissions for the group, as one might to choose to setup a group (given with gid) that should have access to the mount (which is in my case).


SolydXK needs you!
Development | Testing | Translations


Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 2 guests