φ(.. )メモシテオコウ Fedora20でLXCを使うめも

Fedora20でLXCを使う手順のメモです。

インストールするのはlxcパッケージとlxcで使うOS環境のテンプレートが入っているlxc-templatesパッケージです。

masami@saga:~$ sudo dnf install lxc lxc-templates

テンプレートはこのようにfedoradebianubuntu等が揃ってます。

masami@saga:~$ ls /usr/share/lxc/templates/
.  ..  lxc-alpine  lxc-busybox  lxc-debian  lxc-fedora  lxc-oracle  lxc-sshd  lxc-ubuntu

では、コンテナの作成。ここではfedora環境をfoobarという名前で作ってます。

masami@saga:~$ sudo lxc-create -n foobar -t /usr/share/lxc/templates/lxc-fedora

lxc-create: No config file specified, using the default config /etc/lxc/default.conf
Host CPE ID from /etc/os-release: cpe:/o:fedoraproject:fedora:20
Checking cache download in /var/cache/lxc/fedora/x86_64/20/rootfs ...

~中略~

  util-linux.x86_64 0:2.24-0.1.fc20            xz-libs.x86_64 0:5.1.2-6alpha.fc20         yum-metadata-parser.x86_64 0:1.1.4-9.fc20    zlib.x86_64 0:1.2.8-3.fc20

Complete!
Download complete.
Copy /var/cache/lxc/fedora/x86_64/20/rootfs to /var/lib/lxc/foobar/rootfs ...
Copying rootfs to /var/lib/lxc/foobar/rootfs ...setting root passwd to root
installing fedora-release package
warning: Failed to read auxiliary vector, /proc not mounted?
warning: Failed to read auxiliary vector, /proc not mounted?
warning: Failed to read auxiliary vector, /proc not mounted?
warning: Failed to read auxiliary vector, /proc not mounted?
warning: Failed to read auxiliary vector, /proc not mounted?
warning: Failed to read auxiliary vector, /proc not mounted?
warning: Failed to read auxiliary vector, /proc not mounted?
warning: Failed to read auxiliary vector, /proc not mounted?
Package fedora-release-20-0.7.noarch already installed and latest version
Nothing to do
unlink: cannot unlink ‘/var/lib/lxc/foobar/rootfs/etc/systemd/system/default.target’: No such file or directory
container rootfs and config created
'/usr/share/lxc/templates/lxc-fedora' template installed
'foobar' created

lxc-createによってrootfs環境が作られますが、ここで一番重要と言えるのは[Copying rootfs to /var/lib/lxc/foobar/rootfs ...setting root passwd to root]ですね。rootのパスワードはrootです。

これで環境が作成できたのでlxc-startしてみます。

masami@saga:~$ sudo lxc-start -n foobar
systemd 208 running in system mode. (+PAM +LIBWRAP +AUDIT +SELINUX +IMA +SYSVINIT +LIBCRYPTSETUP +GCRYPT +ACL +XZ)
Detected virtualization 'lxc'.

Welcome to Fedora 20 (Heisenbug)!

Set hostname to <foobar.localdomain>.
Failed to verify GPT partition /dev/dm-1: No such file or directory
/usr/lib/systemd/system-generators/systemd-gpt-auto-generator exited with exit status 1.
[  OK  ] Reached target Remote File Systems.
[  OK  ] Listening on Delayed Shutdown Socket.
~中略~
[  OK  ] Reached target Multi-User System.

Fedora release 20 (Heisenbug)
Kernel 3.11.5-300.fc20.x86_64 on an x86_64 (console)

foobar login: root
Password: 
Last failed login: Thu Oct 17 01:47:30 UTC 2013 on console
There was 1 failed login attempt since the last successful login.

Cannot make/remove an entry for the specified session

Fedora release 20 (Heisenbug)
Kernel 3.11.5-300.fc20.x86_64 on an x86_64 (console)

えー、「Cannot make/remove an entry for the specified session」と言われてログインできません(´;ω;`)ブワッ
これはググったところfedoraのbugzillaで「Bug 966807 - Can't login to systemd lightweight container」というバグがあり、この中で暫定対処として/etc/pam.d/loginのpam_loginuidの行をコメントアウトしたとあったので試したところ上手くいきました。
コンテナのルートファイルシステムは「/var/lib/lxc/foobar/rootfs」ですので「/var/lib/lxc/foobar/rootfs/etc/pam.d/login」を修正すると以下のようになります。

masami@saga:~$ cat /var/lib/lxc/foobar/rootfs/etc/pam.d/login
#%PAM-1.0
auth [user_unknown=ignore success=ok ignore=ignore default=bad] pam_securetty.so
auth       substack     system-auth
auth       include      postlogin
account    required     pam_nologin.so
account    include      system-auth
password   include      system-auth
# pam_selinux.so close should be the first session rule
session    required     pam_selinux.so close
#session    required     pam_loginuid.so
session    optional     pam_console.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session    required     pam_selinux.so open
session    required     pam_namespace.so
session    optional     pam_keyinit.so force revoke
session    include      system-auth
session    include      postlogin
-session   optional     pam_ck_connector.so

そうしたらlxc-stopで一旦止めて、再度lxc-startすると今度は上手くいきます(∩´∀`)∩ワーイ

masami@saga:~$ sudo lxc-start -n foobar
systemd 208 running in system mode. (+PAM +LIBWRAP +AUDIT +SELINUX +IMA +SYSVINIT +LIBCRYPTSETUP +GCRYPT +ACL +XZ)
Detected virtualization 'lxc'.

Welcome to Fedora 20 (Heisenbug)!

Set hostname to <foobar.localdomain>.
Failed to verify GPT partition /dev/dm-1: No such file or directory
/usr/lib/systemd/system-generators/systemd-gpt-auto-generator exited with exit status 1.
[  OK  ] Reached target Remote File Systems.
[  OK  ] Listening on Delayed Shutdown Socket.
[  OK  ] Listening on /dev/initctl Compatibility Named Pipe.
~中略~
[  OK  ] Reached target Multi-User System.

Fedora release 20 (Heisenbug)
Kernel 3.11.5-300.fc20.x86_64 on an x86_64 (console)

foobar login: root
Password: 
Last failed login: Thu Oct 17 01:47:30 UTC 2013 on console
There was 1 failed login attempt since the last successful login.
[root@foobar ~]# ls

selinuxで警告が出たらsealertの指示に従って直せばOKです。sealertのGUIを使うかターミナルから下記コマンドで確認できます。

# sealert -a /var/log/audit/audit.log

追記:
上記の手順は設定ファイルを使っていないのでデフォルトの設定が使われてます。

masami@saga:~$ cat /etc/lxc/default.conf
lxc.network.type = veth
lxc.network.link = virbr0
lxc.network.flags = up

これだとネットワークにつながらないので/etc/lxc/default.confを適当にコピーしてネットワーク設定を追加してからlxc-createするか、/var/lib/lxc/<コンテナ名>/configを修正すればOKです。
追加するのは下記3行で環境に合わせて適当に修正してください。

lxc.network.ipv4 = 192.168.122.1/24
lxc.network.name = eth0
lxc.network.hwaddr = 52:54:00:7e:3a:AA

lxc.network.ipv4はすでにFedoravirt-managerを使って仮想環境を使っているならvirbr0があると思いますので、それを使えば良いかと。
lxc.network.hwaddrは適当に。私は既存の仮想環境で使っているMACアドレスを適当に変えてます。

あとはコンテナのほうで/sbin/ifup eth0すればネットワークにつながるはずです。