2007年4月23日月曜日

Debian ネットワーク起動

 Debian(etch)のネットワーク起動について調査した。Debianの起動スクリプトは/etc/init.d/にあり、ネットワーク関係は/etc/init.d/networkになる。

start)
        process_options
        log_action_begin_msg "Configuring network interfaces"
        if ifup -v -a; then
            log_action_end_msg $?
        else
            log_action_end_msg $?
        fi
        ;;

 'ifup -v -a'を実行し、ネットワークインターフェースを起動している。ifupはDebian独自のユーティリティのようで、manページによると'-a'オプションは、/etc/network/interfacesの中でautoに指定されいているものを起動する。/etc/network/interfacesは以下のようになっていた。

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# This is a list of hotpluggable network interfaces.
# They will be activated automatically by the hotplug subsystem.
mapping eth0
        script grep
        map eth0

# The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp

 autoになっているのはループバックデバイスloのみ。試しに、/etc/init.d/networking stopを実行すると、eth0, loともに停止する。しかし、/etc/init.d/networking startを実行しても、loしか起動しない。いろいろ試したところ、/etc/init.d/udev startを実行すると、eth0が起動した。これは、udevdを起動するスクリプトである。

 udevdが何かを調べてみたらSUSE Linuxのマニュアルにいろいろ書いてあった。SUSE Linux Enterprise Server Manual (PDF)の25章udevを使用した動的カーネルデバイス管理によると、udevdは動的にデバイスを管理するためのものであり、それまでのhotplugを置き換えるもののようだ。 udevdは、/etc/udev/rules.d/*.rulesディレクトリィに記述されルールに従って処理を行う。この場所にz55_hotplug.rulesがありここにnetwork関連の記述があった。

SUBSYSTEM=="net",                               RUN+="net.agent"

 net.agentを実行するようになっており、/lib/udev/net.agentがそれに相当するようだ。これはシェルスクリプトであり、net_ifup()関数でifupを実行している。

net_ifup() {
    check_program /sbin/ifup

    if grep -q '^auto[[:space:]].*\<'"$INTERFACE"'\>' \
            /etc/network/interfaces; then
        # this $INTERFACE is marked as "auto"
        IFUPARG='\('$INTERFACE'\|-a\|--all\)'
    else
        IFUPARG=$INTERFACE
    fi

    if ps -C ifup ho args | grep -q "$IFUPARG"; then
        debug_mesg "Already ifup-ing interface $INTERFACE"
        exit 0
    fi

    wait_for_interface lo

    exec ifup --allow=hotplug $INTERFACE
}

 ついでに上記ででてくるwait_for_interfaceは、sysファイルシステムを使ってネットワークインターフェースの状態を判定している。

wait_for_interface() {
    local interface=$1

    while :; do
        local state="$(cat /sys/class/net/$interface/operstate 2>/dev/null || true)"
        if [ "$state" != down ]; then
                return 0
        fi
        sleep 1
    done
}

■ 参考資料

SUSE Linux Enterprise Serverマニュアル(PDF)
ifupのmanページ

0 件のコメント: