docker基础:从chroot理解namespace的隔离

docker与linux内核的两个重要特性关系无比密切:namespace和cgroup。namespace实现了资源的隔离,而cgroup实现了控制。而namespace中隔离分为pid/net/ipc/mnt/uts/user,而mnt namespace,则是将一个进程放到指定的目录执行。其使得不同namespace的进程看到的文件结构不同,从而实现了隔离。而chroot则是更为古老的技术,但是由于其和mnt namespace实现的功能较为类似,本文将尝试使用chroot和busybox进行结合实现mnt namespace所实现的隔离机制。

这里写图片描述看起来有些廉价感的busybox,但是确实小巧且强大。

BusyBox 最初是由 Bruce Perens 在 1996 年为Debian GNU/Linux安装盘编写的。其目标是在一张软盘上创建一个可引导的 GNU/Linux 系统,这可以用作安装盘和急救盘。chroot可以在安装或者rescue disk的时候结合busybox,将根设定到新的mnt目录下,从而顺利实现相关功能,比如linux的root密码丢失时通过rescue的模式就是使用的这种方法。(详细可参看文章:http://blog.csdn.net/liumiaocn/article/details/52145327

这里写图片描述

从上面的图可以清晰地看到chroot /mnt/sysimage,是的,chroot就是这样的常用的一个linux的一个用法,远远在docker诞生之前人们已经在利用它做很多事情了。

busybox的最新版本1.25

事前准备

事前准备好要chroot的目录: 我们将使用chroot和bash以及busybox实现某一个实例将/tmp/ttt作为其根目录/,而其所有的操作都回被限制在这个目录之中,从而实现隔离的效果。

[root@host31 ~]# mkdir /tmp/ttt
[root@host31 ~]# cd /tmp/ttt
[root@host31 ttt]# pwd
/tmp/ttt
[root@host31 ttt]#

准备bash和bash的依赖关系,因为/tmp/ttt将会被设定为/,按照此关系将bash与其依赖关系的相对路径设定好。

[root@host31 ttt]# which bash
/usr/bin/bash
[root@host31 ttt]# ldd /usr/bin/bash
        linux-vdso.so.1 =>  (0x00007fff2fd7e000)
        libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007fd8c0cb5000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007fd8c0ab1000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fd8c06ef000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fd8c0ee6000)
[root@host31 ttt]# mkdir bin lib64
[root@host31 ttt]# cp -v /lib64/libtinfo.so.5 /lib64/libdl.so.2 /lib64/libc.so.6 /lib64/ld-linux-x86-64.so.2 lib64
‘/lib64/libtinfo.so.5’ -> ‘lib64/libtinfo.so.5’
‘/lib64/libdl.so.2’ -> ‘lib64/libdl.so.2’
‘/lib64/libc.so.6’ -> ‘lib64/libc.so.6’
‘/lib64/ld-linux-x86-64.so.2’ -> ‘lib64/ld-linux-x86-64.so.2’
[root@host31 ttt]# cp /usr/bin/bash bin
[root@host31 ttt]# wget https://busybox.net/downloads/binaries/busybox-x86_64
--2016-08-31 08:47:11--  https://busybox.net/downloads/binaries/busybox-x86_64
Resolving busybox.net (busybox.net)... 140.211.167.122
Connecting to busybox.net (busybox.net)|140.211.167.122|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1009384 (986K)
Saving to: ‘busybox-x86_64’

100%[=========================================================================================================>] 1,009,384    147KB/s   in 7.8s

2016-08-31 08:47:26 (126 KB/s) - ‘busybox-x86_64’ saved [1009384/1009384]

[root@host31 ttt]# ll
total 988
drwxr-xr-x. 2 root root      17 Aug 31 08:46 bin
-rw-r--r--. 1 root root 1009384 Oct  5  2015 busybox-x86_64
drwxr-xr-x. 2 root root      86 Aug 31 08:46 lib64
[root@host31 ttt]# du -k busybox-x86_64
988     busybox-x86_64
[root@host31 ttt]#

设定busybox的权限

[root@host31 ttt]# ll
total 988
drwxr-xr-x. 2 root root      17 Aug 31 08:46 bin
-rw-r--r--. 1 root root 1009384 Oct  5  2015 busybox-x86_64
drwxr-xr-x. 2 root root      86 Aug 31 08:46 lib64
[root@host31 ttt]# chmod 755 busybox-x86_64
[root@host31 ttt]# mv busybox-x86_64  busybox
[root@host31 ttt]# ll
total 988
drwxr-xr-x. 2 root root      17 Aug 31 08:46 bin
-rwxr-xr-x. 1 root root 1009384 Oct  5  2015 busybox
drwxr-xr-x. 2 root root      86 Aug 31 08:46 lib64
[root@host31 ttt]#

使用chroot将/tmp/ttt设定为根

[root@host31 ~]# pwd
/root
[root@host31 ~]# chroot /tmp/ttt /bin/bash
bash-4.2# pwd
/
bash-4.2#

可以看到chroot之后,在实际的目录/tmp/ttt下面,执行bash的内嵌命令pwd,其提示为/,然后我们可以通过busybox的ls命令继续确认出当前目录的内容确实就是刚刚的/tmp/ttt

bash-4.2# ./busybox ls -l
total 988
drwxr-xr-x    2 0        0               17 Aug 31 12:46 bin
-rwxr-xr-x    1 0        0          1009384 Oct  5  2015 busybox
drwxr-xr-x    2 0        0               86 Aug 31 12:46 lib64
bash-4.2#

在另外一个terminal中,确认/tmp/ttt的内容

[root@host31 ~]# cd /tmp/ttt
[root@host31 ttt]# ls -l
total 988
drwxr-xr-x. 2 root root      17 Aug 31 08:46 bin
-rwxr-xr-x. 1 root root 1009384 Oct  5  2015 busybox
drwxr-xr-x. 2 root root      86 Aug 31 08:46 lib64
[root@host31 ttt]#

如果依然不能完全确认,我们可以通过被chroot后的terminal中生成一个文件然后在另外一个terminal中进行确认

bash-4.2# ./busybox echo "hello , this is for testing" >test.txt
bash-4.2# ./busybox cat test.txt
hello , this is for testing
bash-4.2#

结果确认

[root@host31 ttt]# pwd
/tmp/ttt
[root@host31 ttt]# ll
total 992
drwxr-xr-x. 2 root root      17 Aug 31 08:46 bin
-rwxr-xr-x. 1 root root 1009384 Oct  5  2015 busybox
drwxr-xr-x. 2 root root      86 Aug 31 08:46 lib64
-rw-r--r--. 1 root root      28 Aug 31 08:58 test.txt
[root@host31 ttt]# cat test.txt
hello , this is for testing
[root@host31 ttt]#

隔离

在隔离了的/tmp/ttt中,如果试图改变目录到/tmp/ttt下存在的目录都是没有问题的,如果试图改变目录到/tmp/ttt之外,比如/tmp下则无法做到。

bash-4.2# cd bin
bash-4.2# ../busybox ls -l
total 940
-rwxr-xr-x    1 0        0           960376 Aug 31 12:46 bash
bash-4.2# cd /tmp
bash: cd: /tmp: No such file or directory
bash-4.2#

所以,通过这个简单的例子即可看出chroot如何能够实现了隔离的功能。

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值