ようへいの日々精進XP

よかろうもん

(ショロカレ 22 日目)LXC を一般ユーザー権限で利用する超メモ

「初老丸の独り Advent calendar 2015」の二十二日目の記事です。

tl;dr

test-kitchen の Driver の一つ kitchen-lxc を動かしてみたくなって、まずは以下の記事を参考に LXC を一般ユーザー権限で動かしてみた。

gihyo.jp

上記の記事は LXC を一般ユーザー権限で動かす為の基礎知識等を含めて詳しく記載されているので、上記の記事だけで簡単に動かすことが出来た。


memo

注意

すいません...上記の記事を参考に写経しているだけなので詳しい用語等については追記していく予定。

ホスト環境

vagrant@vagrant-ubuntu-trusty-64:~$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.2 LTS"

VirtualBox で起動した Ubuntu 14.04 を利用する。

サブ ID の確認

サブ ID とは...以下を参考に。

以下の通り、Ubuntu の場合には確認するだけ。

$ cat /etc/subuid /etc/subgid                                                                                            
vagrant:100000:65536
ubuntu:165536:65536
consul:231072:65536
vagrant:100000:65536
ubuntu:165536:65536
consul:231072:65536

ネットワークインターフェースの設定

仮想インターフェース(veth)を操作することが出来るコマンドを確認。

$ ls -l /usr/lib/x86_64-linux-gnu/lxc/lxc-user-nic
-rwsr-xr-x 1 root root 34888 Nov  4 00:39 /usr/lib/x86_64-linux-gnu/lxc/lxc-user-nic

ユーザーが作成できるネットワークインターフェースの数や作成したインターフェースをどのホストにブリッジさせるかを指定できるファイルを以下のように作成した。

$ cat /etc/lxc/lxc-usernet
# USERNAME TYPE BRIDGE COUNT
vagrant veth lxcbr0 10

LXC 設定ファイルを配置するディレクトリ作成

$ mkdir -p ~/.config/lxc

LXC コンテナのデフォルト設定ファイルをコピーする

$ cp /etc/lxc/default.conf ~/.config/lxc/default.conf

ファイルの中身を確認。

$ cat  ~/.config/lxc/default.conf
lxc.network.type = veth
lxc.network.link = lxcbr0
lxc.network.flags = up
lxc.network.hwaddr = 00:16:3e:xx:xx:xx
#
lxc.id_map = u 0 100000 65536
lxc.id_map = g 0 100000 65536

コンテナ作成

$ lxc-create -n oreno-lxc-centos -t download -- -d centos
Setting up the GPG keyring
Downloading the image index

---
DIST    RELEASE ARCH    VARIANT BUILD
---
centos  6       amd64   default 20151222_02:16
centos  6       i386    default 20151222_02:16
---

Release: 6
Architecture: amd64

Downloading the image index
Downloading the rootfs
Downloading the metadata
The image cache is now ready
Unpacking the rootfs

---
You just created a CentOS container (release=6, arch=amd64, variant=default)

To enable sshd, run: yum install openssh-server

For security reason, container images ship without user accounts
and without a root password.

Use lxc-attach or chroot directly into the rootfs to set a root password
or create user accounts.

上記の lxc-create オプションは以下の通り。

  • -n コンテナ名
  • -t テンプレートを指定(一般ユーザーの場合には download を利用する)
  • -- テンプレートオプションのオプションを指定する際に利用
  • -d テンプレートのディストリビューションを指定

今回は指定していないが、テンプレートオプションには以下のオプションも利用可能。

コンテナスタート

コンテナは作成済み。念のために確認。

$ lxc-ls --fancy
NAME              STATE    IPV4       IPV6  AUTOSTART
-----------------------------------------------------
oreno-lxc-centos  STOPPED  -          -     NO

コンテナスタート。

$ lxc-start --name oreno-lxc-centos --daemon
$ lxc-ls --fancy
NAME              STATE    IPV4        IPV6  AUTOSTART
------------------------------------------------------
oreno-lxc-centos  RUNNING  10.0.3.227  -     NO

コンテナにログイン

コンテナの root パスワードは設定されておらず、ユーザーも作成されていないので lxc-attach でコンテナにアクセスして root パスワードの設定、またはユーザーを作成する。

$ lxc-attach --name oreno-lxc-centos
bash-4.1#
bash-4.1# passwd root
Changing password for user root.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

一旦、exit して lxc-console でログインする。

$ lxc-console --name oreno-lxc-centos

Connected to tty 1
Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself


CentOS release 6.7 (Final)
Kernel 3.13.0-55-generic on an x86_64

oreno-lxc-centos login: root
Password:
Last login: Tue Dec 22 08:25:42 on tty1
[root@oreno-lxc-centos ~]#

コンソールから抜ける場合には Ctrl+a q を押下する。

Apache をインストールしてみる

Apache をインストールしてコンテナホストからインストールしてみる。

$ lxc-console --name oreno-lxc-centos
Connected to tty 1
Type <Ctrl+a q> to exit the console, <Ctrl+a Ctrl+a> to enter Ctrl+a itself


CentOS release 6.7 (Final)
Kernel 3.13.0-55-generic on an x86_64

oreno-lxc-centos login: root
Password:
Last login: Tue Dec 22 08:28:00 on tty1
[root@oreno-lxc-centos ~]# yum -y install httpd

(snip)

[root@oreno-lxc-centos ~]# echo "hello LXC" > /var/www/html/index.html
[root@oreno-lxc-centos ~]# service httpd start
                                                           [  OK  ]
[root@oreno-lxc-centos ~]#

# Ctrl+a q コンテナを抜ける

$ curl 10.0.3.227
hello LXC

おっけ。


以上

LXC を一般ユーザーでコンテナを作成して起動するメモでした。参考にさせていただいた記事だけでここまで出来た。嬉しい。

次は kitchen-lxc を動かしてみたいと企んでいる。