2012年12月29日土曜日

GentooのLXC(Linux Container)をやってみた

LXCをインストールする

Solaris Zoneは結構お世話になっているコンテナベースの仮想化ですが最近のLinux界隈のコンテナ仮想化はOpenVZ以来ご無沙汰だったので、LXC(Container-based virtualization)を試してみました。

とりあえず色々細かい設定は置いといてコンテナを起動するまでを目標にインストールしてみる。

カーネルをコンテナ用にリビルドする

インストールカーネル
  • Linux karky7 3.6.5-gentoo #11 SMP x86_64 Intel(R) Core(TM) i7-2620M CPU @ 2.70GHz GenuineIntel GNU/Linux
cgroup関連を組み込む
General setup  --->
  --- Control Group support
  [*]   Example debug cgroup subsystem
  [*]   Freezer cgroup subsystem
  [*]   Device controller for cgroups
  [*]   Cpuset support
  [*]     Include legacy /proc/<pid>/cpuset file
  [*]   Simple CPU accounting cgroup subsystem
  [*]   Resource counters
  [*]     Memory Resource Controller for Control Groups
  [*]       Memory Resource Controller Swap Extension
  [*]         Memory Resource Controller Swap Extension enabled by default (NEW)
  [*]       Memory Resource Controller Kernel Memory accounting (EXPERIMENTAL)
  [*]   Enable perf_event per-cpu per-container group (cgroup) monitoring
  -*-   Group CPU scheduler  --->
  [*]   Block IO controller
  [*]     Enable Block IO controller debugging
  --- Group CPU scheduler
  -*-   Group scheduling for SCHED_OTHER
  [*]     CPU bandwidth provisioning for FAIR_GROUP_SCHED
  [*]   Group scheduling for SCHED_RR/FIFO
  [*] Namespaces support  --->
  --- Namespaces support
    [*]   UTS namespace (NEW)
    [*]   IPC namespace (NEW)
    [*]   PID Namespaces (NEW)
    [*]   Network namespace (NEW)
Networking support
  Networking options  --->
    <M> 802.1d Ethernet Bridging
    <*> 802.1Q VLAN Support
Device Drivers  --->
  Character devices  --->
    [*] Unix98 PTY support
    [*]   Support multiple instances of devpts
  
  -*- Network device support  --->
     <M>     MAC-VLAN support (EXPERIMENTAL)
     <M>     Virtual ethernet pair device
karky7 linux # make && make modules_install && make install
karky7 linux # reboot

LXCパッケージをインストール

マスクパッケージなのでマスクを外す
karky7 ~ # emerge -s lxc
Searching...    
[ Results for search key : lxc ]
[ Applications found : 2 ]

*  app-emulation/lxc [ Masked ]
      Latest version available: 0.8.0-r1
      Latest version installed: [ Not Installed ]
      Size of files: 305 kB
      Homepage:      http://lxc.sourceforge.net/
      Description:   LinuX Containers userspace utilities
      License:       LGPL-3
karky7 ~ # cat /etc/portage/package.keywords | grep lxc
app-emulation/lxc ~amd64
karky7 ~ # emerge -pv lxc

These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild  N     ] virtual/awk-0  0 kB
[ebuild     U  ] sys-kernel/linux-headers-3.6 [2.6.39] 5,145 kB
[ebuild  N    ~] app-emulation/lxc-0.8.0-r1  USE="-examples" 306 kB
karky7 ~ # emerge lxc
/sys/fs/cgroupがマウントされていない場合、fstabにcgroupをマウントする設定を書いておく
cgroup       /sys/fs/cgroup  cgroup   rw       0 0
karky7 ~ # mount /sys/fs/cgroup

ホスト側へネットワークパッケージをインストールする

Bridge Utilsのインストール
karky7 ~ # emerge net-misc/bridge-utils
Bridgeの設定
karky7 ~ # cat /etc/conf.d/net
bridge_br0="eth0"
bridge_add_eth0="br0"
brctl_br0="setfd 0 sethello 0 stp off"

config_br0="192.168.xx.x/24"
routes_br0="default via 192.168.xx.x"

config_eth0="null"

lxc-gentooでgentooコンテナを作成

ゲスト側のOSのインストールもgentooで行う、(fedoraとかすんなり出来なかっただけの話ですが...笑)
Gentooならコマンド2発位で何とかなった

lcx-gentooのインストール

これは、ネットワークからgentooのstage3あたりのアーカイブを自動的にダウンロードしキャッシュまでやってくれる。キャッシュデータは/var/cache/lxcあたりへ展開される。 lxc-gentooプロジェクトからチェックアウトする
karky7 opt # cd /opt/
karky7 opt # git clone git clone git://github.com/globalcitizen/lxc-gentoo
好みでパスを通してください

Guest OSを作成する

先ほどチェックアウトしたlxc-gentooディレクトリにあるlxc-gentooコマンドを実行し必要なデータを先にcacheさせる。初回の実行時はキャッシュをダウンロードするためチョット時間がかかります。
以下の必要な入力を設定して終わり

  • コンテナ名
  • ホスト名
  • ネットワーク
  • アーキテクチャ(gentoo固有)
  • rootのパスワード

karky7 opt # cd /opt/lxc-gentoo
karky7 lxc-gentoo # ./lxc-gentoo create     
What is the name for the container (recommended &lt;=15 chars)? [gentoo]gentoo1
What hostname do you wish for this container ? [gentoo]gentoo1
What IP address do you wish for this container (enter 'dhcp' to use DHCP) ? [172.20.0.2/24]dhcp
Which gentoo architecture should we use in this container ? [amd64]
Architecture variant (usually '', 'hardened' or 'hardened+nomultilib') ? []
Type guest root password (enter for none):

Thanks! Now sit back and relax while your gentoo brews...


Checking for pre-existing cache in /var/cache/lxc/gentoo/amd64_amd64__rootfs...not found.
Determining path to latest gentoo amd64 stage3 archive...20121210/stage3-amd64-nomultilib-20121210.tar.bz2
Downloading (~130MB), please wait...complete.
Extracting stage3 archive...done.
Downloading gentoo portage (software database) snapshot...done.
Extracting...done.
Copying filesystem...done.
 - writing LXC guest configuration...done.
 - setting hostname...done.
 - setting guest root password..done.
All done!

You can run your container with the 'lxc-start -f gentoo1.conf -n gentoo1'
 - For host-side networking setup info, see ./gentoo1/etc/conf.d/net

Note that container startup will show some errors, these do not matter.

To enter your container for setup WITHOUT running it, try:
 # mount -t proc proc ./gentoo1/proc
 # mount -o bind /dev ./gentoo1/dev
 # chroot ./gentoo1 /bin/bash
 # export PS1=&quot;(gentoo1) $PS1&quot;
 (gentoo1) #   &lt;-- you are now in the guest
... or, start it on the current console with:
    'lxc-start -f gentoo1.conf -n gentoo1'
... or, start it elsewhere and use lxc-console -n gentoo1 to get a shell.

(Report bugs to https://github.com/globalcitizen/lxc-gentoo/issues )

コンテナを起動する

lxc-startコマンドで起動する。
その他にlxc-stopなど他のコマンドもありますので調べてください。
karky7 ~ # lxc-start -f /opt/lxc-gentoo/gentoo1.conf -n gentoo1
INIT: version 2.88 booting

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

 * /proc is already mounted
 * /run/openrc: creating directory
 * /run/lock: creating directory
 * /run/lock: correcting owner
 * Caching service dependencies ...
Service `netmount' needs non existent service `net'
Service `pydoc-3.2' needs non existent service `net'
Service `sshd' needs non existent service `net'
 [ ok ]
 * Creating user login records ...
 [ ok ]
 * Cleaning /var/run ...
 [ ok ]
 * Wiping /tmp directory ...
 [ ok ]
 * Updating /etc/mtab ...
 [ ok ]
 * Bringing up interface lo
....
若干エラーが発生してる模様ですが、細かいところは後で調整する予定です。

とりあえず適当です、次回はfedoraとかdebianとかインストールしたり、cgroupでリソースを細かく設定したりしてみたいですな、では

0 件のコメント:

コメントを投稿