如果不知道什麼是LXC 可以看看 http://www.arthurtoday.com/2012/10/linux-containers-lxc.html#.U74_Wj8vBOK
對我而言就是進階的chroot,只能運作於 Linux 相關的還境上,不像 Virtual Machine 可以弄不同作業系統,但是資源的運用,與系統整合上我覺得比VM更為方便。尤其是指令狂,這真是太好用了!我的工作常要測試不同版本Linux和不同Arch,一直都用VM,試了幾天 Linux Container 發現不錯,最近又解決的 Unprivileged 的問題,整個大爽^^,相關用法底下的 link 都有,我只是把幾個常用的部份紀錄起來如下:
ps: 發現 lxc-start 會有問題,最後是chown /dev/pts/1 出問題,網路上有 patch 了 還沒測試!
Linux distribution and kernel:
Debian Sid with kernel Linux
lxc 3.14-1-amd64 #1 SMP Debian 3.14.10-1 (2014-07-07) x86_64 GNU/Linux
相關版本:
ii lxc 1.1.0~alpha1-1 amd64 Linux Containers userspace tools
ii uidmap 1:4.2-2 amd64 programs to help use subuids
確認有NAMESPACE support
cat /proc/1/{g,u}id_map
grep NAMESPACE /boot/config-3.14-1-amd64
CONFIG_NAMESPACES=y
開始設定一般的 LXC
安裝套件:
aptitude install lxc
aptitude install bridge-utils debootstrap
設定 cgroup 於 /etc/fstab
cgroup /sys/fs/cgroup cgroup defaults 0 0
掛載
mount /sys/fs/cgroup
Check kernel configuration :
sudo lxc-checkconfig
設定網路(SimpleBridge),確認有安裝 bridge-utils 之後修改 /etc/network/interfaces
auto br0
iface br0 inet dhcp
bridge_ports eth0
bridge_fd 0
bridge_maxwait 0
重新啟動網路
/etc/init.d/networking restart
之後每次lxc-create之後去修改 LXC 網路設定 /var/lib/lxc/$container/config
[...]
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.hwaddr = 00:FF:AA:00:00:01
lxc.network.ipv4 = 0.0.0.0/24
[...]
到這邊,已經可以開始LXC但會發現都要sudo, 借下來我要設定 一般使用者可以用的方式 主要參考這邊的資訊 https://www.stgraber.org/2014/01/17/lxc-1-0-unprivileged-containers/
再度開始安裝相關套件
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install lxc systemd-services uidmap
然後設定uidmap
sudo usermod --add-subuids 100000-165536 $USER
sudo usermod --add-subgids 100000-165536 $USER
sudo chmod +x $HOME
確認 uidmap grep $USER /etc/sub* 2>/dev/null
編輯設定檔 ~/.config/lxc/default.conf
lxc.network.type = veth
lxc.network.link = br0
lxc.network.flags = up
lxc.network.hwaddr = 00:16:3e:xx:xx:xx
lxc.id_map = u 0 100000 65536
lxc.id_map = g 0 100000 65536
編輯網路設定檔 /etc/lxc/lxc-usernet
<your username> veth br0 10
幾個系統設定對應到家目錄的設定檔位置
/etc/lxc/lxc.conf => ~/.config/lxc/lxc.conf
/etc/lxc/default.conf => ~/.config/lxc/default.conf
/var/lib/lxc => ~/.local/share/lxc
/var/lib/lxcsnaps => ~/.local/share/lxcsnaps
/var/cache/lxc => ~/.cache/lxc
針對 Debian Sid 因為 kernel 關係需要設定下面這個,找了好久才找到問題 XD
echo 1 > /proc/sys/kernel/unprivileged_userns_clone
建立 lxc
lxc-create -l DEBUG -l lxc-debug.log -t download -n p1 -- -d ubuntu -r trusty -a amd64
Unprivileged 部份結束!
最近發現 lxc-start 有問題 所以 殘念了
some references:
- https://wiki.debian.org/LXC
- https://wiki.debian.org/LXC/LibVirtDefaultNetwork
- https://www.stgraber.org/2014/01/17/lxc-1-0-unprivileged-containers/
- http://crashcourse.housegordon.org/LXC-semi-unprivileged-containers.html
- https://lists.linuxcontainers.org/pipermail/lxc-devel/2014-February/008051.html
Comments