TenForward

技術ブログ。はてなダイアリーから移転しました

Ubuntu 12.04 の lxc (1)

続くかわかりませんが,一応 (1) としときます.:-)

Ubuntu 12.04 の lxc (libvirt じゃない) で起動した Ubuntu 12.04 ですが,特にコンテナ特有のパッケージがインストールされてるわけでもなさそうなので,どういう仕組みでコンテナかどうか判断してるのかなと思いましたが,container-detect.conf という init ファイルがあって,そこでコンテナかどうか判断して,あとは環境変数渡してるってことですね.

このファイルは upstart パッケージに入っていて,コンテナでなくてもジョブは実行されます.

description "Track if upstart is running in a container"

start on mounted MOUNTPOINT=/run

env container
env LIBVIRT_LXC_UUID

emits container
emits not-container

pre-start script
    # The "standard" way of telling if we are in a container
    # is to check for "container" in init's environment.
    # The code below is for cases where it's not set.

    # Detect old-style libvirt
    if [ -z "$container" ]; then
        [ -n "$LIBVIRT_LXC_UUID" ] && container=lxc-libvirt
    fi

    # Detect OpenVZ containers
    if [ -z "$container" ]; then
        [ -d /proc/vz ] && [ ! -d /proc/bc ] && container=openvz
    fi

    # Detect Vserver containers
    if [ -z "$container" ]; then
        VXID="$(cat /proc/self/status | grep ^VxID | cut -f2)" || true
        [ "${VXID:-0}" -gt 1 ] && container=vserver
    fi

    if [ -n "$container" ]; then
        echo "$container" > /run/container_type || true
        initctl emit --no-wait container CONTAINER=$container
        exit 0
    fi

    # If not a container, stop there
    rm -f /run/container_type
    initctl emit --no-wait not-container
    stop
end script

OpenVZ とか VServer とか使うんかいな? というのもありますね.で,lxc って判断がないなと思ったのですが,これは lxc-start のコードを見たらわかりました.container という環境変数に "lxc" という値を設定しているからですね.

src/lxc/lxc_start.c の main 関数で,以下のような処理があります.

        if (putenv("container=lxc")) {
                SYSERROR("failed to set environment variable");
                return err;
        }