无法写入 /sys/kernel/ 以在 OVH CentOS 7 上为 MongoDB 禁用透明大页面 (THP)

Can't Write to /sys/kernel/ to disable Transparent Huge Pages (THP) for MongoDB on OVH CentOS 7

我的问题

我无法删除关于 OVH CentOS 7 安装上的透明大页面 (THP) 的 MongoDB 警告,问题似乎是无法写入 /sys/kernel/mm 作为 root。

首先,我知道 OVH 内核是定制的,我知道你们中的许多人会说要使用全新的非定制内核,但现在还不行。我需要为当前 OS.

解决这个问题

MongoDB 警告:

2016-03-09T00:31:45.889-0500 W CONTROL  [initandlisten] Failed to probe "/sys/kernel/mm/transparent_hugepage": Permission denied
2016-03-09T00:31:45.889-0500 W CONTROL  [initandlisten] Failed to probe "/sys/kernel/mm/transparent_hugepage": Permission denied

MongoDB 正在尝试读取 transparent_hugepage 文件(如下),但它们不存在:

/sys/kernel/mm/transparent_hugepage/enabled
/sys/kernel/mm/transparent_hugepage/defrag

无法创建文件

我见过的所有解决方案都涉及创建文件并使用 never 填充它们,包括 script in the MongoDB documentation。在所有的解决方案中,这是关键部分:

echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

但是,文件不存在,我无法以 root 身份在 /sys/kernel/mm 下创建任何内容。

root@myhost [~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
-bash: /sys/kernel/mm/transparent_hugepage/enabled: No such file or directory

root@myhost [~]# mkdir -p /sys/kernel/mm/transparent_hugepage
mkdir: cannot create directory ‘/sys/kernel/mm/transparent_hugepage’: Operation not permitted

目录/sys/kernel/mm的所有者和所属组是root,我临时把权限从700改成了777,但是还是不能用root创建目录。

调整后的配置文件也无济于事

为了彻底,我还创建了自定义 Tuned 配置文件(按照上面 MongoDB link 中的说明)并激活它,但它会生成错误 WARNING tuned.plugins.plugin_vm: Option 'transparent_hugepages' is not supported on current hardware.

调整配置文件(/etc/tuned/no-thp/tuned.conf):

[main]
include=virtual-guest

[vm]
transparent_hugepages=never

Tuned 日志中的错误:

WARNING  tuned.plugins.plugin_vm: Option 'transparent_hugepages' is not supported on current hardware.

MongoDB 本身的一些解决方案?

似乎最好的解决方案是以某种方式显式配置 MongoDB 不使用 THP,这样它就不必检查丢失的文件,但我从未见过这样的情况。如果有办法,即使涉及自定义MongoDB(每次更新后重复),我也愿意这样做。

现在我已经在 OVH 上安装了 CentOS 7。他们使用 /boot/bzImage-3.14.32-xxxx-grs-ipv6-64 来实现 grsecurity (https://grsecurity.net),从而阻止访问某些文件夹。

MongoDB关于huge pages的警告的非常简单的解决方案可以通过更换内核来解决。 CentOS7的程序如下:

  1. 从 OVH ftp 下载所需内核:ftp://ftp.ovh.net/made-in-ovh/bzImage2/boot 文件夹。
  2. 编辑 /etc/grub2.cfg

    # linux /boot/bzImage-3.14.32-xxxx-grs-ipv6-64 root=/dev/md1 ro net.ifnames=0

    linux /boot/bzImage-4.8.17-xxxx-std-ipv6-64 root=/dev/md1 ro net.ifnames=0

这里我把默认的bzImage-3.14.32-xxxx-grs-ipv6-64换成了bzImage-4.8.17-xxxx-std-ipv6-64没有grs.

现在,重启并检查新内核是否正常:

root@ns506846 ~]# uname -r
4.8.17-xxxx-std-ipv6-64