0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

特定のネットワークに接続したら自動的にファイルシステムをマウントする

0
Posted at

概要

  • NetworkManagerDispatch機能を用いてネットワーク接続時にスクリプト実行
  • systemdtargetを用いて複数のサービスを一括管理

私は普段ノートパソコンを持ち歩いていて、なおかつ家にファイルサーバがあります。
なので、家のWifiまたはVPN接続時に自動でネットワーク上のファイルシステムをマウントしよう、ということです。

大まかな流れとしてはNetworkMangerがネットワークの変更を検知してsystemdのファイルマウントを開始・停止する、といった流れです。

NetworkMangerの設定

NetworkManagerdispatch機能を有効化します。

$ sudo systemctl enable NetworkManager-dispatcher.service
$ sudo systemctl start NetworkManager-dispatcher.service
$ systemctl status NetworkManager-dispatcher.service

有効化すると/etc/NetworkManager/dispatcher.d下のスクリプトがネットワーク変更時に実行されるので、これを用意します。
これを書く際に使える変数については参考文献を参照してください。
ここではユーザーのsystemdhome-network.targetstart/stopしています。モチベーションとしてはこのtargetにいろいろなサービスをフックして(ファイルマウント以外にも)接続時にいろいろと自動化したい、というのがあります。

/etc/NetworkManager/dispatcher.d/10-home_network.sh
#!/bin/bash

# Activates home-network.target when the Neutrino Wi-Fi or VPN connections are established, and deactivates it when they are disconnected.

WIFI_UUID="<your uuid>"
VPN_UUID="<vpn uuid>"
USER="<username>"

current_status=$(systemctl --machine=$USER@.host --user is-active home-network.target)

# Stop target on unexpected disconnection
if [[ "$NM_DISPATCHER_ACTION" == "down" ]]; then
  systemctl --machine=$USER@.host --user stop home-network.target
fi

# Skip if the connection is not related
if [[ "$CONNECTION_UUID" != "$WIFI_UUID" && "$CONNECTION_UUID" != "$VPN_UUID" ]]; then
  exit 0;
fi


# If the connection is active and the target is inactive, start the target.
if [[ "$current_status" == "inactive" ]]; then
  if [[ "$NM_DISPATCHER_ACTION" == "up" || "$NM_DISPATCHER_ACTION" == "vpn-up" ]]; then
    systemctl --machine=$USER@.host --user start home-network.target
  fi
fi

# If the connection is about to be disconnected and the target is active, stop the target.
if [[ "$current_status" == "active" ]]; then
  if [[ "$NM_DISPATCHER_ACTION" == "pre-down" || "$NM_DISPATCHER_ACTION" == "vpn-pre-down" ]]; then
    systemctl --machine=$USER@.host --user stop home-network.target
  fi
fi

pre-downの際にはdispatcher.d/pre-down.d/下のファイルが実行されるのでそこにもシンボリックリンクを貼っておきます。ここでは一つのファイルを書いてシンボリックリンクで写していますが、pre-downの動作を別ファイルにしてここにおいてあげても大丈夫です。

ln -s /etc/NetworkManager/dispatcher.d/10-home_network.sh /etc/NetworkManager/dispatcher.d/pre-down.d/10-home_network.sh

権限などを調整します。

# chown root:root <your bash script>
# chmod +x <your bash script>

Unitファイル(例)

最後にhome-network.targetに依存するサービスを書きます(ここでは例としてsshfsでの接続を想定します)。

.config/systemd/user/rclone-home-server.service
[Unit]
Description=Rclone home-server Mount
After=home-network.target
BindsTo=home-network.target
StopWhenUnneeded=yes

[Service]
Type=simple
ExecStart = /usr/bin/rclone mount home-server:/home/<username> /home/<username>/home-server \
  --vfs-cache-mode full \
  --vfs-write-back 0s \
  --dir-cache-time 1s \
  --attr-timeout 1s \
  --poll-interval 0 \
  --vfs-read-ahead 1M
ExecStopPost=/bin/fusermount -u /home/<username>/home-server
Restart=always
RestartSec=10

[Install]
WantedBy=default.target

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?