4

I would like to create a service file that, when started, causes a specified filesystem to be mounted before the action of the service file is performed (as it needs that file system to be available). Furthermore, I would like this service file to fail gracefully in case the file system cannot be mounted (e.g., because it is located on a sometimes unplugged external hard drive).

My first try was simple and partly successful: add x-systemd.automount as an option in the filesystem's fstab line. Then the filesystem is automatically mounted when the service file causes its mountpoint to be accessed. However, it does not fail gracefully in the sense that there is a long timeout and it is logged as an ERROR.

My second try was not as successful, I added BindsTo=path-to-mountpoint.mount and After=path-to-mountpoint.mount to the service file (x-systemd.automount was removed): the path-to-mountpoint.mount is not found. I guess it only is created upon successful mount, so it is not meant for what I want to do.

2

According to the manual:

Mounts listed in /etc/fstab will be converted into native units dynamically at boot and when the configuration of the system manager is reloaded.

This means that you can add the following to the [Unit] section of your .service file:

Requires=opt.mount
After=opt.mount

(this is and example for my case where I mount /opt separately in my fstab file).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.