Installation
Install lxc
, bridge-utils
, netctl
from the official repository.
$ sudo pacman -S lxc bridge-utils netctl
For creating ArchLinux Container, install arch-install-scripts:
$ sudo pacman -S arch-install-scripts
Check Configurations:
$ lxc-checkconfig
Network Configuration
Since I use systemd for bridged network, so this step remains blank, the official Arch Wiki use netctl.
Creating Container Using Template
List all of the available templates:
$ 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
Now using the template for creating the Linux Container:
$ sudo lxc-create -n Arch_Container -t /usr/share/lxc/templates/lxc-archlinux
If the container is created successfully, it will indicates that you could use it from now.
Start the Container
List the installed Containers:
$ sudo lxc-ls
Arch_Container
Start the installed Container:
$ sudo lxc-start -n Arch_Container
The default username is root, and you will got a terminal window, now check the network configuration:
[root@Arch_Container ~]# ip link show dev eth0
5: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
Simply use dhcpcd
we could get the ip address currently, and surely we can see this network configuration is OK now.
Enable the automatically dhcpcd at startup
$ dhcpcd
$ systemctl enable dhcpcd.service
Now reboot you will notice the ip address has been configurated automatically.
LXC常用命令
1. lxc-checkconfig 用于判断Linux内核是否支持LXC
2. lxc-create 用于创建一个容器
lxc-create –n name [ –f config_file ]
name 创建的容器名称
config_file 容器配制文件的路径
备注: 容器名称是全局的,也就是说,系统不允许存在重名的容器。lxc-create创建的容器,在停止运行后,不会自动销毁。
3. lxc-destroy 用于销毁一个容器
4. lxc-execute 用于在一个容器执行应用程序
lxc-execute –n name [ –f config_file] [ –s KEY=VAL ] command
name 容器名称
config_file 容器配制文件路径
-s 后面跟配制键值对 例如 lxc.cgroup.cpu.shares = 512
command 待执行的命令
备注:如果不存在name容器,lxc-execute会自动创建一个,容器停止运行后会自动销毁。
执行过程中,有限选用 –f 指定的配置文件参数。 -s选项可以特别改变配置文件中的某一项参数值。
lxc-execute 先创建lxc-init进程,然后在lxc-init中fork一个进行来执行命令,常用于在容器中执行应用程序。
5. lxc-start 用于在容器中执行给定命令
lxc-start –n name [-f config_file ] [-c console_file ] [-d] [-s KEY=VAL] [command]
-d : 将容器当做守护进程执行
-c : 指定一个文件作为容器的控制台输出,默认为终端
备注:如果容器还不存在,lxc-start会自动创建一个,容器停止允许后会自动销毁。它是直接创建进程来运行命令。
lxc-start 用于在容器中启动system
6. lxc-kill 用于发送信号给容器中的第一个用户进程(容器内部进程号为1的为init,进程号为2的为第一个用户进程),默认信号为SIGKILL。
lxc-kill –n name [SIGNUM]
7. lxc-stop 用于停止容器中所有的进程
lxc-stop –n name
8. lxc-destroy 用于销毁容器
lxc-destroy –n name
9. 其他命令,等到需要的时候再补充。
使用上述命令,需要root权限来执行。
在成成的虚拟机容器中,有config配置文件,可以在里面添加针对这个虚拟机的各种资源限制范围。