Skip to content

Automount sshfs server

Learn how to create reliable OpenRC init.d automount scripts for sshfs and rclone. This guide covers automatic mounting of remote SSH servers and Google Drive using FUSE, with proper network dependency handling, PID management, and Gentoo-compatible best practices


First of all we must create our mount path

mkdir -vp /mnt/server

Here is an example how to build an automounter of an sshfs server with /etc/init.d/sshfs-server

#!/sbin/openrc-run

description="Mount remote server via sshfs"
command="/usr/bin/sshfs"
command_args="-f -o allow_other,reconnect,ServerAliveInterval=15,ServerAliveCountMax=3,IdentityFile=/root/.ssh/id_ed25519 username@server:/ /mnt/server"

pidfile="/run/sshfs-server.pid"

start_stop_daemon_args="--background --make-pidfile"

depend() {
  need NetworkManager
  after dbus
}

start_pre() {
    checkpath -d -m 0755 /mnt/server
    mountpoint -q /mnt/server && return 0

    local i=0
    while ! ip route show default 2>/dev/null | grep -q .; do
        i=$((i+1))
        if [ "$i" -ge 20 ]; then
            eerror "No default route after 20s, aborting sshfs mount"
            return 1
        fi
        sleep 1
    done

    i=0
    while ! getent hosts linux-shell.se >/dev/null 2>&1; do
        i=$((i+1))
        if [ "$i" -ge 20 ]; then
            eerror "DNS lookup failed for linux-shell.se after 20s, aborting sshfs mount"
            return 1
        fi
        sleep 1
    done
}

stop() {
    ebegin "Unmounting /mnt/server"

    if mountpoint -q /mnt/server; then
        /bin/fusermount -u /mnt/server
    fi

    if [ -f "$pidfile" ]; then
        local pid
        pid="$(cat "$pidfile" 2>/dev/null)"

        if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
            start-stop-daemon --stop --pidfile "$pidfile"
        fi

        rm -f "$pidfile"
    fi

    return 0

Mount sshfs server to /mnt/server

/etc/init.d/sshfs-server start

Unmount and stop sshfs server

/etc/init.d/sshfs-server stop
  • or

Unmount and stop sshfs server

rc-service sshfs-server stop

Add sshfs-server to default runlevel

rc-update add sshfs-server default