LXC触ってみた

本当にチョンって触ったようなレベル。期待してはいけない。

Dockerのお勉強の一環ってことで、LXCを触ってみた。OSレベルの仮想化だとか


CentOS 6.2です。

[root@localhost ~]# uname -a
Linux localhost.localdomain 2.6.32-358.14.1.el6.x86_64 #1 SMP Tue Jul 16 23:51:20 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
# 必要なものたち
# rsyncはlxc-createしたときにrsyncが無いと怒るので。
# lxc-createしたときに勝手にupdateする時に入れてくれれば良いのに・・・
# libcap-devel は最後のトラブルシューティング参照

yum install gcc make rsync libcap-devel

LXCのソースコードをfree get & build。

cd /usr/local/src
curl -O http://linuxcontainers.org/downloads/lxc-1.0.0.tar.gz
tar xzvf lxc-1.0.0.tar.gz
cd lxc-1.0.0

# prefixぐらいは指定したほうが良いかも。デフォルトで/usr/localだった。
./configure
make && make install

ライブラリパスを通しておく

echo "/usr/local/lib" >  /etc/ld.so.conf.d/lxc.conf
ldconfig

使いたいテンプレートを探す。

ls -l /usr/local/share/lxc/templates

今回はcentosなのでlxc-centosを使う

lxc-create --name=LXC-Study --template=/usr/local/share/lxc/templates/lxc-centos

# テンプレートは省略して以下でも良い
lxc-create --name=LXC-Study --template=centos

んで、勝手に必要なパッケージを勝手に入れ始める。(rsync以外。なんでや。)

...

Update finished
Copy /usr/local/var/cache/lxc/centos/x86_64/6/rootfs to /usr/local/var/lib/lxc/LXC-Study/rootfs ...
Copying rootfs to /usr/local/var/lib/lxc/LXC-Study/rootfs ...
Storing root password in '/usr/local/var/lib/lxc/LXC-Study/tmp_root_pass'
Expiring password for user root.
passwd: 成功

Container rootfs and config have been created.
Edit the config file to check/enable networking setup.

The temporary root password is stored in:

        '/usr/local/var/lib/lxc/LXC-Study/tmp_root_pass'


The root password is set up as expired and will require it to be changed
at first login, which you should do as soon as possible.  If you lose the
root password or wish to change it without starting the container, you
can change it from the host by running the following command (which will
also reset the expired flag):

        chroot /usr/local/var/lib/lxc/LXC-Study/rootfs passwd

一時パスワードは '/usr/local/var/lib/lxc/LXC-Study/tmp_root_pass' に払い出したけど、
さっさと、`chroot /usr/local/var/lib/lxc/LXC-Study/rootfs passwd` しろよ、とのこと。
多分。

パスワードは変えておきましょう。

chroot /usr/local/var/lib/lxc/LXC-Study/rootfs passwd

lxc-startまであと少し。

# 適当なブリッジ接続用のデバイスを立てる。centosテンプレートだとこのデバイス名?
# ちゃんと設定してないので、netに繋がらない。ちゃんと設定しましょう。
brctl addbr virbr0

# cgroupの情報を出す先をマウント
cp -p /etc/fstab /etc/fstab.bak
echo "cgroup /cgroup cgroup defaults 0 0" >> /etc/fstab
mkdir /cgroup
mount cgroup -t cgroup /cgroup

lxc-start!!

lxc-start --name=LXC-Study

終了する時はshutdown -h nowしたら戻れた。・・・違うんだよ、デタッチ的なことしたいんだよ・・・

そんなこんなで失敗作が出来たりしたら、lxc-destroy で削除する。

lxc-destroy --name=LXC-Study


トラブルシューティング

[root@localhost ~]# lxc-start --name=LXC-Study
lxc-start: unknown capability mac_admin
lxc-start: failed to drop capabilities
lxc-start: failed to setup the container
lxc-start: invalid sequence number 1. expected 2
lxc-start: failed to spawn 'LXC-Study'

こんなエラーが出たら、以下を入れないとダメ。
LXCがlinux-capabilityが無効になったままビルドしてしまうから。

yum install libcap-devel

その後、LXCをリビルド・インストール

感想

lxc-createしてからlxc-startできるまでの時間が結構短い。VMをCloneするより全然早い。

centos環境がかなり手軽に作れるので良い感じ。でももう少し使いこなしたい。