美文网首页
Learning KVM - part7 如何为VM添加和删除内

Learning KVM - part7 如何为VM添加和删除内

作者: drfung | 来源:发表于2017-06-16 20:35 被阅读352次
    • 如果应用程序/数据库要求更多内存,则需要相应地调整虚拟机的内存限制。 当您配置具有最大内存限制的虚拟机时,KVM支持动态内存添加。
    • VM配置中有两个部分。
      1. 最大限度
      2. 当前分配。
    • 在任何时间点,您不能超过使用virsh setmem命令的最大内存限制。
    • 您需要关闭guest虚拟机才能执行虚拟机最大内存限制调整。

    检查VM内存配置

    • “memory unit”标签指定了vm可以使用的最大内存;
    • “currentMemory”标签定义了当前vm配置的内存;
    root@test-kvm ~]# virsh dumpxml centos7 | grep -i memo
      <memory unit='KiB'>2097152</memory>
      <currentMemory unit='KiB'>2097152</currentMemory>
    

    在运行的VM上调整ram/内存大小

    使用virsh setmem命令减少VM的当前内存

    [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
    
    

    注意设置当前内存的大小不能小于已经使用的内存大小。

    使用virsh setmem命令增加VM的当前内存。

    [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
    

    将内存修改写入配置文件

    如果您想让内存修改永久生效,你必须将修改的内容写入VM的配置文件,有两种方法来实现这个想法:

    1. 在虚拟机关机情况下,使用“-config”选项
    [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
    
    1. 在虚拟机关机情况下,使用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最大内存限制。

    如果想要修改VM的最大内存的限制,需要在VM停机下修改:

    [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
    
    

    相关文章

      网友评论

          本文标题:Learning KVM - part7 如何为VM添加和删除内

          本文链接:https://www.haomeiwen.com/subject/pgbsqxtx.html