LoginSignup
5
6

More than 5 years have passed since last update.

autofs で cifs マウントした中のディレクトリを mount --bind する

Posted at

Windows のフォルダ共有を Linux で cifs でマウントして、Windows でコードを修正 → Linux で実行、のような流れで開発を行っているのですが、ネットワークファイルシステムは I/O が遅く、PHP の composer の vendor/ とか node.js の node_modules/ のようなディレクトリまで cifs 経由でのアクセスになるとかなりのストレスです。

なので、下記の記事のように cifs をマウントした中のディレクトリに mount --bind で Linux のローカルのファイルファイルシステムをマウントするようにしています。

この記事の通り netfs を使って Linux の起動時にマウントするようにしているのですが、同僚が、autofs も便利だよー、と言っていたので使ってみました。

最初に少し試してみた感じでは↑のようなマウントが入れ子になっている構造はできなさそうかと思ったのですが、man をよく読んでみたらできました。

autofs の設定と cifs の自動マウント

autofs をインストールします。CentOS 7 を使っているので yum で入れます。

yum install autofs

マスタマップファイルにエントリを追加します。/etc/auto.master に追記しなくても /etc/auto.master.d/ に拡張子 .autofs で作成すれば追加で読まれる(auto.master でそのように設定されていれば)ので oreore.autofs というファイル名で作ります。

cat <<EOS> /etc/auto.master.d/oreore.autofs
/-   /etc/auto.oreore
EOS

次にマップファイル(/etc/auto.oreore)を作ります。マスタマップファイルで /- としているので、マップファイルには絶対パスでマウントポイントを記述します。

cat <<EOS> /etc/auto.oreore
/oreore/sandbox -fstype=cifs,rw,credentials=/root/.cifs ://ore-no-pc/sandbox
EOS

マウントポイントを作ります。

mkdir -p /oreore/sandbox

/root/.cifs に ID/PW を書きます。

touch /root/.cifs
chmod 600 /root/.cifs
vim /root/.cifs
username=oreore
password=ore-no-password

autofs を起動します。

systemctl start autofs

/oreore/sandbox を ls してみます。

ls /oreore/sandbox

Windows のフォルダ共有が cifs で自動的にマウントされて、フォルダの中身が見えていれば成功です。

cifs と bind で別々に設定 → 失敗

↑でマウントしたディレクトリの中の local/ というディレクトリに、Linux 上の /tmp をマウントするように autofs で設定してみます。

cat <<EOS> /etc/auto.oreore
/oreore/sandbox -fstype=cifs,rw,credentials=/root/.cifs ://ore-no-pc/sandbox
/oreore/sandbox/local -bind :/tmp/
EOS

autofs を再起動します。

systemctl stop autofs
umount -a -t cifs
umount -a -t autofs
systemctl start autofs

/oreore/sandbox を ls してみます。

ls /oreore/sandbox

Windows のフォルダ共有の中身が見えているので、次は /oreore/sandbox/local を ls してみます。

ls /oreore/sandbox/local

うーん、ダメです。local/ のマウントがされる気配がありません。

Multiple Mounts で設定してみる → 成功

man 5 autofs をよく読んでみると、次のような説明がありました。

Multiple Mounts
   A multi-mount map can be used to name multiple filesystems to mount.  It takes the form:

     key [ -options ] [[/] location [/relative-mount-point [ -options ] location...]...

   This may extend over multiple lines, quoting the line-breaks with `\´.  If present, the per-mountpoint mount-options are appended to  the  default
   mount-options. This behaviour may be overridden by the append_options configuration setting.

これを参考に、次のように変更しました。

cat <<EOS> /etc/auto.oreore
/oreore/sandbox -fstype=cifs,rw,credentials=/root/.cifs ://ore-no-pc/sandbox /local -bind :/tmp/
EOS

autofs を再起動します。

systemctl stop autofs
umount -a -t cifs
umount -a -t autofs
systemctl start autofs

/oreore/sandbox を ls してみます。

ls /oreore/sandbox

Windows のフォルダ共有の中身が見えたので、次は /oreore/sandbox/local を ls してみます。

ls /oreore/sandbox/local

Linux の /tmp が見えました。

先に /oreore/sandbox/local の方を ls しても大丈夫だったので、この設定で問題なさそうです。

なお、次のように複数のディレクトリを指定することもできます。

cat <<'EOS'> /etc/auto.oreore
/oreore/sandbox -fstype=cifs,rw,credentials=/root/.cifs ://ore-no-pc/sandbox \
  /local -bind :/tmp/ \
  /misc  -bind :/tmp/
EOS

これは便利。

5
6
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
5
6