问题

环境:Oracle VM VirtualBox + CentOS 8.5 + MongoDB 5.0.28。

现象:MongoDB服务无法启动,报“非法指令(Illegal instruction)”错误:

snippet.bash
[root@bogon ~]# mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log
Illegal instruction (core dumped)

gdb调式宕机堆栈:

snippet.bash
[root@bogon ~]# gdb /usr/bin/mongod
......
Program received signal SIGILL, Illegal instruction.
0x000055555966d59a in tcmalloc::SizeMap::Init() ()
(gdb) bt
#0  0x000055555966d59a in tcmalloc::SizeMap::Init() ()
#1  0x0000555559675eb7 in tcmalloc::Static::InitStaticVars() ()
#2  0x0000555559677907 in tcmalloc::ThreadCache::InitModule() ()
#3  0x0000555559677a9d in tcmalloc::ThreadCache::CreateCacheIfNecessary() ()
#4  0x0000555559721275 in tcmalloc::allocate_full_malloc_oom(unsigned long) ()
#5  0x00007ffff606c00f in __fopen_internal () from /lib64/libc.so.6
#6  0x00007ffff3768dc2 in selinuxfs_exists () from /lib64/libselinux.so.1
#7  0x00007ffff3760d0c in init_lib () from /lib64/libselinux.so.1
#8  0x00007ffff7ddf8ba in call_init.part () from /lib64/ld-linux-x86-64.so.2
#9  0x00007ffff7ddf9ba in _dl_init () from /lib64/ld-linux-x86-64.so.2
#10 0x00007ffff7dd0fda in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
#11 0x0000000000000001 in ?? ()
#12 0x00007fffffffe620 in ?? ()
#13 0x0000000000000000 in ?? ()

查看Linux系统日志:

Sep 24 20:58:22 bogon kernel: traps: mongod[4061] trap invalid opcode ip:55ad7638859a sp:7ffe93a191a0 error:0 in mongod[55ad7226f000+5288000]
Sep 24 20:58:22 bogon systemd[1]: Started Process Core Dump (PID 4062/UID 0).
Sep 24 20:58:22 bogon systemd-coredump[4063]: Resource limits disable core dumping for process 4061 (mongod).
Sep 24 20:58:22 bogon systemd-coredump[4063]: Process 4061 (mongod) of user 0 dumped core.

原因

MongoDB 5.0之后版本需要CPU支持AVX指令集。

image

虚拟机的宿主机CPU是支持AVX指令集的,但是没有配置好虚拟机的环境。

image

解决方法

禁用Hyper-V

管理用户启动cmd,执行命令:

bcdedit /set hypervisorlaunchtype off

注:如果想要重新开启Hyper-V,则执行:

bcdedit /set hypervisorlaunchtype auto

重新启动计算机。

确认指令集是否可用,Linux执行命令:

cat /proc/cpuinfo |grep avx2

VirtualBox启用虚拟化

在启用CPU虚拟化之前,可以看到虚拟机的处理器配置“启用嵌套VT-x/AMD-V”选项是灰的,无法勾选。

image

以管理员身份运行cmd:

Microsoft Windows [版本 10.0.22621.2283]
(c) Microsoft Corporation。保留所有权利。

C:\Windows\System32>cd C:\Program Files\Oracle\VirtualBox\

C:\Program Files\Oracle\VirtualBox>VBoxManage.exe list vms
"RHL8.5-x86_64" {c053a82f-5df6-4783-b83a-40318bc1f3f3}

C:\Program Files\Oracle\VirtualBox>VBoxManage.exe modifyvm "RHL8.5-x86_64" --nested-hw-virt on

现在可以看到,“启用嵌套VT-x/AMD-V”已经处于可用状态:

image

参考