目次: Linux
昨日(2015年3月5日の日記参照)に引き続き、何となく自分ではわかっているつもりでしたが、いざ変更しようとしたら全然わかっていなかったSysV initの話です。
まずはSysV initの動きのおさらいから。
前提: ディストリビューションはDebian GNU/Linux 7.8(Wheezy) 1. Linux Kernel起動終盤にkernel_init() が /sbin/initを起動 2. /sbin/initは /etc/inittabを見る 3. /etc/inittabにはinitへの指示が書いてある 3-1. デフォルトのrunlevelは2である 3-2. ブート時に /etc/init.d/rcSを実行せよ 3-2-1. /etc/init.d/rcSは /etc/init.d/rc Sを起動する 3-3. runlevel 2なら /etc/init.d/rc 2を実行せよ ↑↑↑↑↑ 以上、おさらい1の部分 ↑↑↑↑↑ 4. 下記がいずれか一つでも成立すればCONCURRENCY=none成立しなければCONCURRENCY=makefile - /etc/init.d/.depend.bootが存在しないか、空ファイル - /etc/init.d/.depend.startが存在しないか、空ファイル - /etc/init.d/.depend.stopが存在しないか、空ファイル - /etc/init.d/.legacy-bootorderingが存在する - startpar -vコマンドが失敗する 5. /etc/rcXXXX.d/S* をアルファベット順に実行 CONCURRENCY=noneならば、単に実行するだけ、 CONCURRENCY=makefileならば、startparに起動を任せる
さて、お忘れの方も多いかと思いますが、本題は「initの処理に何か足したい」でした。
もしCONCURRENCY=noneつまり直列版のinitを使っている場合、initに何か足す方法は非常に簡単です。今まで見てきたように、/etc/rcS.d以下にスクリプトファイルを足せば、/etc/init.d/rcSが実行してくれます。
しかしCONCURRENCY=makefileつまり並列版のinitを使っている場合、/etc/rcS.d以下にスクリプトファイルを足すだけでは実行されません。なぜかというとstartparというコマンドが一枚噛んでいるためです。
このstartparというコマンドには2つのモードがあります。
NAME startpar - start runlevel scripts in parallel SYNOPSIS startpar [-p par] [-i iorate] [-t timeout] [-T global_timeout] [-a arg] prg1 prg2 ... ★1 startpar [-p par] [-i iorate] [-t timeout] [-T global_timeout] -M [ boot|start|stop] ★2
一つは ★1側の、渡された引数を実行するCONCURRENCY=noneに近い動作をするモード(引数に -Mを指定しない)です。
もう一つは ★2側の、別の設定ファイルからスクリプトの依存関係を得て、出来る限り並列に実行する(引数に -Mを指定する)モードです。
さて /etc/init.d/rcでは、startparの2つモードのうち、どちらが使われていたでしょうか?覚えている人はスゴい暗記力です。私も含めて忘れてしまった方のために、もう一度コードを見ます。
#
# Start script or program.
#
case "$CONCURRENCY" in
makefile|startpar|shell) # startpar and shell are obsolete
CONCURRENCY=makefile
log_action_msg "Using makefile-style concurrent boot in runlevel
$runlevel"
startup() {
eval "$(startpar -p 4 -t 20 -T 3 -M $1 -P $previous -R $
runlevel)" ★1
簡単ですね。別の設定ファイルを見る(-Mを指定する)モードです。しかもよく見るとstartup() に渡されているスクリプト名($2以降の引数)は無視されています。だから /etc/rcS.d/ にスクリプトを足すだけでは、実行されなかったのです。
ではstartparに何か処理を追加するには、何をどうしたら良いのか?と言う話です。こういうときはまずstartparのマニュアルを見てみます。
The -M option switches startpar into a make(1) like behaviour. This option takes three different arguments: boot, start, and stop for read- ing .depend.boot or .depend.start or .depend.stop respectively in the★1 directory /etc/init.d/. By scanning the boot and runlevel directories in /etc/init.d/ it then executes the appropriate scripts in parallel. FILES /etc/init.d/.depend.boot /etc/init.d/.depend.start /etc/init.d/.depend.stop SEE ALSO init(8) insserv(8). ★2
★1の説明を見るに、-Mはboot, start, stopのうちどれか1つ引数を取って、実際の動作は .depend.boot or .depend.start or .depend.stopで決まりますよ、というようなことが書いてあります。親切なことにFILESの章にファイルのフルパスまで書いてくれています。
これら3つの .depend.XXXXファイルを更新すれば良い、ということがわかりましたが、どうやって更新するのかが書いていなくて困ってしまいます。とりあえず★2のSEE ALSOの章にあるinsservという奴が新顔で怪しいので、マニュアルを見ます。
INSSERV(8) INSSERV(8) NAME insserv - boot sequence organizer using LSB init.d script dependency information (...略...) FILES /etc/insserv.conf configuration file for insserv which defines the LSB System Facilities. (...略...) /etc/init.d/.depend.boot, /etc/init.d/.depend.start, /etc/init.d/.depend.stop The make(1) like dependency files produced by insserv for boot‐ ing, starting, and stopping with the help of startpar(8). ★1
コマンドの説明はちょっと抽象的でわかりづらいですが、FILESの ★1の部分を見る限り、探し求めていた物に間違いないでしょう。
1. /etc/rcS.d/ にS20script.shのような名前のスクリプトを追加する 2. startpar -Mを使っている場合は、insservを実行して設定ファイルを更新する
まとめてしまえば短いものですが、これもinitの仕組みの一端だと思うと、中々に感慨深いもんがありますね。
< | 2015 | > | ||||
<< | < | 03 | > | >> | ||
日 | 月 | 火 | 水 | 木 | 金 | 土 |
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 | - | - | - | - |
合計:
本日: