Linux Container是Google开源的轻量级虚拟化方案,是一种资源容器,简称LXC。LXC可以制作自己的文件系统,即rootfs。
一、创建文件系统与挂载表
1、准备目录
mkdir /lxc
然后进入/lxc目录中,创建根文件系统的目录
mkdir rootfs
进入/lxc/rootfs目录中,创建其他目录
mkdir bin dev etc lib lib64 proc sbin sys usr var
2、创建挂载表
在/lxc目录下,新建文件fstab,编辑内容如下:
/bin /lxc/rootfs/bin none ro,bind 0 0 /sbin /lxc/rootfs/sbin none ro,bind 0 0 /lib /lxc/rootfs/lib none ro,bind 0 0 /lib64 /lxc/rootfs/lib64 none ro,bind 0 0 /etc /lxc/rootfs/etc none ro,bind 0 0 /usr /lxc/rootfs/usr none ro,bind 0 0 /dev /lxc/rootfs/dev none rw,bind 0 0 /dev/pts /lxc/rootfs/dev/pts none rw,bind 0 0 /proc /lxc/rootfs/proc proc defaults 0 0 /sys /lxc/rootfs/sys sysfs defaults 0 0
其大意就是将宿主机操作系统的/bin /sbin /lib /etc等目录挂载到/lxc/rootfs下的对应目录中。
二、启动lxc
1、编辑lxc配置文件
编辑config文件,内容如下
lxc.utsname = host_name lxc.rootfs = /lxc/rootfs lxc.mount = /lxc/fstab
2、启动lxc,执行bash命令
lxc-execute -n lxc1 -f config bash
这样就启动了一个名为lxc1的Container,执行bash,就相当于进入了Container内部。如果在终端输出不正常,就使用命令reset。
3、查看lxc内部文件系统
bash-4.1# ls bin data dev etc lib lib64 proc sbin sys usr var
我们发现只能在/lxc/rootfs内部进行操作。
转载于:https://blog.51cto.com/speakingbaicai/1319572