Linux 进程管理之命名空间_linux设置一个新的命名空间(2)

本文详细解释了Linux内核中的set_fs_root函数,chroot操作对进程root目录的影响,以及uts_namespace和namespace创建过程,包括init_nsproxy、init_uts_ns、copy_namespaces和create_new_namespaces等关键函数的作用和实现细节。
摘要由CSDN通过智能技术生成

struct dentry {

struct inode *d_inode; /* Where the name belongs to - NULL is * negative */

};


(3)  
 set\_fs\_root就是最关键的函数,把当前进程的文件系统的root设置为path。



set_fs_root(current->fs, &path);



/*
* Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
* It can block.
*/
void set_fs_root(struct fs_struct *fs, const struct path *path)
{
struct path old_root;

path\_get(path);
spin\_lock(&fs->lock);
write\_seqcount\_begin(&fs->seq);
old_root = fs->root;
fs->root = \*path;
write\_seqcount\_end(&fs->seq);
spin\_unlock(&fs->lock);
if (old_root.dentry)
	path\_put(&old_root);

}



struct fs_struct {

struct path root, pwd;

};


其中:  
 root:根目录的目录项。  
 pwd:当前工作目录的目录项。


(4)  
 chroot 修改了进程的 root 目录的核心操作就是修改了 当前进程(struct task\_struct) 的 current->fs->root = 当前文件名filename的struct path。  
 这样当前进程就认为 filename 是根目录,因为 fs->root 存的是 filename 目录的 path 结构。


chroot仅仅是在访问文件系统目录的时候,限定了用户的根目录。


## 二、uts\_namespace


### 2.1 init\_nsproxy


假如不指定ns,那么默认所有进程在创建的时候,都会指定一个默认的ns:init\_nsproxy。  
 比如 init\_pid\_ns :  
 进程号命名空间用来隔离进程号,对应的结构体是pid\_namespace。每个进程号命名空间独立分配进程号。进程号命名空间按层次组织成一棵树,初始进程号命名空间是树的根,对应全局变量init\_pid\_ns,所有进程默认属于初始进程号命名空间。



extern struct nsproxy init_nsproxy;

struct nsproxy init_nsproxy = {
.count = ATOMIC_INIT(1),
.uts_ns = &init_uts_ns,
#if defined(CONFIG_POSIX_MQUEUE) || defined(CONFIG_SYSVIPC)
.ipc_ns = &init_ipc_ns,
#endif
.mnt_ns = NULL,
.pid_ns_for_children = &init_pid_ns,
#ifdef CONFIG_NET
.net_ns = &init_net,
#endif
#ifdef CONFIG_CGROUPS
.cgroup_ns = &init_cgroup_ns,
#endif
};


init\_nsproxy定义了初始的全局命名空间,其中维护了指向各个子系统初始化的命名空间对象的指针。


### 2.2 init\_uts\_ns



struct new_utsname {
char sysname[__NEW_UTS_LEN + 1];
char nodename[__NEW_UTS_LEN + 1];
char release[__NEW_UTS_LEN + 1];
char version[__NEW_UTS_LEN + 1];
char machine[__NEW_UTS_LEN + 1];
char domainname[__NEW_UTS_LEN + 1];
};


这些字符串存储了系统名Linux,发行版,内核版本,机器等等,可以通过uname查看,也可以在/proc/sys/kernel/下查看:  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/797fac39314b40c4a39bfff308e0f925.png)



struct uts_namespace {
struct kref kref;
struct new_utsname name;
struct user_namespace *user_ns;
struct ucounts *ucounts;
struct ns_common ns;
};
extern struct uts_namespace init_uts_ns;



struct uts_namespace init_uts_ns = {
.kref = {
.refcount = ATOMIC_INIT(2),
},
.name = {
.sysname = UTS_SYSNAME,
.nodename = UTS_NODENAME,
.release = UTS_RELEASE,
.version = UTS_VERSION,
.machine = UTS_MACHINE,
.domainname = UTS_DOMAINNAME,
},
.user_ns = &init_user_ns,
.ns.inum = PROC_UTS_INIT_INO,
#ifdef CONFIG_UTS_NS
.ns.ops = &utsns_operations,
#endif
};
EXPORT_SYMBOL_GPL(init_uts_ns);


init\_uts\_ns是uts\_ns的初始化配置,相关的预处理器常数在内核中各处定义,通过编译内核顶层Makefile动态生成。  
 系统名是固定的:



#define UTS_SYSNAME “Linux”


## 三、创建namespace


### 3.1 fork


以fork为例:



SYSCALL_DEFINE0(fork)
–>_do_fork()
–>copy_process()
–>copy_namespaces()
–>create_new_namespaces()


### 3.2 copy\_namespaces



/*
* called from clone. This now handles copy for nsproxy and all
* namespaces therein.
*/
int copy_namespaces(unsigned long flags, struct task_struct *tsk)
{
struct nsproxy *old_ns = tsk->nsproxy;
struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns);
struct nsproxy *new_ns;

if (likely(!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
		      CLONE_NEWPID | CLONE_NEWNET |
		      CLONE_NEWCGROUP)))) {
	get\_nsproxy(old_ns);
	return 0;
}

if (!ns\_capable(user_ns, CAP_SYS_ADMIN))
	return -EPERM;

/\*

* CLONE_NEWIPC must detach from the undolist: after switching
* to a new ipc namespace, the semaphore arrays from the old
* namespace are unreachable. In clone parlance, CLONE_SYSVSEM
* means share undolist with parent, so we must forbid using
* it along with CLONE_NEWIPC.
*/
if ((flags & (CLONE_NEWIPC | CLONE_SYSVSEM)) ==
(CLONE_NEWIPC | CLONE_SYSVSEM))
return -EINVAL;

new_ns = create\_new\_namespaces(flags, tsk, user_ns, tsk->fs);
if (IS\_ERR(new_ns))
	return  PTR\_ERR(new_ns);

tsk->nsproxy = new_ns;
return 0;

}


如果 clone 的参数里面没有 CLONE\_NEWNS | CLONE\_NEWUTS | CLONE\_NEWIPC | CLONE\_NEWPID | CLONE\_NEWNET | CLONE\_NEWCGROUP,就返回原来的 namespace,调用 get\_nsproxy。  
 get\_nsproxy函数只是将struct nsproxy成员的count加1。



static inline void get_nsproxy(struct nsproxy *ns)
{
atomic_inc(&ns->count);
}


相对应有个函数put\_nsproxy将struct nsproxy成员的count减1,这个函数每次减1时会判断count是否等于0,等于0就释放掉调用free\_nsproxy释放掉命名空间资源。



static inline void put_nsproxy(struct nsproxy *ns)
{
if (atomic_dec_and_test(&ns->count)) {
free_nsproxy(ns);
}
}


get\_nsproxy 和 put\_nsproxy 操作类似于C++中的智能指针操作。


### 3.3 create\_new\_namespaces


如果 clone 的参数里面有 CLONE\_NEWNS | CLONE\_NEWUTS | CLONE\_NEWIPC | CLONE\_NEWPID | CLONE\_NEWNET | CLONE\_NEWCGROUP标志位,调用create\_new\_namespaces



/*
* Create new nsproxy and all of its the associated namespaces.
* Return the newly created nsproxy. Do not attach this to the task,
* leave it to the caller to do proper locking and attach it to task.
*/
static struct nsproxy *create_new_namespaces(unsigned long flags,
struct task_struct *tsk, struct user_namespace *user_ns,
struct fs_struct *new_fs)
{
struct nsproxy *new_nsp;
int err;

new_nsp = create\_nsproxy();
if (!new_nsp)
	return ERR\_PTR(-ENOMEM);

new_nsp->mnt_ns = copy\_mnt\_ns(flags, tsk->nsproxy->mnt_ns, user_ns, new_fs);
if (IS\_ERR(new_nsp->mnt_ns)) {
	err = PTR\_ERR(new_nsp->mnt_ns);
	goto out_ns;
}

new_nsp->uts_ns = copy\_utsname(flags, user_ns, tsk->nsproxy->uts_ns);
if (IS\_ERR(new_nsp->uts_ns)) {
	err = PTR\_ERR(new_nsp->uts_ns);
	goto out_uts;
}

new_nsp->ipc_ns = copy\_ipcs(flags, user_ns, tsk->nsproxy->ipc_ns);
if (IS\_ERR(new_nsp->ipc_ns)) {
	err = PTR\_ERR(new_nsp->ipc_ns);
	goto out_ipc;
}

new_nsp->pid_ns_for_children =
	copy\_pid\_ns(flags, user_ns, tsk->nsproxy->pid_ns_for_children);
if (IS\_ERR(new_nsp->pid_ns_for_children)) {
	err = PTR\_ERR(new_nsp->pid_ns_for_children);
	goto out_pid;
}

new_nsp->cgroup_ns = copy\_cgroup\_ns(flags, user_ns,
				    tsk->nsproxy->cgroup_ns);
if (IS\_ERR(new_nsp->cgroup_ns)) {
	err = PTR\_ERR(new_nsp->cgroup_ns);
	goto out_cgroup;
}

new_nsp->net_ns = copy\_net\_ns(flags, user_ns, tsk->nsproxy->net_ns);
if (IS\_ERR(new_nsp->net_ns)) {
	err = PTR\_ERR(new_nsp->net_ns);
	goto out_net;
}

return new_nsp;
......

}


创建新的nsproxy及其所有关联的名称空间,返回新创建的nsproxy。


#### 3.3.1 copy\_utsname


比如copy\_utsname:如果没有标志位CLONE\_NEWUTS,则返回old\_ns,如果设置了创建新的new\_ns。



// linux-4.10.1/kernel/utsname.c
/*
* Copy task tsk’s utsname namespace, or clone it if flags
* specifies CLONE_NEWUTS. In latter case, changes to the
* utsname of this process won’t be seen by parent, and vice
* versa.
*/
struct uts_namespace *copy_utsname(unsigned long flags,
struct user_namespace *user_ns, struct uts_namespace *old_ns)
{
struct uts_namespace *new_ns;

BUG\_ON(!old_ns);
get\_uts\_ns(old_ns);

if (!(flags & CLONE_NEWUTS))
	return old_ns;

new_ns = clone\_uts\_ns(user_ns, old_ns);

put\_uts\_ns(old_ns);
return new_ns;

}


#### 3.3.2 copy\_ipcs


比如copy\_ipcs:



struct ipc_namespace *copy_ipcs(unsigned long flags,
struct user_namespace *user_ns, struct ipc_namespace *ns)
{
if (!(flags & CLONE_NEWIPC))
return get_ipc_ns(ns);
return create_ipc_ns(user_ns, ns);
}


如果没有设置标志位CLONE\_NEWIPC,调用get\_ipc\_ns将tsk->nsproxy->ipc\_ns->count加1。



static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
{
if (ns)
atomic_inc(&ns->count);
return ns;
}


如果有标志位CLONE\_NEWIPC,调用 create\_ipc\_ns创建新的ipc\_namespace命名空间。


#### 3.3.3 copy\_pid\_ns


如果没有设置标志位 CLONE\_NEWPID,则返回老的 pid namespace。



// linux-4.10.1/kernel/pid_namespace.c
struct pid_namespace *copy_pid_ns(unsigned long flags,
struct user_namespace *user_ns, struct pid_namespace *old_ns)
{
if (!(flags & CLONE_NEWPID))
return get_pid_ns(old_ns);
if (task_active_pid_ns(current) != old_ns)
return ERR_PTR(-EINVAL);
return create_pid_namespace(user_ns, old_ns);
}


如果设置了,就调用 create\_pid\_namespace,创建新的 pid namespace。采用slab分配器分配struct pid\_namespace对象。



// linux-4.10.1/kernel/pid_namespace.c
static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns,
struct pid_namespace *parent_pid_ns)
{

struct pid_namespace *ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);

}



static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)
{
return kmem_cache_alloc(k, flags | __GFP_ZERO);
}


#### 3.3.4 copy\_cgroup\_ns


如果没有设置标志位 CLONE\_NEWCGROUP,则返回老的 struct cgroup\_namespace,old\_ns。



// linux-4.10.1/kernel/cgroup.c
struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
struct user_namespace *user_ns,
struct cgroup_namespace *old_ns)
{
struct cgroup_namespace *new_ns;
struct ucounts *ucounts;
struct css_set *cset;

BUG\_ON(!old_ns);

if (!(flags & CLONE_NEWCGROUP)) {
	get\_cgroup\_ns(old_ns);
	return old_ns;
}

/\* Allow only sysadmin to create cgroup namespace. \*/
if (!ns\_capable(user_ns, CAP_SYS_ADMIN))
	return ERR\_PTR(-EPERM);

ucounts = inc\_cgroup\_namespaces(user_ns);
if (!ucounts)
	return ERR\_PTR(-ENOSPC);

/\* It is not safe to take cgroup\_mutex here \*/
spin\_lock\_irq(&css_set_lock);
cset = task\_css\_set(current);
get\_css\_set(cset);
spin\_unlock\_irq(&css_set_lock);

new_ns = alloc\_cgroup\_ns();
if (IS\_ERR(new_ns)) {
	put\_css\_set(cset);
	dec\_cgroup\_namespaces(ucounts);
	return new_ns;
}

new_ns->user_ns = get\_user\_ns(user_ns);
new_ns->ucounts = ucounts;
new_ns->root_cset = cset;

return new_ns;

}


如果设置标志位 CLONE\_NEWCGROUP,则调用alloc\_cgroup\_ns创建新的struct cgroup\_namespace,new\_ns。



// linux-4.10.1/kernel/cgroup.c
static struct cgroup_namespace *alloc_cgroup_ns(void)
{
struct cgroup_namespace *new_ns;
int ret;

new_ns = kzalloc(sizeof(struct cgroup\_namespace), GFP_KERNEL);
if (!new_ns)
	return ERR\_PTR(-ENOMEM);
ret = ns\_alloc\_inum(&new_ns->ns);
if (ret) {
	kfree(new_ns);
	return ERR\_PTR(ret);
}
atomic\_set(&new_ns->count, 1);
new_ns->ns.ops = &cgroupns_operations;
return new_ns;

}


#### 3.3.5 copy\_net\_ns


如果没有设置标志位 CLONE\_NEWNET,则返回老的 old\_net。





为了做好运维面试路上的助攻手,特整理了上百道 **【运维技术栈面试题集锦】** ,让你面试不慌心不跳,高薪offer怀里抱!

这次整理的面试题,**小到shell、MySQL,大到K8s等云原生技术栈,不仅适合运维新人入行面试需要,还适用于想提升进阶跳槽加薪的运维朋友。**

![](https://img-blog.csdnimg.cn/img_convert/333a887797d42a7e90c2c886477d40d1.png)

本份面试集锦涵盖了

*   **174 道运维工程师面试题**
*   **128道k8s面试题**
*   **108道shell脚本面试题**
*   **200道Linux面试题**
*   **51道docker面试题**
*   **35道Jenkis面试题**
*   **78道MongoDB面试题**
*   **17道ansible面试题**
*   **60道dubbo面试题**
*   **53道kafka面试**
*   **18道mysql面试题**
*   **40道nginx面试题**
*   **77道redis面试题**
*   **28道zookeeper**

**总计 1000+ 道面试题, 内容 又全含金量又高**

*   **174道运维工程师面试题**

> 1、什么是运维?

> 2、在工作中,运维人员经常需要跟运营人员打交道,请问运营人员是做什么工作的?

> 3、现在给你三百台服务器,你怎么对他们进行管理?

> 4、简述raid0 raid1raid5二种工作模式的工作原理及特点

> 5、LVS、Nginx、HAproxy有什么区别?工作中你怎么选择?

> 6、Squid、Varinsh和Nginx有什么区别,工作中你怎么选择?

> 7、Tomcat和Resin有什么区别,工作中你怎么选择?

> 8、什么是中间件?什么是jdk?

> 9、讲述一下Tomcat8005、8009、8080三个端口的含义?

> 10、什么叫CDN?

> 11、什么叫网站灰度发布?

> 12、简述DNS进行域名解析的过程?

> 13、RabbitMQ是什么东西?

> 14、讲一下Keepalived的工作原理?

> 15、讲述一下LVS三种模式的工作过程?

> 16、mysql的innodb如何定位锁问题,mysql如何减少主从复制延迟?

> 17、如何重置mysql root密码?

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
y有什么区别?工作中你怎么选择?

> 6、Squid、Varinsh和Nginx有什么区别,工作中你怎么选择?

> 7、Tomcat和Resin有什么区别,工作中你怎么选择?

> 8、什么是中间件?什么是jdk?

> 9、讲述一下Tomcat8005、8009、8080三个端口的含义?

> 10、什么叫CDN?

> 11、什么叫网站灰度发布?

> 12、简述DNS进行域名解析的过程?

> 13、RabbitMQ是什么东西?

> 14、讲一下Keepalived的工作原理?

> 15、讲述一下LVS三种模式的工作过程?

> 16、mysql的innodb如何定位锁问题,mysql如何减少主从复制延迟?

> 17、如何重置mysql root密码?

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
  • 9
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值