5

I have a CIFS mount in fstab which works fine, except when something goes wrong with the network or the remote server and it doesn't recover. I want the share to auto-remount in the event of failure, but from what I've read this isn't supported via fstab.

What I've done for now is move it to an autofs mount, which to be fair works well, but I've always found autofs mounts a bit laggy vs fstab (just my perception visually on the command line). Does anyone have any experience of another solution for this?

2 Answers 2

3

As far as remounting the mounts present in your /etc/fstab, you can use this method to force a remount:

$ sudo mount -a -t cifs

Per man mount:

   -t, --types vfstype
   [...]

   More than one type may be specified in a comma  separated  list.
   The  list of filesystem types can be prefixed with no to specify
   the filesystem types on which no action should be taken.   (This
   can be meaningful with the -a option.) For example, the command:

            mount -a -t nomsdos,ext

You could then put a check in a shell script which would force the mount -a -t cifs command to run based on either a scheduled time or come up with a check using inotify or something else that would verify the status of the mounts, and if it detects that they've failed, run the mount .. command I provided above.

0

Auto-mounting (and remounting) with systemd

For the purposes of this example let's say our share is "Loads o' Big-Storage"

(because it works well to illustrate systemd file path and unit file escaping, which most tutorials don't cover)

  1. Create your mount point:
    sudo mkdir -p "/mnt/Loads o' Big-Storage"
    sudo chown -R "$(whoami)":"$(whoami)" "/mnt/Loads o' Big-Storage"
    
  2. Use systemd-escape --path to create a .mount filename:
    my_systemd_mountfile="$(
        systemd-escape --path "/mnt/Loads o' Big-Storage"
    )"
    echo "${my_systemd_mountfile}"
    
    mnt-Loads\x20o\x27\x20Big\x2dStorage
    
  3. Create that file in /etc/system/system/ with literal file path contents (no escapes)
    sudo vim "/etc/system/system/${my_systemd_mountfile}.mount"
    
    [Unit]
    Description=My CIFs Media Mounter
    Requires=network-online.target
    After=network-online.service
    
    [Mount]
    What=//192.168.2.200/Loads o' Big-Storage
    Where=/mnt/Loads o' Big-Storage
    Options=username=foo-user,password=foo-secret
    Type=cifs
    
    [Install]
    WantedBy=multi-user.target
    EOF
    
  4. Create the corresponding .automount file:
    sudo vim "/etc/system/system/${my_systemd_mountfile}.automount"
    
    [Unit]
    Description=My CIFs Media Automounter
    
    [Automount]
    Where=/mnt/Loads o' Big-Storage
    
    [Install]
    WantedBy=multi-user.target
    
  5. Load and enable the service:
    sudo systemctl daemon-reload
    sudo systemctl enable "${my_systemd_mountfile}".automount
    sudo systemctl start "${my_systemd_mountfile}".automount
    

Troubleshooting

If you have troubles, journalctl will have better log info than self-recommended systemd status:

sudo journalctl -xe "${my_systemd_mountfile}".mount

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .