zoukankan      html  css  js  c++  java
  • 管理配置KVM,热添加、热迁移

    管理KVM虚拟机命令

    //虚拟机随着系统启动
    
    virsh autostart Name
    
    //关闭虚拟机随系统启动
    
    virsh autostart —disable Name
    
    //查看正在运行的KVM虚拟机
    
    [root@test01 ~]# virsh list
    
    //查看所有KVM虚拟机
    
    [root@test01 ~]# virsh list --all
    
    //启动KVM虚拟机
    
    [root@test01 ~]# virsh start name
    
    //关闭KVM虚拟机
    
    [root@test01 ~]# virsh shutdown name
    
    //强制关闭KVM虚拟机(断电)
    
    [root@test01 ~]# virsh destroy name
    
    //挂起KVM虚拟机
    
    [root@test01 ~]# virsh suspend name
    
    //恢复挂起的虚拟机
    
    [root@test01 ~]# virsh resume name
    
    //编辑KVM虚拟机的xml配置文件
    
    [root@test01 ~]# virsh edit name
    
    //删除创建的KVM虚拟机
    
    [root@test01 ~]# virsh undefine name

     

    KVM虚拟机热添加磁盘

    KVM虚拟机中有两种磁盘格式:

    raw格式: 直接占用物理磁盘, 写入快, 性能优, 但占用空间
    Qcow2格式: 使用多少占多少磁盘, 支持压缩、快照、镜像
    注意:raw不支持快照, Qcow2支持快照, 但两者文件类型是可以相互间转换

    无论磁盘是raw qcow2格式, 扩展思路如下

    1.新添加一块磁盘加入至需要扩容的虚拟主机
    2.使用lvm逻辑卷管理方式进行扩展

    1.查看当前KVM虚拟机所使用的虚拟磁盘
    
    [root@test01 ~]# virsh domblklist centos7Target     Source------------------------------------------------vda        /data/centos7.qcow2
    
    2.创建一块qcow2虚拟磁盘
    
    [root@test01 ~]# qemu-img create -f qcow2 /data/centos7-disk2.qcow2 10G
    
    3.在线添加虚拟磁盘
    
    //在线添加
    
    [root@test01 ~]# virsh attach-disk centos7 /data/centos7-disk2.qcow2 vdb --cache=none --subdriver=qcow2Disk attached successfully
    
    //查看新增磁盘状态
    
    [root@test01 ~]# virsh domblklist centos7Target     Source
    
    ------------------------------------------------
    
    vda        /data/centos7.qcow2
    
    vdb        /data/centos7-disk2.qcow2

     

    KVM虚拟机热添加网卡[root@test01 ~]# virsh      

                                            
    进入virsh命令行模式
    virsh # attach-interface cetnos7 --type bridge --source br0
         添加一块桥接网卡
    virsh # attach-interface cetnos7 --type bridge --source br0 --model virtio
    添加一块网卡,指定模式virtio网卡更快
    virsh # attach-interface cetnos7 --type bridge --source br0 --model virtio --config
    写进配置文件,永久生效
    virsh # domiflist cetnos7   
                                 查看虚拟机有多少块网卡
    virsh # domblklist cetnos7
         查看虚拟机有多少硬盘
    virsh # detach-interface cetnos7 --type bridge --mac 52:54:2f:6f:47:8f    分离网卡

    KVM虚拟机热添加内存

    添加内存立即生效,临时的
    virsh setmem test01 1024M 
    
    --config 不会立即不生效,在下一次启动生效
    virsh setmem test01 1024M --config
    
    修改最大内存限制
    \关机
    virsh shutdown ttest01
    
    \修改最大内存
    virsh setmaxmem test01 2048M
    
    \查看结果
    virsh dominfo test01 | grep -i max

    KVM虚拟机热添加CPU

     \cpu的在线个数调整为3个 
    [root@test01 ~]# virsh setvcpus CentOS-7.6-X86_64  3  --live      
    
    \写到配置文件 永久
    [root@test01 ~ ]# virsh setvcpus CentOS-7.6-X86_64  3  --config     
    
    \调整CPU最大值
    [root@test01 ~]# virsh setvcpus web01 --maximum 4 --config    
    
    #查看CPU相关信息
    [root@test01 ~]# cat /proc/interrupts  
    [root@test01 ~]# cat /proc/cpuinfo 
    [root@test01 ~]# lscpu 

    KVM虚拟机快照

    1.查看磁盘格式raw格式需要转换成qcow2
    
    [root@test01 ~]# qemu-img info /data/centos7.raw image: /data/centos7.rawfile format: rawvirtual size: 10G (10737418240 bytes)disk size: 1.2Gcluster_size: 65536Format specific information:    compat: 1.1
    
        lazy refcounts: false
    
    2.必须关闭虚拟机进行磁盘转换
    
    [root@test01 ~]# virsh shutdown centos7
    
    [root@test01 ~]# qemu-img convert -f raw /data/centos7.raw -O qcow2 /data/centos7.qcow2//convert 将磁盘文件转换为指定格式的文件//-f 指定需要转换文件的文件格式//-O 指定要转换的目标格式//转换完成后,将新生产一个目标映像文件,原文件保存
    
    3.修改KVM虚拟机配置文件配置文件
    
    [root@test01 ~]# virsh edit centos7
    
          <driver name='qemu' type='qcow2'/>
    
          <source file='/data/centos7.qcow2'/>
    
    4.对虚拟机当前进行快照拍摄
    
    [root@test01 ~]# virsh snapshot-create centos7    
    
    Domain snapshot 123456 created
    
    5.查看虚拟机快照
    
    [root@test01 ~]# virsh snapshot-list  centos7
    
     Name                 Creation Time             State
    
    ------------------------------------------------------------
    
     123456           2020-07-11 21:01:47 -0400 running
    
     
    
     //检查当前虚拟机最新快照版本
    
    [root@test01 ~]# virsh snapshot-current centos7|less
    
    //快照xml文件存放路径
    
    [root@test01 ~]# ls /var/lib/libvirt/qemu/snapshot/centos7/123456.xml  1234567.xml
    
    6.恢复快照
    
    [root@test01 ~]# virsh snapshot-revert centos7 123456
    
    //确认恢复版本正确
    
    [root@test01 ~]# virsh snapshot-current centos7|grep "123456"
    
      <name>123456</name>
    
      <creationTime>123456</creationTime>
    
    7.删除快照
    
    [root@test01 ~]# qemu-img info /data/centos7.qcow2 image: /data/centos7.qcow2file format: qcow2virtual size: 10G (10737418240 bytes)disk size: 1.8Gcluster_size: 65536Snapshot list:ID        TAG                 VM SIZE                DATE       VM CLOCK1         123456             282M 2018-04-11 21:01:48   00:16:25.4892         1234567             282M 2018-04-11 21:03:56   00:18:29.210Format specific information:
    
        compat: 1.1
    
        lazy refcounts: false
    
    //删除最近一个快照[root@test01 ~]# virsh snapshot-delete centos7 1234567Domain snapshot 1234567 deleted

     

    KVM虚拟机克隆

    //将test01克隆为test02
    virt-clone -o test01 -n test02 -f /kvm/store/test02.qcow2 

     

    KVM虚拟机热迁移

    可参考该篇

    https://blog.51cto.com/10802692/2414689

  • 相关阅读:
    最大公约数
    九宫格
    Hanoi双塔问题(简单的枚举)
    最高分
    盒子
    CodeForces Round #303 Div. 2
    关于“被密码保护”的文章
    【学习】组合数的递推公式
    [FZYZOJ 1821] 一道果题
    [FZYZOJ 1889] 厨房救济
  • 原文地址:https://www.cnblogs.com/yihr/p/13516368.html
Copyright © 2011-2022 走看看