virsh的名称的含义是virtualization shell。它有两种工作模式:
立即模式

[root@localhost kvm-vm]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7.0                      shut off
 -     ubuntu16.04                    shut off
 -     vmtest01                       shut off
 -     vncvm01                        shut off

交互模式

virsh # list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7.0                      shut off
 -     ubuntu16.04                    shut off
 -     vmtest01                       shut off
 -     vncvm01                        shut off

kvm配置文件存放路径为:

[root@localhost kvm-vm]# ls /etc/libvirt/qemu/
autostart      networks         vmtest01.xml     vncvm01.xml
centos7.0.xml  ubuntu16.04.xml  vncvm01-bak.xml

kvm基本功能管理

  1. 查看虚拟机状态:virsh list --all

    virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     -     centos7.0                      shut off
     -     ubuntu16.04                    shut off
     -     vmtest01                       shut off
     -     vncvm01                        shut off
    
  2. 虚拟机开机与关机

    开启:virsh start vmtest01 
    virsh list
    Id    Name                           State
    ----------------------------------------------------
    1     vmtest01                       running
    
    关闭:virsh shutdown vmtest01 
    Domain vmtest01 is being shutdown
    
  3. 强制关闭系统电源:virsh destroy vmtest01

    virsh list
    Id    Name                           State
    ----------------------------------------------------
    2     vmtest01                       running
     
    virsh destroy vmtest01 
    Domain vmtest01 destroyed
    virsh list --all
    Id    Name                           State
    ----------------------------------------------------
    -     vmtest01                       shut off
    
  4. 通过配置文件启动虚拟机

    virsh create /etc/libvirt/qemu/vmtest01.xml 
    Domain vmtest01 created from /etc/libvirt/qemu/vmtest01.xml
    virsh list 
    Id    Name                           State
    ----------------------------------------------------
    3     vmtest01                       running
    
  5. 挂起、恢复虚拟机

    挂起:virsh suspend vmtest01 
    Domain vmtest01 suspended
    virsh list --all
    Id    Name                           State
    ----------------------------------------------------
    3     vmtest01                       paused
     
    恢复:virsh resume vmtest01 
    Domain vmtest01 resumed
    virsh list --all
    Id    Name                           State
    ----------------------------------------------------
    3     vmtest01                       running
    
  6. 配置虚拟机实例伴随宿主机自动启动

    virsh autostart vmtest01 
    Domain vmtest01 marked as autostarted
    ll /etc/libvirt/qemu/autostart/
    total 0
    lrwxrwxrwx. 1 root root 30 Apr 28 21:25 vmtest01.xml -> /etc/libvirt/qemu/vmtest01.xml
    
    关闭自启动
    virsh autostart --disable vmtest01 
    Domain vmtest01 unmarked as autostarted
    
  7. 导出虚拟机配置

     virsh dumpxml vmtest01 > /etc/libvirt/qemu/vm.bak
    
  8. 删除虚拟机

    virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     -     centos7.0                      shut off
     -     ubuntu16.04                    shut off
     -     vmtest01                       shut off
     -     vncvm01                        shut off
    virsh undefine vmtest01 
    Domain vmtest01 has been undefined
    virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     -     centos7.0                      shut off
     -     ubuntu16.04                    shut off
     -     vncvm01                        shut off
    
  9. 恢复被删除的虚拟机

    #使用刚才备份的文件
    virsh define /etc/libvirt/qemu/vm.bak 
    Domain vmtest01 defined from /etc/libvirt/qemu/vm.bak
    virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     -     centos7.0                      shut off
     -     ubuntu16.04                    shut off
     -     vmtest01                       shut off
     -     vncvm01                        shut off
    

克隆虚拟机

 virt-clone -o vmtest01 -n vm_clone -f /kvm-vm/vm_clone.qcow2
 Clone 'vm_clone' created successfully
 参数说明:
 -o:指定被克隆虚拟机
 -n:克隆机名称
 -f:指定克隆机文件位置与名称
 virsh list --all
 Id    Name                           State
----------------------------------------------------
-     vm_clone                       shut off

虚拟机快照

virsh snapshot-create-as vmtest01 kz1
Domain snapshot kz1 created
参数说明:
snapshot-create:创建快照
-as:快照取名
virsh snapshot-list vmtest01 
Name                 Creation Time             State
------------------------------------------------------------
kz1                  2020-04-28 21:40:50 +0800 shutoff
##查看当前快照信息(部分截图)
virsh snapshot-current vmtest01 
<domainsnapshot>
  <name>kz1</name>			##快照名称
  <state>shutoff</state>
  <creationTime>1588081250</creationTime>
  <memory snapshot='no'/>

恢复快照
连接虚拟机后创建文件:

[root@localhost ~]# touch tt
[root@localhost ~]# echo "hello" > tt 
[root@localhost ~]# cat tt
hello

##恢复快照
virsh snapshot-revert vmtest01 kz1 
##验证
[root@localhost ~]# ls
anaconda-ks.cfg

##删除快照
virsh snapshot-list vmtest01 
Name                 Creation Time             State
------------------------------------------------------------
kz1                  2020-04-28 21:40:50 +0800 shutoff
virsh snapshot-delete vmtest01 kz1 
Domain snapshot kz1 deleted
virsh snapshot-list vmtest01 
Name                 Creation Time             State
------------------------------------------------------------
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐