Linux

reboot前のsync

rebootコマンドを実行する前にsyncを実行する必要があるか?
システムをリブートする時やシャットダウンする前にはおまじない的に

# sync
# sync
# shutdown -r now

等とやっていた記憶があります。これは現在果たして必須なんでしょうか?

rebootはsync()している?

そもそも、無理にshutdown -r nowを叩かなくても良いのかも知れません。と言うか、私はrebootしてます。(SolarisとかBSDだとどうでしょう…sync; shutdownするのが常識?)

man reboot
システムのランレベルが 0 または 6 でないときに、すなわちシステムが通常に動作しているときに halt や reboot が呼び出されると 、 代 わ り にshutdown(8) が (-h や -r オプションを付けて) 起動される。詳細は shutdown(8) の man ページを参照のこと。

ということで、通常の動作状態であることがわかればreboot(halt)はshutdownを実行します。
ソースを覗いてみましょう。

sysvinit-2.86/src/halt.c

165 int main(int argc, char **argv)
166 {
167         int do_reboot = 0;
168         int do_sync = 1; ★syncフラグ
 :
 :
233     if (!do_hard && !do_nothing) {
234         /*
235          *  See if we are in runlevel 0 or 6.
236          */
237         c = get_runlevel(); ★ここでランレベルを取得
238         if (c != '0' && c != '6') { ★1〜5であればこのルートへ
239             char *file;
240
241             if (do_poweroff) {
242                 file = strdup("/poweroff");
243             } else {
244                 file = strdup("/halt");
245             }
246             close(open(file, O_CREAT|O_RDWR, 0644));
247             free(file);
248
249             do_shutdown(do_reboot ? "-r" : "-h", tm); ★do_rebootフラグが1なら"-r"をdo_shutdown()へ
250         }
251     }
 :
264     if (do_sync) { ★do_shutdown()出来なくても、"-n"オプションが無ければdo_sync=1なのでsync()を呼び出す
265         sync();
266         sleep(2);
267     }
268
sysvinit-2.86/src/shutdown.c

 50 int dosync = 1;         /* Sync before reboot or halt   */
 :
 :
270 void fastdown()
271 {
 :
338     sync(); ★fastdown()はsync()を呼び出す
 :
353     init_reboot(BMAGIC_REBOOT);
354     exit(0);
355 }
 :
 :
360 void shutdown(char *halttype)
361 {
 :
380     if (doself) fastdown(); ★"-n"オプション付きはfastdown()へ
 :
394     /* Now execute init to change runlevel. */
395     sync(); ★shutdown()はsync()を呼び出す
 :
406     closelog();
407     exit(1);
408 }

無理にsyncは叩かなくても良いのでは、と思います。

get_runlevel()は失敗する?

もし/var/run/utmpが壊れていたり、空になって(initsctriptsのrpm更新した直後は空のはず)いたりしてランレベルを特定出来なければ、get_runlevel()は失敗すると思います。このときは、"WARNING: could not determine runlevel - doing soft %s"を出力して強制reboot(halt)へ。
utmpdumpでdumpできればおそらく大丈夫でしょう。壊れているようであれば、まずはutmpを削除してリトライで良いです。

# utmpdump /var/run/utmp
Utmp dump of /var/run/utmp
[8] [00384] [si  ] [        ] [            ] ......
[2] [00000] [~~  ] [reboot  ] [~           ] ......
[1] [20019] [~~  ] [runlevel] [~           ] ......
[8] [01392] [l3  ] [        ] [            ] ......
 :
 :

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2008-08-17 (日) 14:03:44