Single System, Multiple Machines #22006年03月24日 08時39分59秒

さて、前回の同名記事では、一つのシステムイメージを複数の機械で共有しやすくすることで、管理の手間を省く事に成功した。今回はそれを更に一歩進めてみよう。

前回では、ホスト毎に違うファイルを /etc/hostname/${hostname}-${filename} の形で置き、本来の位置からリンクすることによって、ファイルの編集を避けることが出来た。しかし、これではリンクの作成が各ホスト毎に必要になるのである。どうすれば、このリンクを作る手間を最小限に出来るのだろうか。

答えは簡単である。リンクをもう一段階増やせばいいのである。今回のやり方は、/etc/hostname にホスト名のディレクトリを作り、そこに直接ファイルを置くのである。そして、/etc/hostname/${hostname} を指すリンク /hostname から作るのである。そして、全ての設定ファイルはこのリンクを仲介して、目的の物を指すようにすればいい。


# find /etc/hostname -type f
/etc/hostname/host1.example.com/loader.conf
/etc/hostname/host1.example.com/rc.conf
/etc/hostname/host1.example.com/xorg.conf
/etc/hostname/host1.example.com/fstab
/etc/hostname/host2.example.com/loader.conf
/etc/hostname/host2.example.com/rc.conf
/etc/hostname/host2.example.com/xorg.conf
/etc/hostname/host2.example.com/fstab

# ls -ld /hostname
lrwxr-xr-x ... /hostname -> etc/hostname/host1.example.com

# ls -ld /boot/loader.conf /etc/fstab /etc/X11/xorg.conf
lrwxr-xr-x ... /boot/loader.conf -> ../hostname/loader.conf
lrwxr-xr-x ... /etc/X11/xorg.conf -> ../../hostname/xorg.conf
lrwxr-xr-x ... /etc/fstab -> ../hostname/fstab

# head -n 1 /etc/rc.conf
hostname="default.example.com"

# cat /etc/rc.conf.local
. /hostname/rc.conf

これで、わかっただろうか。つまり、システム毎に変えなくていけないのは /hostname が指す先だけである。これを host1 から host2 に変えるだけで、ホスト間の移行は完了である。さらにもう一段進化させて、fstab を起動パーティション毎に変えるのも不可能ではない。なお、/etc/rc.conf はわざと hostname="default.example.com" にしてある。各システムの rc.conf で設定を忘れたときにわかりやすいようにだ。

少々、厄介なのが rc.conf だ。man rc.conf をすると

The /etc/rc.conf file is included from the file /etc/defaults/rc.conf, which specifies the default settings for all the available options. Options need only be specified in /etc/rc.conf when the system administrator wishes to override these defaults. The file /etc/rc.conf.local is used to override settings in /etc/rc.conf for historical reasons. See the rc_conf_files variable below.
とある。rc.conf.local は過去のものだと言われている。sysinstall などは rc.conf に書き足すのである。だんだん、見づらくなっていく。また、rc_conf_files は /etc/defaults/rc.conf で変更しなくては意味が無い。/etc/rc.conf を読むときには既に、$rc_conf_files は展開され、時既に遅しなのである。ここで、rc.conf.local は過去の物だと言われても、使わない手は無い。rc.conf の後に読まれるので、rc.conf の最後の行に無理して入れようとするより、すっきりできる。

以下のが、手順の例である。もし、システム毎に違うファイルが他にもあれば同じ要領で追加していくだけである。


# mkdir -p /etc/hostname/laptop.example.com
# mv /boot/loader.conf /etc/fstab /etc/X11/xorg.conf /etc/hostname/laptop.example.com
# vi /etc/hostname/laptop.example.com/rc.conf
# echo '. /hostname/rc.conf' >> /etc/rc.conf.local

# cd / && ln -s etc/hostname/laptop.example.com hostname
# cd /boot && ln -s ../hostname/loader.conf
# cd /etc && ln -s ../hostname/fstab
# cd /etc/X11 && ln -s ../../hostname/xorg.conf

前回