简单上手 LXC(Linux Containers)

2018-01-05  本文已影响457人  已不再更新_转移到qiita

LXC是Linux containers的简称,操作系统级别的虚拟化技术。它可以在操作系统层次上为进程提供的虚拟的执行环境。一个虚拟的执行环境被称为一个容器(container)。可以为容器绑定特定的cpu和memory节点,分配特定比例的cpu时间、IO时间,限制可以使用的内存大小(包括内存和是swap空间),提供device访问控制,提供独立的namespace(网络、pid、ipc、mnt、uts)。

如果你了解Docker, 很可能听说过LXC.

安装

sudo apt-get install lxc

查看系统模板

ls /usr/share/lxc/templates/

lxc-alpine     lxc-centos    lxc-fedora        lxc-oracle
lxc-altlinux   lxc-cirros    lxc-gentoo        lxc-plamo
lxc-archlinux  lxc-debian    lxc-openmandriva  lxc-sshd
lxc-busybox    lxc-download  lxc-opensuse      lxc-ubuntu

创建系统

要花费很多时间

sudo lxc-create -t ubuntu -n zqw
# -n 是 name 缩写

启动

sudo lxc-start -n zqw -d

显示lxc创建的系统

sudo lxc-ls

通过控制台进入系统

sudo lxc-console -n zqw, 就可以在系统内执行shell啦

停止

lxc-stop -n zqw

通过配置文件进入系统

# gitian的配置文件
# https://github.com/devrandom/gitian-builder/blob/master/etc/lxc.config.in

lxc.tty = 4
lxc.pts = 1024
lxc.rootfs = ROOTFS
lxc.arch = ARCH
lxc.cgroup.devices.deny = a
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# consoles
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
lxc.cgroup.devices.allow = c 4:0 rwm
lxc.cgroup.devices.allow = c 4:1 rwm
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm

# mounts points
lxc.mount.entry=proc ROOTFS/proc proc nodev,noexec,nosuid 0 0
lxc.mount.entry=sysfs ROOTFS/sys sysfs defaults  0 0

# Container with network virtualized using a pre-configured bridge named br0 and
# veth pair virtual network devices
# On the host, run: ifconfig br0 up 10.0.2.2
# Alternatively, you can use another IP range for the bridge interface, in this case set
# the environment variables GITIAN_HOST_IP and LXC_GUEST_IP appropriately.
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = GUESTLINK
lxc.network.ipv4 = GUESTIP/24
lxc.network.ipv4.gateway = auto

lxc.utsname = gitian
sudo lxc-start -n gitian -f gitian-builder/var/lxc.config
sudo lxc-attach -n gitian -f gitian-builder/var/lxc.config

# -n name缩写
# -f file缩写, 输入配置文件路径

参考:

https://segmentfault.com/a/1190000000443812

https://www.ibm.com/developerworks/cn/linux/l-lxc-containers/

https://knowledge.sakura.ad.jp/2108/ 日文

上一篇下一篇

猜你喜欢

热点阅读