在CentOS上,使用lxc运行CentOS容器

将lxc-1.0.5安装到CentOS中。

LXC – Linux 容器

从spec文件中生成RPM并进行安装

$ curl -O https://linuxcontainers.org/downloads/lxc-1.0.5.tar.gz
$ tar zxvf lxc-1.0.5.tar.gz
$ mkdir -p ~/src/rpm/SPECS ~/src/rpm/SOURCES
$ cp lxc-1.0.5/lxc.spec ~/src/rpm/SPECS
$ cp lxc-1.0.5.tar.gz ~/src/rpm/SOURCES
$ rpmbuild -bb ~/src/rpm/SPECS/lxc.spec
$ cd  ~/src/rpm/RPMS/x86_64
$ sudo rpm -ivh lxc-1.0.5-1.el6.x86_64.rpm lxc-libs-1.0.5-1.el6.x86_64.rpm

还需要libvirt、docbook2X和cgconfig。

使用模板创建Linux容器

以下是模板。

$ ls /usr/share/lxc/templates
lxc-alpine     lxc-centos    lxc-fedora        lxc-oracle  lxc-ubuntu-cloud
lxc-altlinux   lxc-cirros    lxc-gentoo        lxc-plamo
lxc-archlinux  lxc-debian    lxc-openmandriva  lxc-sshd
lxc-busybox    lxc-download  lxc-opensuse      lxc-ubuntu

Busybox是一个具有最简单的Linux容器功能的工具,sshd是一个带有SSH守护程序的Linux容器。

$ sudo lxc-create -t centos -n centos-test1
$ sudo lxc-start -n centos-test1 -d
lxc-start: Executing '/sbin/init' with no configuration file may crash the host

请指定 -f /var/lib/lxc/centos-test1/config,因为会导致错误提示出现。

$ sudo lxc-start -f /var/lib/lxc/centos-test1/config -n centos-test1 -d
lxc_conf - failed to attach 'vethPX7X32' to the bridge 'virbr0' : No such device

再次出错了。不存在虚拟桥接。更改设置后重启。

$ sudo vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
$ sudo shutdown -r now

当您再次执行时会出现提示符。
从第二次开始似乎不需要-f选项。
密码被定义为/var/lib/lxc/centos-test1/tmp_root_pass,
但在第一次执行时需要进行更改。

$ sudo lxc-start -f /var/lib/lxc/centos-test1/config -n centos-test1 -d
$ sudo lxc-console -n centos-test1
CentOS release 6.5 (Final)
Kernel 2.6.32-431.20.3.el6.x86_64 on an x86_64

centos-test1 login: root
Password:
Last login: Wed Aug  6 01:40:45 on lxc/console
[root@centos-test1 ~]#

要停止的话,可以在虚拟容器中执行 shutdown 命令,或者使用 lxc-stop 命令来停止。