KVM:qemu-system-x86_64:无法设置客户机内存“pc.ram”:无法分配内存

KVM:qemu-system-x86_64:无法设置客户机内存“pc.ram”:无法分配内存

当我尝试启动虚拟机(分配了 1GB)时,尽管系统上有足够的内存(16GB),但我遇到了内存分配问题:

error: Failed to start domain exampleVM
error: internal error: qemu unexpectedly closed the monitor: 2023-09-10T11:49:20.853205Z qemu-system-x86_64: cannot set up guest memory 'pc.ram': Cannot allocate memory

我有三个虚拟机正在运行,总共分配了 6GB:

在此处输入图片描述

那时我还没有分配交换空间(现在已经添加了交换空间以使系统正常运行),但为什么这是问题所在呢?系统难道不应该交换内存中的页面来为虚拟机的内存腾出空间吗?

请解释一下这个问题,因为这对我来说相当神秘。谷歌只给出了不充分的答案和可能。

附言

% free -g
              total        used        free      shared  buff/cache   available
Mem:             15           6           2           5           7           3
Swap:             7           3           4
% ps_mem.py 
 Private  +   Shared  =  RAM used   Program

 44.0 KiB +  82.0 KiB = 126.0 KiB   dnsmasq
120.0 KiB +  79.0 KiB = 199.0 KiB   xrdp
292.0 KiB +  81.0 KiB = 373.0 KiB   cron
280.0 KiB + 115.0 KiB = 395.0 KiB   agetty (2)
540.0 KiB + 100.5 KiB = 640.5 KiB   auditd
600.0 KiB +  58.5 KiB = 658.5 KiB   mdadm
868.0 KiB + 150.5 KiB =   1.0 MiB   xrdp-sesman
996.0 KiB + 157.5 KiB =   1.1 MiB   exim4
200.0 KiB +   1.0 MiB =   1.2 MiB   ha_logd (2)
984.0 KiB + 333.5 KiB =   1.3 MiB   systemd-timesyncd
  1.2 MiB + 368.0 KiB =   1.5 MiB   systemd-logind
  1.0 MiB + 564.0 KiB =   1.5 MiB   upowerd
  1.6 MiB + 154.0 KiB =   1.7 MiB   dbus-daemon
  1.8 MiB + 154.0 KiB =   1.9 MiB   smartd
  1.5 MiB + 462.0 KiB =   1.9 MiB   unattended-upgr
  2.0 MiB +  94.5 KiB =   2.1 MiB   systemd-udevd
  1.6 MiB + 583.5 KiB =   2.2 MiB   virtlogd
  1.7 MiB + 559.5 KiB =   2.2 MiB   polkitd
  1.9 MiB + 314.0 KiB =   2.2 MiB   systemd-journald
  2.2 MiB + 126.5 KiB =   2.3 MiB   rsyslogd
  1.5 MiB + 854.5 KiB =   2.3 MiB   ssh
  1.4 MiB + 933.5 KiB =   2.4 MiB   (sd-pam)
  3.3 MiB + 617.5 KiB =   3.9 MiB   udisksd
  4.4 MiB +   1.2 MiB =   5.6 MiB   tuned
  4.2 MiB +   1.5 MiB =   5.7 MiB   bash (2)
  2.6 MiB +   3.5 MiB =   6.2 MiB   sshd (3)
  4.0 MiB +   3.2 MiB =   7.2 MiB   systemd (2)
  9.4 MiB +   1.2 MiB =  10.6 MiB   libvirtd
 13.6 MiB +   1.1 MiB =  14.7 MiB   fail2ban-server
 17.5 MiB + 300.5 KiB =  17.8 MiB   zabbix_agent2
 12.4 MiB +  40.5 MiB =  52.9 MiB   heartbeat (6)
  7.4 GiB +   6.2 MiB =   7.5 GiB   qemu-system-x86_64 (5)
---------------------------------
                          7.6 GiB
=================================

% cat /proc/meminfo 
MemTotal:       16298980 kB
MemFree:         2471024 kB
MemAvailable:    5498208 kB
Buffers:         2161672 kB
Cached:          3251996 kB
SwapCached:        10692 kB
Active:          8700828 kB
Inactive:        4551496 kB
Active(anon):    7763376 kB
Inactive(anon):  2282256 kB
Active(file):     937452 kB
Inactive(file):  2269240 kB
Unevictable:       59280 kB
Mlocked:           59280 kB
SwapTotal:       8388604 kB
SwapFree:        2057932 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:       7888728 kB
Mapped:            83356 kB
Shmem:           2164944 kB
Slab:             236904 kB
SReclaimable:     144204 kB
SUnreclaim:        92700 kB
KernelStack:        4448 kB
PageTables:        23336 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    16538092 kB
Committed_AS:   19768952 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
Percpu:            11904 kB
HardwareCorrupted:     0 kB
AnonHugePages:   7198720 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
DirectMap4k:     3027424 kB
DirectMap2M:    13621248 kB

答案1

这不是 KVM/QEMU/LIBVIRT 错误,因为内核分配了 1G 的系统内存,导致内存耗尽。

这可以是:

  • FS 无法释放缓存块(取决于 FS 的类型,例如 zfs 的处理方式可能与内核的正常行为不同)。尝试删除缓存、设置 overcommit_memory 等,并检查是否有帮助,并查看类似的问题
  • 内存碎片,参见或者 相关问题。

相关内容