Linux Kernel の User Namespace 機能と lxc (1)
色々な Namespace の機能はコンテナを動かす上で重要な機能なのですが,Linux kernel でこれから実装される予定の機能がいくつかあります.そのうちの結構重要なのが User Namespace です.
今の lxc コンテナは,起動するとコンテナ内の root (uid:0) とホスト上の root が同じ権限を持つユーザなので,コンテナからホストに対して色々出来るとか,セキュリティ的にマズい事ができてしまう可能性があります.これを,コンテナ内では普通の root でありながら,ホスト上で見ると 0 以外の uid を持つユーザに見えるというような機能が User Namespace です (たぶん ^^;).
この辺りの詳細は以下をご参照ください.
さて,この辺りは以前から Ubuntu で簡単に試せるよ、という話があったのですが (上記 URI もそう) 全く試したことはありませんでしたが,最近
なんてブログエントリが出てきて,すげー簡単やん,ということで試してみました.
手順は書いてあるとおりで,Ubuntu 12.10 を入れて (12.04 はパッケージがありませんでした...)
sudo add-apt-repository ppa:serge-hallyn/userns-natty sudo apt-get update sudo apt-get dist-upgrade sudo apt-get install linux-image-3.7.0-0-generic nsexec lxc sudo lxc-create -t ubuntu -n test01 sudo container-userns-convert test01 100000 sudo reboot
(reboot いるん?)
この後,以下のように起動させてみます.
sudo lxc-start -n test01 -d sudo lxc-console -n test01
が... コンテナ内の init は起動するものの,その途中で止まってるっぽく,ログインプロンプトも出てきませんし,ssh サーバも起動していません.-d を付けずに起動してみると
<4>init: hwclock main process (4) terminated with status 77 <4>init: ureadahead main process (5) terminated with status 5 <4>init: mounted-proc main process (11) terminated with status 1 mount: permission denied mountall: mount /sys/fs/fuse/connections [23] terminated with status 32 mountall: Filesystem could not be mounted: /sys/fs/fuse/connections mount: permission denied mountall: mount /sys/kernel/debug [24] terminated with status 32 mountall: Filesystem could not be mounted: /sys/kernel/debug mount: permission denied mountall: mount /sys/kernel/security [25] terminated with status 32 mountall: Filesystem could not be mounted: /sys/kernel/security mountall: Event failed <4>init: udev-fallback-graphics main process (43) terminated with status 1 <4>init: console-font main process (54) terminated with status 1 <4>init: setvtrgb main process (64) terminated with status 1
色々エラーは出てますが,当然のエラーもあったりしますので,これだけでは何が悪いかすぐにはわかりません.init は実行出来ているのでとりあえずコンテナは出来上がって実行されていますが,Ubuntu 環境としては起動しないなあ.と,これだけではナンですので,色々観察してみました.
- container-userns-convert を実行すると,コンテナのルート以下のファイルも uid/gid の書き換えがなされたりしています.
root@ubuntu:~# ls -l /var/lib/lxc/test01/rootfs
合計 80
drwxr-xr-x 2 100000 100000 4096 11月 2 17:13 bin
drwxr-xr-x 2 100000 100000 4096 10月 10 00:03 boot
drwxr-xr-x 4 100000 100000 4096 11月 2 18:13 dev
drwxr-xr-x 63 100000 100000 4096 11月 2 18:13 etc
drwxr-xr-x 3 100000 100000 4096 11月 2 17:17 home
(以下略) - User Namespace 対応カーネルで起動すると,/proc/$PID/ 以下に uid_map, gid_map というファイルが出来ています.コンテナ内の init のものを見てみると,こんな感じです.
root@ubuntu:/proc/1# cat /proc/3470/uid_map
0 100000 10000
root@ubuntu:/proc/1# cat /proc/3470/gid_map
0 100000 10000 - この init もホストから見ると指定した uid で起動しています.
root 3466 0.0 0.1 27544 1264 pts/0 S+ 18:13 0:00 lxc-start -n test01
100000 3470 0.0 0.1 24072 1912 pts/6 Ss+ 18:13 0:00 /sbin/init
root 3472 0.0 0.0 21408 496 ? S 18:13 0:00 /sbin/udevd --daemon
root 3473 0.0 0.0 21408 496 ? S 18:13 0:00 /sbin/udevd --daemon
100000 3498 0.0 0.1 49452 1336 ? S 18:13 0:00 /sbin/plymouthd --mode=boot --attach-to-session
100000 3506 0.0 0.1 23708 1236 ? S 18:13 0:00 mountall --daemon
100000 3584 0.0 0.0 15188 388 ? S 18:13 0:00 upstart-socket-bridge --daemon
あとはコンテナ内から色々見たいなと思ったのですが,こんな状況なので... (つづく)