afuse 用户级自动挂载程序与 sshfs 集成,作为本地用户登录生成的 systemd 服务

afuse 用户级自动挂载程序与 sshfs 集成,作为本地用户登录生成的 systemd 服务

编辑2020:我一直在使用下面发布了 systemd-answer已经好几年了,我对此感到非常满意。

为了方便处理远程文件,我设置afusesshfs.bashrc从本地用户运行。它可以工作,在 bash init 后,所有远程树都~/.ssh/config/按需安装在 下~/scp/,并具有正确的访问权限/限制。

当前位于(来自的文件).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

(上面已经包含了@sato-katsura 的有用提示)

上述内容确保它正常运行直到机器关闭。它与 X 次停止/启动无关。如果发生 afuse 崩溃(我从未观察到),下一个 shell 会重新生成它。

即使在将其迁移到 之前,您是否看到任何有用的优化、陷阱或有其他提示systemd?您如何将其包装到 systemd 用户单元中?

答案1

我一直在用这个几年前的用户服务单元定义文件: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

afuse.service在以下位置创建包含上述内容的文件:

  • ~/.config/systemd/user/如果仅为您安装
  • /etc/systemd/user/如果为所有本地用户安装

安装此服务:

mkdir ~/scp  # Or Home Directories of all existing 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

如果需要,您可以将 SSH-AGENT 配置为将套接字不放置在 中/tmp/...,而是放置在 下的某处~,那么该服务应该能够使用代理。 (如果有需求,我也可以添加确切的步骤,@dirdi)

相关内容