在 Red Hat Enterprise Linux 7 中可以使用三种不同的方法来限制 CPU 的数量。
方法二:nr_cpus
使用 nr_cpus 参数:在 /boot/grub2/grub.cfg 中添加内核参数 nr_cpus=N
重要的提示: 无法在 Red Hat Enterprise Linux 系统上禁用 CPU0。
启用上述参数后,将无法在线热插更多CPU 。
nr_cpus= [SMP] Maximum number of processors that an SMP kernel could support. nr_cpus=n : n >= 1 limits the kernel to supporting 'n' processors.
通过在“/etc/sysconfig/grub”中使用 GRUB_CMDLINE_LINUX 下的 maxcpus 变量提供我们要在系统中使用的 CPU 数量,如下所示我将 CPU 数量限制为 5
GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 nr_cpus=5" GRUB_DISABLE_RECOVERY="true"
接下来使用重新生成 grub2 配置文件
# grub2-mkconfig -o /boot/grub2/grub.cfg Generating grub configuration file ... Found linux image: /boot/vmlinuz-3.10.0-693.5.2.el7.x86_64 Found initrd image: /boot/initramfs-3.10.0-693.5.2.el7.x86_64.img Found linux image: /boot/vmlinuz-0-rescue-1be8540dbd0449f4b6c1a94f585eb350 Found initrd image: /boot/initramfs-0-rescue-1be8540dbd0449f4b6c1a94f585eb350.img done
验证更改
# grep nr_cpus /boot/grub2/grub.cfg linux16 /vmlinuz-3.10.0-693.5.2.el7.x86_64 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto noht rhgb quiet console=tty0 nr_cpus=5 linux16 /vmlinuz-0-rescue-1be8540dbd0449f4b6c1a94f585eb350 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 nr_cpus=5
在重新启动之前让我们检查可用的核心
# grep processor /proc/cpuinfo | wc -l 16
接下来重新启动服务器以激活更改
系统启动后,验证系统上活动的 cpu 计数
# grep processor /proc/cpuinfo processor : 0 processor : 1 processor : 2 processor : 3 processor : 4
方法三
使用“/sys/devices/system/cpu/cpu<id>/online”文件手动禁用单个 CPU
重要的提示:
此方法是临时的,不会在重新启动后持续存在。
方法一:maxcpus
使用 maxcpus 参数:在 /boot/grub2/grub.cfg 中添加内核参数 maxcpus=N。
提示:无法在 Red Hat Enterprise Linux 系统上禁用 CPU0。
启用上述参数后,无法在线热插拔更多CPU。
maxcpus= [SMP] SMP内核应使用的最大处理器数. maxcpus=n : n >= 0 限制内核使用 'n' 个处理器. n=0 是一种特殊情况,它相当于“nosmp”,它还禁用IO APIC。
通过在“/etc/sysconfig/grub”中的“GRUB_CMDLINE_LINUX”下使用 maxcpus 变量提供我们想要在系统中使用的 CPU 数量,如下所示
我将 CPU 数量限制为 6
GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="novga panic=1 numa=off crashkernel=auto noht rhgb quiet console=tty0 maxcpus=6" GRUB_DISABLE_RECOVERY="true"
接下来使用重新生成 grub2 配置文件
# grub2-mkconfig -o /boot/grub2/grub.cfg Generating grub configuration file ... Found linux image: /boot/vmlinuz-3.10.0-693.5.2.el7.x86_64 Found initrd image: /boot/initramfs-3.10.0-693.5.2.el7.x86_64.img Found linux image: /boot/vmlinuz-0-rescue-1334797d644549a8aa195f756eaab1e1 Found initrd image: /boot/initramfs-0-rescue-1334797d644549a8aa195f756eaab1e1.img done
验证更改
# grep maxcpus /boot/grub2/grub.cfg linux16 /vmlinuz-3.10.0-693.5.2.el7.x86_64 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 maxcpus=6 linux16 /vmlinuz-0-rescue-1334797d644549a8aa195f756eaab1e1 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 maxcpus=6
由于 systemd 内核中的已知 BUG 添加了一条 udev 规则,该规则会在 CPU 出现在系统中后自动将其设置为“在线”状态,因此必须禁用 maxcpus 才能工作
可以在 /usr/lib/udev/rules.d/40-redhat.rules 文件中使用以下命令禁用此规则
# sed -i 's/^(SUBSYSTEM=="cpu".*TEST=="online".*ATTR{online}="1")/#1/' /usr/lib/udev/rules.d/40-redhat.rules
在这里,我们注释掉了在系统重启期间启用 CPU 的以下行
#SUBSYSTEM=="cpu", ACTION=="add", TEST=="online", ATTR{online}=="0", ATTR{online}="1"
接下来重建initramfs
# dracut -f
接下来重新启动Knife 片机以使更改生效并在节点启动后验证 cpu 的数量
# cat /proc/cmdline BOOT_IMAGE=/vmlinuz-3.10.0-693.5.2.el7.x86_64 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 maxcpus=6
# grep processor /proc/cpuinfo | wc -l 6
# lscpu | grep -i numa NUMA node(s): 1 NUMA node0 CPU(s): 0-5
正如我们现在看到的,我们只有 6 个 CPU 内核。