Linux的命名空间

1. 为何提供命名空间

命名空间是一种轻量级的虚拟化手段。网络

传统的虚拟化软件,是虚拟化多个不一样的操做系统,对共享资源的限制很大。app

经过提供命名空间,能够让进程与进程之间,用户与用户之间彼此看不到对方。atom

命名空间,至关于容器。spa

命名空间,本质上创建了系统的不一样视图。操作系统

 

chroot是一种简单的命名空间,仅限于将进程限制在文件系统的某一部分。指针

2. 建立命名空间的方式

1). fork/clone建立新进程时,能够设置选项,使新进程与父进程共享命名空间,仍是新进程建立一个独立的命名空间。code

2). unshare系统调用,能够将进程的某些部分从父进程分离,其中也包括命名空间。进程

 

3. 实现:

   1: struct task_struct {
   2: ......
   3: /* namespaces */
   4:     struct nsproxy *nsproxy;
   5: ......
   6: }
   1: /*
   2:  * A structure to contain pointers to all per-process
   3:  * namespaces - fs (mount), uts, network, sysvipc, etc.
   4:  *
   5:  * 'count' is the number of tasks holding a reference.
   6:  * The count for each namespace, then, will be the number
   7:  * of nsproxies pointing to it, not the number of tasks.
   8:  *
   9:  * The nsproxy is shared by tasks which share all namespaces.
  10:  * As soon as a single namespace is cloned or unshared, the
  11:  * nsproxy is copied.
  12:  */
  13: struct nsproxy {
  14:     atomic_t count;
  15:     struct uts_namespace *uts_ns;
  16:     struct ipc_namespace *ipc_ns;
  17:     struct mnt_namespace *mnt_ns;
  18:     struct pid_namespace *pid_ns;
  19:     struct net          *net_ns;
  20: };

每一个进程都有一个指针指向nsproxy结构体,多个进程可能共享一个nsproxy结构体,好比父子进程。ip

一个nsproxy表明一整套的命名空间实现,其中包含了几个子系统的命名空间:资源

UTS(UNIX Timesharing System),包含了运行内核的名称,版本,底层体系结构的信息;

IPC,包含了全部与进程间通讯有关的信息;

MNT,包含了文件系统的视图;

PID,就是进程ID;

USER,就是用户;

NET,与网络相关。

各个子系统对于命名空间的实现与应用都各不相同。

相关文章
相关标签/搜索