2

To facilitate working with remote files, I setup afuse with sshfs to run from .bashrc of local users. It works, after bash init all remote trees from ~/.ssh/config/ are mounted on-demand under ~/scp/, with correct access rights/restrictions.

This is currently in (a file, that is sourced from) .bashrc:

exists(){ [[ ${1:0:1} == "/" ]] && { test -f $1 || test -d $1; } \
          || command -v $1 >/dev/null 2>&1;                        }

# Run an afuse process as daemon
if pgrep -u $USER -f "afuse.*$USER/scp" >/dev/null ; then
  echo "afuse/sshfs already running"
else
  exists afuse && exists sshfs && exists $HOME/scp \
  && export TAfuseX=$(mktemp --suffix=__afuse) && chmod u+x "$TAfuseX" \
  && echo -e "#!/bin/sh\ngrep '^Host ..' ~/.ssh/co* |colrm 1 5" > $TAfuseX \
  && echo "running afuse/sshfs with nohup" \
  && nohup afuse -o mount_template="sshfs %r:/ %m" \
                 -o unmount_template="fusermount -u -z %m" \
                 -o populate_root_command=$TAfuseX \
                 ~/scp 2>&1 > ~/afuse.log
fi

(The above already incorporates a helpful hint from @sato-katsura)

The above makes sure, it normally runs until machine-shutdown. It is independent of X stops/starts. In case of afuse crash (has never been observed by me), the next shell re-spawns it.

Do you see any useful optimizations, pitfalls, or have other hints, even before migrating this to systemd? How would you wrap that into a systemd user unit?

0

I have started using this user service unit definition file for a while: afuse.service

[Unit]
Description="SSHFS via Afuse automounter"
AssertPathExists=%h/scp/
AssertFileIsExecutable=/usr/bin/afuse
AssertFileIsExecutable=/usr/bin/sshfs

[Service]
Type=forking
WorkingDirectory=%h/scp
ExecStart=/usr/bin/afuse \
    -o fsname=AutoSCP \
    -o timeout=300 \
    -o auto_unmount \
    -o flushwrites \
    -o mount_template="sshfs -o ServerAliveInterval=10 -o reconnect %%r:/ %%m" \
    -o unmount_template="fusermount -u -z %%m" .
Restart=always
PrivateTmp=true
#NoNewPrivileges=true  <- That option breaks this unit, why?

[Install]
WantedBy=default.target

Create the file afuse.service in:

  • ~/.config/systemd/user/ if installing only for you
  • /etc/systemd/user/ if installing for all local users

Install this service with:

mkdir ~/scp  # Or Home Directories of all users, /etc/skel/ 
systemctl --user daemon-reload   # If root, then omit '--user'
# If enabling only for the current user:
systemctl --user enable afuse.service
# If enabling for all users, execute as root:
# systemctl --user --global enable service
systemctl --user start afuse.service

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.