LXC是Linux containers的簡(jiǎn)稱,操作系統(tǒng)級(jí)別的虛擬化技術(shù)。它可以在操作系統(tǒng)層次上為進(jìn)程提供的虛擬的執(zhí)行環(huán)境疑俭。一個(gè)虛擬的執(zhí)行環(huán)境被稱為一個(gè)容器(container)遥巴。可以為容器綁定特定的cpu和memory節(jié)點(diǎn)悴务,分配特定比例的cpu時(shí)間睹限、IO時(shí)間譬猫,限制可以使用的內(nèi)存大小(包括內(nèi)存和是swap空間)羡疗,提供device訪問控制染服,提供獨(dú)立的namespace(網(wǎng)絡(luò)、pid叨恨、ipc柳刮、mnt、uts)痒钝。
如果你了解Docker, 很可能聽說過LXC.
安裝
sudo apt-get install lxc
查看系統(tǒng)模板
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
創(chuàng)建系統(tǒng)
要花費(fèi)很多時(shí)間
sudo lxc-create -t ubuntu -n zqw
# -n 是 name 縮寫
啟動(dòng)
sudo lxc-start -n zqw -d
顯示lxc創(chuàng)建的系統(tǒng)
sudo lxc-ls
通過控制臺(tái)進(jìn)入系統(tǒng)
sudo lxc-console -n zqw
, 就可以在系統(tǒng)內(nèi)執(zhí)行shell啦
停止
lxc-stop -n zqw
通過配置文件進(jìn)入系統(tǒng)
# 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/