module blacklist

对内核模块来说,黑名单是指禁止某个模块装入的机制
使用 /etc/modprobe.d/ 中的文件
在 /etc/modprobe.d/ 中创建 .conf 文件,使用 blacklist 关键字屏蔽不需要的模块,例如如果不想装入 pcspkr 模块:

/etc/modprobe.d/nobeep.conf
# Do not load the pcspkr module on boot
blacklist pcspkr
或者通过命令行的模式
modprobe.blacklist=modname1,modname2
从这里可以看出blacklist是一个模块参数,其实现在linux/module.c中
其源码如下:
static bool blacklisted(const char *module_name)
{
	const char *p;
	size_t len;
	#可以看到module_blacklist 这个list 为null,则直接返回false
	if (!module_blacklist)
		return false;
	#这里通过查询这个list 来比较模块的name 是否在module_blacklist中,如果在的话,返回true,就不在加载#layout_and_allocate 这个函数中加载这个模块
	for (p = module_blacklist; *p; p += len) {
		len = strcspn(p, ",");
		if (strlen(module_name) == len && !memcmp(module_name, p, len))
			return true;
		if (p[len] == ',')
			len++;
	}
	return false;
}
core_param(module_blacklist, module_blacklist, charp, 0400);
blacklisted 这个函数是在layout_and_allocate 中加载的
static struct module *layout_and_allocate(struct load_info *info, int flags)
{
	/* Module within temporary copy. */
	struct module *mod;
	unsigned int ndx;
	int err;

	mod = setup_load_info(info, flags);
	if (IS_ERR(mod))
		return mod;
	#可以看到如果blacklisted 返回true,则layout_and_allocate 就返回-EPERM,表示不加载这个模块
	if (blacklisted(info->name))
		return ERR_PTR(-EPERM);
}

猜你喜欢

转载自blog.csdn.net/tiantao2012/article/details/80966482