- 如果應(yīng)用程序/數(shù)據(jù)庫要求更多內(nèi)存宇智,則需要相應(yīng)地調(diào)整虛擬機(jī)的內(nèi)存限制。 當(dāng)您配置具有最大內(nèi)存限制的虛擬機(jī)時(shí)喂分,KVM支持動(dòng)態(tài)內(nèi)存添加。
- VM配置中有兩個(gè)部分蒲祈。
- 最大限度
- 當(dāng)前分配萝嘁。
- 在任何時(shí)間點(diǎn)梆掸,您不能超過使用
virsh setmem
命令的最大內(nèi)存限制牙言。 - 您需要關(guān)閉guest虛擬機(jī)才能執(zhí)行虛擬機(jī)最大內(nèi)存限制調(diào)整。
檢查VM內(nèi)存配置
- “memory unit”標(biāo)簽指定了vm可以使用的最大內(nèi)存咱枉;
- “currentMemory”標(biāo)簽定義了當(dāng)前vm配置的內(nèi)存;
root@test-kvm ~]# virsh dumpxml centos7 | grep -i memo
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
在運(yùn)行的VM上調(diào)整ram/內(nèi)存大小
使用virsh setmem
命令減少VM的當(dāng)前內(nèi)存
[root@test-kvm ~]# virsh console centos7
Connected to domain centos7
Escape character is ^]
[root@localhost /]# free -m
total used free shared buff/cache available
Mem: 1839 105 1608 8 125 1582
Swap: 1023 0 1023
[root@localhost /]#
[root@test-kvm ~]# virsh setmem centos7 512M
[root@test-kvm ~]# virsh dominfo centos7
Id: 7
Name: centos7
UUID: 6693189b-0a29-4225-b822-724001270bc0
OS Type: hvm
State: running
CPU(s): 1
CPU time: 205.4s
Max memory: 2097152 KiB
Used memory: 524288 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
[root@test-kvm ~]# vrish console centos7
-bash: vrish: command not found
[root@test-kvm ~]# virsh console centos7
Connected to domain centos7
Escape character is ^]
[root@localhost /]# free -m
total used free shared buff/cache available
Mem: 303 105 72 8 125 47
Swap: 1023 0 1023
注意設(shè)置當(dāng)前內(nèi)存的大小不能小于已經(jīng)使用的內(nèi)存大小欢伏。
使用virsh setmem
命令增加VM的當(dāng)前內(nèi)存基括。
[root@test-kvm ~]# virsh setmem centos7 2048M
[root@test-kvm ~]# virsh dominfo centos7
Id: 7
Name: centos7
UUID: 6693189b-0a29-4225-b822-724001270bc0
OS Type: hvm
State: running
CPU(s): 1
CPU time: 207.7s
Max memory: 2097152 KiB
Used memory: 2097152 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
[root@test-kvm ~]# virsh console centos7
Connected to domain centos7
Escape character is ^]
[root@localhost /]# free -m
total used free shared buff/cache available
Mem: 1839 105 1608 8 125 1582
Swap: 1023 0 1023
將內(nèi)存修改寫入配置文件
如果您想讓內(nèi)存修改永久生效,你必須將修改的內(nèi)容寫入VM的配置文件风皿,有兩種方法來實(shí)現(xiàn)這個(gè)想法:
- 在虛擬機(jī)關(guān)機(jī)情況下,使用“-config”選項(xiàng)
[root@test-kvm ~]# virsh destroy centos7
Domain centos7 destroyed
[root@test-kvm ~]# virsh setmem centos7 1024M --config
[root@test-kvm ~]# virsh dominfo centos7
Id: -
Name: centos7
UUID: 6693189b-0a29-4225-b822-724001270bc0
OS Type: hvm
State: shut off
CPU(s): 1
Max memory: 2097152 KiB
Used memory: 1048576 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
- 在虛擬機(jī)關(guān)機(jī)情況下桐款,使用
virsh edit
命令修改配置文件
[root@test-kvm ~]# virsh edit centos7
###########################################
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2048576</currentMemory>
###########################################
[root@test-kvm ~]# virsh dominfo centos7
Id: -
Name: centos7
UUID: 6693189b-0a29-4225-b822-724001270bc0
OS Type: hvm
State: shut off
CPU(s): 1
Max memory: 2097152 KiB
Used memory: 2048576 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
修改VM最大內(nèi)存限制。
如果想要修改VM的最大內(nèi)存的限制媳维,需要在VM停機(jī)下修改:
[root@test-kvm ~]# virsh list --all
Id Name State
----------------------------------------------------
1 generic running
- centos7 shut off
[root@test-kvm ~]# virsh setmaxmem centos7 4G
[root@test-kvm ~]# virsh dominfo centos7
Id: -
Name: centos7
UUID: 6693189b-0a29-4225-b822-724001270bc0
OS Type: hvm
State: shut off
CPU(s): 1
Max memory: 4194304 KiB
Used memory: 2048576 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0