请教如何实现 KVM 虚拟机共享内存

1 天前
 equator

有一种技术,可以让 kvm 虚拟机可以看到和使用宿主机全部的内存,但是看不到其它虚拟机具体使用的进程,这是一种什么技术呢?希望有大佬解答一下,采纳后红包答谢。

1430 次点击
所在节点    云计算
22 条回复
equator
1 天前
市面上有明确的云厂商做到了,但是不知道具体咋做的
samhjn
1 天前
搞个 virtio-ballon 再配配策略是不是就好了
busier
1 天前
你确定是 kvm 虚拟机?

搞不好是 lxc 或 openvz 之类的容器虚拟机技术
equator
1 天前
购买过他家的服务器,通过 lscpu 看到是 KVM 虚拟化的。应该不是内存气球技术,内存气球会导致虚拟机可见的内存变小,它是虚拟机能看到并使用宿主机的全部内存;登录 finalshell ,资源监控那里能看到其他人占用了内存,但是看不到具体是被谁使用的。
equator
1 天前
@busier 通过 lscpu 命令能看到是 KVM 虚拟化
busier
1 天前
lscpu 只是列出 cpu 支持的虚拟化指令集

怎么就确定当前运行的是 kvm 虚拟机了?
equator
1 天前
@busier 有这个展示:Hypervisor vendor:KVM
busier
1 天前
要不你把 lscpu 信息完整贴上来 让大家看看
equator
1 天前
这个是 lscpu 的内容:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 46 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 80
On-line CPU(s) list: 0-79
Vendor ID: GenuineIntel
Model name: Intel Xeon Processor
CPU family: 6
Model: 85
Thread(s) per core: 2
Core(s) per socket: 20
Socket(s): 2
Stepping: 4
BogoMIPS: 5999.99
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mc
a cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall n
x pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopolog
y cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pci
d sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer
aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowp
refetch cpuid_fault invpcid_single ibrs ibpb fsgsbase b
mi1 hle avx2 smep bmi2 erms invpcid rtm avx512f avx512d
q rdseed adx smap clwb avx512cd avx512bw avx512vl xsave
opt xsavec xgetbv1 arat pku ospke
Virtualization features:
Hypervisor vendor: KVM
Virtualization type: full
Caches (sum of all):
L1d: 2.5 MiB (80 instances)
L1i: 2.5 MiB (80 instances)
L2: 160 MiB (40 instances)
L3: 32 MiB (2 instances)
NUMA:
NUMA node(s): 2
NUMA node0 CPU(s): 0-39
NUMA node1 CPU(s): 40-79
Vulnerabilities:
Gather data sampling: Unknown: Dependent on hypervisor status
Itlb multihit: KVM: Mitigation: VMX unsupported
L1tf: Mitigation; PTE Inversion
Mds: Vulnerable: Clear CPU buffers attempted, no microcode;
SMT Host state unknown
Meltdown: Vulnerable
Mmio stale data: Vulnerable: Clear CPU buffers attempted, no microcode;
SMT Host state unknown
Retbleed: Mitigation; IBRS
Spec rstack overflow: Not affected
Spec store bypass: Vulnerable
Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer
sanitization
Spectre v2: Mitigation; IBRS, IBPB conditional, STIBP disabled, RSB
filling, PBRSB-eIBRS Not affected
Srbds: Not affected
Tsx async abort: Vulnerable: Clear CPU buffers attempted, no microcode;
SMT Host state unknown
cs8425
1 天前
有没有一种可能
实体机先用 kvm 虚拟化一个 VM VM 内再用容器之类的技术租给使用者?
不是直接 kvm 的 vm 租出去
也不是实体机上面直接开容器组出去
Becod
1 天前
https://s2.567996.xyz/imgs/2025/12/22/a09144da020bf90a.png

你先确定是不是 kvm ,lscpu 不一定准
yanqiyu
23 小时 1 分钟前
> 可以让 kvm 虚拟机可以看到和使用宿主机全部的内存

第一眼吓我一跳,我还以为你说的是虚拟机能字面意义的看到 host 的内存里面的每个 bit ,究极安全地狱。

> 通过 lscpu 命令能看到是 KVM 虚拟化

没什么意义,你在 kvm 的机器开个容器,运行 lscpu 看见的虚拟化也是 kvm ,因为容器看到的也是同一个 CPU 。要探测你看到的环境是什么 systemd-detect-virt 可能更恰当

> 应该不是内存气球技术,内存气球会导致虚拟机可见的内存变小

不,常见的内存气球实现一般是虚拟机里面一个气球驱动,在虚拟机占用掉一部分内存,然后把自己占用(但是实际上永不到)的内存还给主机。

要是你看到的内存占用和实际你的进程看到的内存占用+你能看见的内核占用完全不一致,说明你大概率在某种容器里面。
ysc3839
21 小时 52 分钟前
VMware Workstation 是这样的机制,虚拟机内看到的内存大小是固定的,但不使用就不会占用宿主机内存。不过似乎使用后就不能释放掉。
Hyper-V 虚拟机里内存多大宿主机就要占用多大,即使没使用也要占用,然后虚拟机内存可以动态调大或调小,调小的话占用宿主机的内存就减少。但是实测如果有程序一次性使用大量内存,会来不及扩容而卡死,必须开启 swap 。
julyclyde
21 小时 40 分钟前
你这个可能是 kvm 内的容器,而不是 kvm 本身
equator
21 小时 27 分钟前
我被自己之前的经验误导了,因为之前在 LXD 容器里面看 lscpu ,是看到 Hypervisor vendor: LXD 的。
经过各位大佬点拨,我也感觉不是 kvm 虚拟机了。

我买了一个测试用,不过这个是没有 root 权限的,看起来有点像 docker 哦。


tyu-ukhx1xmi@share-idebu4dywzt:~$ df -h
Filesystem Size Used Avail Use% Mounted on
overlay 20G 36M 20G 1% /
tmpfs 64M 0 64M 0% /dev
shm 64M 0 64M 0% /dev/shm
192.168.101.71:/share/dir_idebu4dywzt 98G 30G 63G 33% /mnt/data
/dev/sdb1 1000G 227G 773G 23% /etc/hosts
tmpfs 126G 72K 126G 1% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 10G 0 10G 0% /run/user/1000
tyu-ukhx1xmi@share-idebu4dywzt:~$ virt-what
-bash: virt-what: command not found
tyu-ukhx1xmi@share-idebu4dywzt:~$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 00:40 ? 00:00:00 /sbin/init
root 23 1 0 00:40 ? 00:00:00 /lib/systemd/systemd-journald
systemd+ 39 1 0 00:40 ? 00:00:00 /lib/systemd/systemd-resolved
message+ 41 1 0 00:40 ? 00:00:00 @dbus-daemon --system --address=systemd: --nofork
root 43 1 0 00:40 ? 00:02:02 /mnt/data/tool/miniconda3/envs/jupyterhub/bin/pyth
root 44 1 0 00:40 ? 00:00:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --ru
root 47 1 0 00:40 ? 00:00:00 /lib/systemd/systemd-logind
rstudio+ 50 1 0 00:40 ? 00:00:40 /usr/lib/rstudio-server/bin/rserver
root 53 1 0 00:40 ? 00:00:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 sta
root 54 1 0 00:40 pts/0 00:00:00 /sbin/agetty -o -p -- \u --noclear --keep-baud con
root 87 1 0 00:40 ? 00:00:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
root 175 43 0 00:41 ? 00:00:01 node /mnt/data/tool/miniconda3/envs/jupyterhub/bin
tyu-ukh+ 3655 43 0 00:47 ? 00:00:03 /mnt/data/tool/miniconda3/envs/jupyterhub/bin/pyth
tyu-ukh+ 3951 3655 0 00:48 pts/3 00:00:00 /bin/bash -l
root 12969 53 0 08:36 ? 00:00:00 sshd: tyu-ukhx1xmi [priv]
tyu-ukh+ 12972 1 0 08:36 ? 00:00:00 /lib/systemd/systemd --user
tyu-ukh+ 12973 12972 0 08:36 ? 00:00:00 (sd-pam)
tyu-ukh+ 12987 12969 0 08:36 ? 00:00:00 sshd: tyu-ukhx1xmi@pts/1,pts/2
tyu-ukh+ 12988 12987 0 08:36 pts/1 00:00:00 -bash
tyu-ukh+ 12992 12987 0 08:36 pts/2 00:00:00 -bash
tyu-ukh+ 12993 12987 0 08:36 ? 00:00:00 /usr/lib/openssh/sftp-server
tyu-ukh+ 13030 12992 0 08:36 pts/2 00:00:00 top
tyu-ukh+ 13563 12987 0 08:37 ? 00:00:00 bash -c export LANG="en_US";export LANGUAGE="en_US
tyu-ukh+ 13568 13563 0 08:37 ? 00:00:00 sleep 1
tyu-ukh+ 13569 12988 0 08:37 pts/1 00:00:00 ps -ef

tyu-ukhx1xmi@share-idebu4dywzt:~$ df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
overlay overlay 20971520 33888 20937632 1% /
tmpfs tmpfs 65536 0 65536 0% /dev
shm tmpfs 65536 0 65536 0% /dev/shm
192.168.101.71:/share/dir_idebu4dywzt nfs4 102626304 31415296 65951744 33% /mnt/data
/dev/sdb1 xfs 1048061956 237704416 810357540 23% /etc/hosts
tmpfs tmpfs 132092204 72 132092132 1% /run
tmpfs tmpfs 5120 0 5120 0% /run/lock
tmpfs tmpfs 10485760 0 10485760 0% /run/user/1000
tyu-ukhx1xmi@share-idebu4dywzt:~$
equator
20 小时 42 分钟前
@yanqiyu 哈哈哈,大佬牛逼啊,systemd-detect-virt 的输出是 docker ,怎么加一下您,请您喝咖啡
vsomeone
20 小时 42 分钟前
首先气球驱动也可以实现你说的这件事情,不一定会让虚机“可见的内存变小”。其次听你描述,感觉更像是租了个 LXC 容器给你
equator
20 小时 36 分钟前
@vsomeone 嗯呢,现在知道是 docker 虚拟化的啦,这里大佬好多,嘻嘻。
equator
19 小时 48 分钟前
还有个问题想请教下大佬们,测试 kvm 的时候,发现 kvm 虚拟机,即使虚拟机里面的进程退出了,不占用内存了,在宿主机上看到那个虚拟机的进程还是占用着内存没释放。是否有方法可以做到内存的及时回收呢,像容器一样,一个虚拟机不用内存了,内存马上就释放了,其它虚拟机就能使用这部分内存了。
elehayym1618
19 小时 26 分钟前
@equator 这个叫内存回收,virtio_balloon 你可以查一下,需要宿主机主动调用触发

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://ex.noerr.eu.org/t/1180284

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX