作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.修改CPU的内存上限
1.查看当前CPU信息
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
3 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
[root@yinzhengjie-kvm01 ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 2
On-line CPU(s) list: 0,1
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 2
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 61
Model name: Intel Core Processor (Broadwell, IBRS)
Stepping: 2
CPU MHz: 2208.000
BogoMIPS: 4416.00
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 4096K
NUMA node0 CPU(s): 0,1
Flags: fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm 3dnowprefetch invpcid_single ssbd ibrs ibpb fsgsbase bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt md_clear spec_ctrl
[root@yinzhengjie-kvm01 ~]#
2.修改cpu的最大核心数
[root@yinzhengjie-kvm189 ~]# virsh setvcpus yinzhengjie-kvm01 --config --maximum 10
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh edit yinzhengjie-kvm01
<domain type='kvm'>
<name>yinzhengjie-kvm01</name>
...
<vcpu placement='static' current='2'>10</vcpu>
...
温馨提示:
建议大家在虚拟机关机的时候调整CPU的最大值,否则可能配置不会立刻生效哟~
3.重启虚拟机使得配置生效
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
3 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh destroy yinzhengjie-kvm01
Domain yinzhengjie-kvm01 destroyed
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
- yinzhengjie-kvm01 shut off
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh start yinzhengjie-kvm01
Domain yinzhengjie-kvm01 started
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
4 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
4.修改当前的cpu核心数量
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
4 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh setvcpus yinzhengjie-kvm01 4
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
[root@yinzhengjie-kvm01 ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 4
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 61
Model name: Intel Core Processor (Broadwell, IBRS)
Stepping: 2
CPU MHz: 2208.000
BogoMIPS: 4416.00
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 4096K
NUMA node0 CPU(s): 0-3
Flags: fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm 3dnowprefetch invpcid_single ssbd ibrs ibpb fsgsbase bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt md_clear spec_ctrl
[root@yinzhengjie-kvm01 ~]#
5.再次调整CPU核心数量
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
[root@yinzhengjie-kvm01 ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 8
On-line CPU(s) list: 0-7
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 8
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 61
Model name: Intel Core Processor (Broadwell, IBRS)
Stepping: 2
CPU MHz: 2208.000
BogoMIPS: 4416.00
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 4096K
NUMA node0 CPU(s): 0-7
Flags: fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm 3dnowprefetch invpcid_single ssbd ibrs ibpb fsgsbase bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt md_clear spec_ctrl
[root@yinzhengjie-kvm01 ~]#
二.其他注意事项
1.永久性修改cpu核心配置
- 热添加CPU核心数量
virsh setvcpus yinzhengjie-kvm01 8
- 永久添加CPU核心数量
virsh setvcpus yinzhengjie-kvm01 8 --config
- 调整虚拟机支持CPU的最大值
virsh setvcpus yinzhengjie-kvm01 --maximum 20 --config
2.不支持热修改CPU数量
[root@yinzhengjie-kvm189 ~]# virsh setvcpus yinzhengjie-kvm01 9 # 可以调大CPU数量
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh setvcpus yinzhengjie-kvm01 5 # 但是不可以调小CPU数量
error: unsupported configuration: failed to find appropriate hotpluggable vcpus to reach the desired target vcpu count
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh setvcpus yinzhengjie-kvm01 5 --config # 永久修改CPU数量需要重启虚拟机才能生效
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
4 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
[root@yinzhengjie-kvm01 ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 9
On-line CPU(s) list: 0-8
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 9
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 61
Model name: Intel Core Processor (Broadwell, IBRS)
Stepping: 2
CPU MHz: 2208.000
BogoMIPS: 4416.00
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 4096K
NUMA node0 CPU(s): 0-8
Flags: fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm 3dnowprefetch invpcid_single ssbd ibrs ibpb fsgsbase bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt md_clear spec_ctrl
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh reboot yinzhengjie-kvm01
Domain yinzhengjie-kvm01 is being rebooted
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh destroy yinzhengjie-kvm01
Domain yinzhengjie-kvm01 destroyed
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh start yinzhengjie-kvm01
Domain yinzhengjie-kvm01 started
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64
yinzhengjie-kvm01 login: root
Password:
Last login: Wed Apr 10 21:42:52 on ttyS0
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 5
On-line CPU(s) list: 0-4
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 5
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 61
Model name: Intel Core Processor (Broadwell, IBRS)
Stepping: 2
CPU MHz: 2208.000
BogoMIPS: 4416.00
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 4096K
NUMA node0 CPU(s): 0-4
Flags: fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm 3dnowprefetch invpcid_single ssbd ibrs ibpb fsgsbase bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt md_clear spec_ctrl
[root@yinzhengjie-kvm01 ~]#