2010年8月11日水曜日

[memo] LXC - Linux Containers on GentooLinux

GentooLinux上でLXCを使うときのメモ。

Kernel Config
* General
 * Control Group support                                                                                                                                                 
  -> namespace cgroup subsystem
  -> cpuset support
  -> Group CPU scheduler
  -> control group freeze subsystem
  -> Basis for grouping tasks (Control Groups)
  -> Simple CPU accounting
  -> Resource counters
  -> Memory resource controllers for Control Groups
  -> Namespace support
    -> UTS namespace
    -> IPC namespace
    -> User namespace
    -> Pid namespace
    -> Network namespace

cgroupのマウント
$ sudo mkdir /cgroup
$ sudo mount -t cgroup cgroup /cgroup
$ sudo vim /etc/fstab
cgroup                     /cgroup        cgroup           default     0 0



app-emulation/lxc
2010/08/11現在 mask されている。
- app-emulation/lxc-0.7.1 (masked by: package.mask)
/usr/portage/profiles/package.mask:
# Diego E. Pettenò (23 Oct 2009)
#
# Starting work toward supporting Linux Containers in Gentoo.
# Currently, it's a tentative ebuild based upon Tiziano Müller
# (dev-zero)'s overlay, with some differences from the upstream paths
# and handling.
#
# Will be unmasked when felt “ready” (and openrc'll support it as
# guest).

ので、unmask 。
$ echo app-emulation/lxc | sudo tee -a /etc/portage/package.unmask
$ sudo emerge -av lxc


ゲストの作成
ゲストは GentooLinux とする。
Debian を使いたい場合は、debootstrap とか。

HOST# export TARGET_DIR=/var/lxc/rootfs.gentoo
HOST# cd ${TARGET_DIR}
HOST# tar -xjpf stage3-*.tar.bz2
HOST# mkdir usr/portage
HOST# mount -o bind /usr/portage ${TARGET_DIR}/usr/portage
HOST# chroot ${TARGET_DIR} /bin/bash
# env-update
# source /etc/profile
GUEST# PS1="GUEST# "

LXCゲストは baselayout-2 で対応しているらしい。
GUEST# mkdir /etc/portage
GUEST# echo sys-apps/baselayout >> /etc/portage/package.keywords
GUEST# echo sys-apps/openrc >> /etc/portage/package.keywords
GUEST# emerge -u baselayout
GUEST# dispatch-conf

コンテナ用のデバイスファイルを作成します。tarball から展開した /dev は一度削除。
GUEST# rm -rf /dev
GUEST# mknod /dev/urandom c   1  9 
GUEST# mknod /dev/tty1    c 136  1 
GUEST# mknod /dev/tty2    c 136  4 
GUEST# mknod /dev/tty3    c 136  5 
GUEST# mknod /dev/tty4    c 136  6 
GUEST# mknod /dev/tty5    c   4  5 
GUEST# mknod /dev/tty6    c   4  6 
GUEST# mknod /dev/tty7    c   4  7 
GUEST# mknod /dev/tty8    c   4  8 
GUEST# mknod /dev/tty9    c   4  9 
GUEST# mknod /dev/tty10   c   4 10 
GUEST# mknod /dev/tty11   c   4 11 
GUEST# mknod /dev/tty12   c   4 12 
GUEST# mknod /dev/mem     c   1  1 
GUEST# mknod /dev/kmem    c   1  2 
GUEST# mknod /dev/port    c   1  4 
GUEST# mknod /dev/tty     c   5  0 
GUEST# mknod /dev/psaux   c  10  1 
GUEST# mknod /dev/null    c   1  3 
GUEST# mknod /dev/zero    c   1  5 
GUEST# mknod /dev/full    c   1  7 
GUEST# mknod /dev/random  c   1  8 
GUEST# mkdir /dev/pts
GUEST# mknod /dev/pts/ptmx c 5 2
GUEST# ln -s /dev/pts/ptmx /dev/ptmx 
GUEST# mkdir /dev/shm
GUEST# touch /dev/.keep

IPアドレスは、ゲスト内で設定しなくてもLXCが設定してくれるが、 ゲスト内で設定しても良いかもしれない(未確認)。
ルートの設定はしてくれないので、ゲスト内の設定を書く。

GUEST# nano -w /etc/conf.d/net
config_eth0=( "noop" )
routes_eth0=( "default via 192.168.0.1"
              "default via fe80::1" )

GUEST# rc-update add net.eth0 default

その他、resolv.conf や localtime 等はご自由に。
次に、inittab をいじる。TERMINAL以下をコメントアウトして、1行挿入する。

GUEST# nano -w /etc/inittab
# TERMINALS
c0:12345:respawn:/sbin/agetty 38400 console
c1:12345:respawn:/sbin/agetty 38400 tty1 linux
#c2:2345:respawn:/sbin/agetty 38400 tty2 linux
#c3:2345:respawn:/sbin/agetty 38400 tty3 linux
#c4:2345:respawn:/sbin/agetty 38400 tty4 linux
#c5:2345:respawn:/sbin/agetty 38400 tty5 linux
#c6:2345:respawn:/sbin/agetty 38400 tty6 linux

ここで一端ホスト側に戻り、LXCのゲスト設定を作る。
ゲストの名前は "gentoo" とする。

HOST# lxc-create -n gentoo
HOST# vim /etc/lxc/gentoo/config
lxc.utsname = gentoo
lxc.tty = 4
lxc.pts = 1024
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = lan0
lxc.network.hwaddr = 02:00:00:01:01:01
lxc.network.ipv4 = 192.168.0.101/24
lxc.network.ipv6 = 2001:db8:1::101/64
lxc.network.name = eth0
lxc.mount = /etc/lxc/gentoo/fstab
lxc.rootfs = /var/lxc/rootfs.gentoo

ゲスト起動前にホスト側でマウントするところは lxc.mount で指定したファイルに記述する。

HOST# vim /etc/lxc/gentoo/fstab
none /var/lxc/rootfs.gentoo/proc proc defaults 0 0
/usr/portage /var/lxc/rootfs.gentoo/usr/portage none bind,ro 0 0
none /var/lxc/rootfs.gentoo/usr/portage/distfiles tmpfs defaults 0 0

起動してみる。
HOST# lxc-start -n gentoo
INIT: version 2.87 booting
Loading /lib64/rc/console/keymap

  OpenRC 0.6.1 is starting up Gentoo Linux (x86_64) [LXC]

Press I to enter interactive boot mode

* /proc is already mounted, skipping
* Caching service dependencies ...
[ ok ]
* Mounting /dev/pts ...
[ ok ]
* Mounting /dev/shm ...
[ ok ]
* Setting hostname to lxc-gentoo ...
[ ok ]
* Configuring kernel parameters ...
[ ok ]
* Creating user login records ...
[ ok ]
* Cleaning /var/run ...
[ ok ]
* Wiping /tmp directory ...
[ ok ]
* Setting terminal encoding [UTF-8] ...
[ ok ]
* Setting keyboard mode [UTF-8] ...
[ ok ]
* Loading key mappings [us] ...
[ ok ]
* Updating /etc/mtab ...
[ ok ]
* Bringing up interface lo
*   Caching network module dependencies
*   127.0.0.1/8 ...
[ ok ]
*   Adding routes
*     127.0.0.0/8 via 127.0.0.1 ...
[ ok ]
* Initializing random number generator ...
[ ok ]
INIT: Entering runlevel: 3
* Mounting network filesystems ...
[ ok ]
* Starting local ...
[ ok ]


This is gentoo.unknown_domain (Linux x86_64 2.6.34-gentoo) 13:37:01

gentoo login:  root
Password: 
Last login: Wed Aug 11 13:37:59 JST 2010 on console
gentoo ~ # ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   3888   620 ?        Ss   13:39   0:00 init [3]  
root       387  0.2  0.0  56040  1468 console  Ss   13:39   0:00 /bin/login -- 
root       388  0.5  0.0  17728  1840 console  S    13:40   0:00 -bash
root       392  0.0  0.0  14804   980 console  R+   13:40   0:00 ps aux
gentoo ~ #
2010/10/06: 手順の漏れを修正

1 件のコメント:

  1. You’d be forgiven, however you’d be incorrect, for considering that the sorts of|several varieties of|various kinds of} roulette — European, American, and French — are created the identical. The American roulette is totally different by design, gameplay, and format from its European or French counterparts, however they do share some similarities. The shaft is the essential element that offers support to the rotor of the wheel. If the wheel isn’t maintained appropriately, the shaft is most likely to slant or bend, creating a bias. What that means is that the wheel rotor will revolve 1xbet on a bent angle, implying that the ball with land on the decrease side of the slant.

    返信削除