自動マウント機能の設定の仕方のメモです。
 
OSはCentOS6.8(nfsサーバはCentOS7.2) です。
以下のNFSサーバの共有ディレクトリをautomount(autofs)でマウントします。
10.0.1.30:/exports/data
 
すっごく簡単。特にマウントポイントをあらかじめ用意するわけでもなく、設定ファイルに記述すれば、使いたい時にそのマウントポイントにアクセスすれば勝手にマウントするっていう機能です。
普段は隠匿しておきたいバックアップなどで使うマウントポイントとか、DVDなんか毎回マウント処理すんの面倒だから、CLIを使う人は便利なんだろうね。
 
設定の流れは、
・autofsをインストールして
・/etc/auto.masterでマウント先を指定。(といってもデフォルトw)
・/etc/auto.miscでマウントしたいNFSを書くだけ。
・そして/etc/init.d/autofs reload コマンドで設定ファイルを反映。
・あとは、そのマウントポイントを参照すれば自動でマウントしてくれる。

ポイントは、
・普通/etc/fstabで記述するのとは違い、一定の時間(なんか5分くらいかな)がくると勝手にアンマウントされているってこと。
 なんもしないで5分くらいしてからmountコマンドなんかで確認するとわかる。
 まあこの自動アンマウントの時間設定は変えられるらしいです。
・マウントポイントの親ディレクトリは、デフォルトで/miscの下になっている。これは/etc/atuo.masterで設定すれば変更や追加指定できる。
 ここで追加といっているのは、/misc以外にも/mntを追加で親ディレクトリとして指定したい場合とかです。
 NFSは共有ディレクトリなんだからいくつでも同時にマウントできる。この使い方になんのメリットがあるかわからんけどできてしまいます。
 スクリプトで処理を分けたい場合は違うマウントポイントを記載しておくと便利なのかもしれませんね。

参考文献
 http://www.maruko2.com/mw/automount_の設定
 ここの説明が秀逸です。一番構築観点という意味で気になるポイントを押さえて解説されています。


👉マウントして現在のマウント状態を表示しています。ただそれだけw
[root@autofs01 ~]# mount
/dev/mapper/vg_mysql01-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

👉autofsが存在しないことを検索しているだけです。rpm -qa | grep autonfs の方がいいのかな。間違っても大規模なシステムではやらないでくださいw
[root@autofs01 ~]# locate autonfs

👉いないことがわかったのでインストール開始。 
[root@autofs01 ~]# yum install autofs
読み込んだプラグイン:fastestmirror, security
インストール処理の設定をしています
Determining fastest mirrors
 * base: ftp.jaist.ac.jp
 * extras: ftp.jaist.ac.jp
 * updates: ftp.jaist.ac.jp
base                                                                                                                                  | 3.7 kB     00:00     
extras                                                                                                                                | 3.3 kB     00:00     
pgdg96                                                                                                                                | 4.1 kB     00:00     
pgdg96/primary_db                                                                                                                     | 131 kB     00:00     
updates                                                                                                                               | 3.4 kB     00:00     
updates/primary_db                                                                                                                    | 3.5 MB     00:00     
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> Package autofs.i686 1:5.0.5-122.el6 will be 更新
---> Package autofs.i686 1:5.0.5-123.el6_8 will be an update
--> 依存性解決を終了しました。

依存性を解決しました

=============================================================================================================================================================
 パッケージ                         アーキテクチャ                   バージョン                                    リポジトリー                         容量
=============================================================================================================================================================
更新:
 autofs                             i686                             1:5.0.5-123.el6_8                             updates                             720 k

トランザクションの要約
=============================================================================================================================================================
アップグレード       1 パッケージ

総ダウンロード容量: 720 k
これでいいですか? [y/N]y
パッケージをダウンロードしています:
autofs-5.0.5-123.el6_8.i686.rpm                                                                                                       | 720 kB     00:00     
rpm_check_debug を実行しています
トランザクションのテストを実行しています
トランザクションのテストを成功しました
トランザクションを実行しています
  更新                    : 1:autofs-5.0.5-123.el6_8.i686                                                                                                1/2 
  整理中                  : 1:autofs-5.0.5-122.el6.i686                                                                                                  2/2 
  Verifying               : 1:autofs-5.0.5-123.el6_8.i686                                                                                                1/2 
  Verifying               : 1:autofs-5.0.5-122.el6.i686                                                                                                  2/2 

更新:
  autofs.i686 1:5.0.5-123.el6_8                                                                                                                              

完了しました!

👉/etc/auto.master の中身を見てみます。親マウントポイントは/miscのまま使用するので編集はしません。

[root@autofs01 ~]# cat /etc/auto.master 
#
# Sample auto.master file
# This is a 'master' automounter map and it has the following format:
# mount-point [map-type[,format]:]map [options]
# For details of the format look at auto.master(5).
#
/misc /etc/auto.misc 👈ここの記述が親マウントポイントの指定するところです。変更したい場合や追記はここで編集ですね。
#
# NOTE: mounts done from a hosts map will be mounted with the
# "nosuid" and "nodev" options unless the "suid" and "dev"
# options are explicitly given.
#
/net -hosts
#
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
+auto.master

👉さあ/etc/auto.misc に実際のnfs情報を記述しましょう。

[root@autofs01 ~]# vi /etc/auto.misc 

👉今回は以下の通り記述します。

nfsdata -fstype=nfs,rw,noac 10.0.1.30:/exports/data

・マウントポイント=nfsdata
・ファイルシステムタイプ=nfs
・マウントオプション=rw読み書き,noac同期する
 ※nfsのオプションはデリケートですので、わからない場合はデフォルトのままで利用して問題があればオプションを変えていきましょう。
・nfsでマウント元ディレクトリ情報


[root@autofs01 misc]# cat /etc/auto.misc 
#
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# Details may be found in the autofs(5) manpage

cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom

# the following entries are samples to pique your imagination
#linux -ro,soft,intr ftp.example.org:/pub/linux
#boot -fstype=ext2 :/dev/hda1
#floppy -fstype=auto :/dev/fd0
#floppy -fstype=ext2 :/dev/fd0
#e2floppy -fstype=ext2 :/dev/fd0
#jaz -fstype=ext2 :/dev/sdc1
#removable -fstype=ext2 :/dev/hdd

nfsdata -fstype=nfs,rw,noac 10.0.1.30:/exports/data


👉それでは設定を反映させます。autofs再起動でもいいのかもしれませんがこれでいけましたよ。
[root@autofs01 misc]# /etc/init.d/autofs reload
maps を再読み込み中

👉さあマウントできているかな?
[root@autofs01 misc]# ll /misc/nfsdata
合計 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 11月 28 23:56 2016 test

👉できてましたね。testファイルが存在していますが元々他のサーバーでマウントしていたときにつくっていたファイルです。

👉5分待てば勝手にアンマウントします。そこは勝手に時間を割いて確認してください。w
 とりあえずアンマウントを明示的にしても、またnfsディレクトリを参照しに行った時に自動でマウントしてくれるのか確認してみました。

[root@autofs01 misc]# umount /misc/nfsdata

[root@autofs01 misc]# mount
/dev/mapper/vg_mysql01-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

[root@autofs01 misc]# ll /misc/nfsdata
合計 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 11月 28 23:56 2016 test

[root@autofs01 misc]# mount
/dev/mapper/vg_mysql01-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
10.0.1.30:/exports/data on /misc/nfsdata type nfs (rw,noac,sloppy,vers=4,addr=10.0.1.30,clientaddr=10.0.1.22)

👉はい。ちゃんと自動マウントしてくれますね!



簡単足早にアウトマウント機能について説明してみました。
それでは、さよならさよなら。