KVM virsh命令

@查看虚拟机
virsh list  查看运行的虚拟机
virsh list --all  查看所有定义了的虚拟机,包括没有运行的
说明:在所有需要名称作为参数的virsh命令中,可以用ID号代替虚拟机的名称,但ID号是不固定的,每次启动虚拟机会改变,并且只有运行中的虚拟机有ID。

ps -ef | grep -v grep | grep qemu  查看当前Qemu进程。
说明:每个虚拟机都是一个Qemu进程,当虚拟机遇到问题无法关闭时,可以通过kill的方式干掉它。

@virt-manager打开管理控制台

@virsh命令创建、启动、关机、销毁虚拟机
(定义) virsh define <vmname>.xml -> 如果xml文件不在当前路径下,xml文件要写全路径

(启动) virsh start <vmname>      -> <vmname>为虚拟机xml配置文件中虚拟机的名字<name>vm001</name>

(停止) virsh shutdown <vmname>   -> 此方法为正常关机方法,需要一段才能关机

(下电) virsh destroy <vmname>    -> 此方法为暴力下电,虚拟机立即关闭

(删除) virsh undefine <vmname>   -> 关闭了的虚拟机,只是不在运行状态而已,通过virsh undefine xxx就能从virsh列表里面将其删除,undefine命令不会删除镜像文件和xml文件。运行状态的虚拟机是不能删除的。

(临时起虚拟机) virsh create <vmname>.xml -> 此方法为方便开发调试等临时需求,不会持久化,虚拟机关机后就消失了,不推荐生产系统使用。

(设置虚拟机跟随系统自启) virsh autostart <vmname>

(关闭虚拟机自启) virsh autostart --disable <vmname>

[例] virsh相关命令举例

1)virsh list 查看运行的虚拟机

[root@localhost ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 8     vm002                          running
 9     vm003                          running
 10    vm004                          running
[root@localhost ~]#

2)virsh list --all 查看所有定义了的虚拟机

[root@localhost ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 8     vm002                          running
 9     vm003                          running
 10    vm004                          running
[root@localhost ~]#

3)virsh shutdown <vmname>  正常关机方法,需要一段时间才能关机

[root@localhost ~]# virsh shutdown vm002
Domain vm002 is being shutdown

[root@localhost ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 9     vm003                          running
 10    vm004                          running
 -     vm002                          shut off

[root@localhost ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 9     vm003                          running
 10    vm004                          running
[root@localhost ~]#

4)virsh start <vmname> 启动虚拟机,其中vmname是xml配置文件中虚拟机的名字<name>vm001</name>

[root@localhost ~]# virsh start vm002
Domain vm002 started

[root@localhost ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 9     vm003                          running
 10    vm004                          running
 11    vm002                          running

[root@localhost ~]# 

5)virsh destroy <vmname>  暴力下电,虚拟机立即关闭

[root@localhost ~]# virsh destroy vm002
Domain vm002 destroyed

[root@localhost ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 9     vm003                          running
 10    vm004                          running

[root@localhost ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 9     vm003                          running
 10    vm004                          running
 -     vm002                          shut off

[root@localhost ~]# virsh start vm002
Domain vm002 started
[root@localhost ~]#

5)关闭了的虚拟机,只是不在运行状态而已,通过virsh undefine <vmname> 将其从virsh列表中删除

说明:undefine命令不会删除镜像文件和xml文件,不会删除运行状态的虚拟机

[root@localhost ~]# virsh undefine vm002
Domain vm002 has been undefined

[root@localhost ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 9     vm003                          running
 10    vm004                          running
 12    vm002                          running

[root@localhost ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 9     vm003                          running
 10    vm004                          running
 12    vm002                          running

[root@localhost ~]# virsh shutdown vm002
Domain vm002 is being shutdown

[root@localhost ~]# virsh undefine vm002
error: failed to get domain 'vm002'
error: Domain not found: no domain with matching name 'vm002'

[root@localhost ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 9     vm003                          running
 10    vm004                          running

[root@localhost ~]#

6)virsh create <vmname>.xml  该方法创建虚拟机不会持久化,关机后就消失了,不推荐生产系统使用

[root@localhost ~]# virsh create vm002.xml
Domain vm002 created from vm002.xml

[root@localhost ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 9     vm003                          running
 10    vm004                          running
 13    vm002                          running

[root@localhost ~]# virsh shutdown vm002.xml
error: failed to get domain 'vm002.xml'
error: Domain not found: no domain with matching name 'vm002.xml'

[root@localhost ~]# virsh destroy vm002.xml
error: failed to get domain 'vm002.xml'
error: Domain not found: no domain with matching name 'vm002.xml'

[root@localhost ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 9     vm003                          running
 10    vm004                          running
 13    vm002                          running

[root@localhost ~]# virsh undefine vm002
error: Failed to undefine domain vm002
error: Requested operation is not valid: cannot undefine transient domain

[root@localhost ~]# ssh [email protected]
The authenticity of host '10.123.123.123 (10.123.123.123)' can't be established.
ECDSA key fingerprint is SHA256:N9O+aFQrSPZvdkYyuLBXY9d169fXKYBEDZm3TXYVeSM.
ECDSA key fingerprint is MD5:04:92:1a:95:9c:96:00:11:42:12:33:94:6f:b8:a3:e4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.123.123.123' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Tue Jun 16 11:22:17 2020
[root@vm002 ~]# shutdown -h now
Connection to 10.123.123.123 closed by remote host.
Connection to 10.123.123.123 closed.
[root@localhost ~]#
[root@localhost ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 9     vm003                          running
 10    vm004                          running

[root@localhost ~]#

7)virsh define <vmname>.xml 创建虚拟机,如果xml文件不在当前路径下,要写全路径

[root@localhost ~]# virsh define vm002.xml
Domain vm002 defined from vm002.xml

[root@localhost ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 9     vm003                          running
 10    vm004                          running
 -     vm002                          shut off

[root@localhost ~]# virsh start vm002
Domain vm002 started

[root@localhost ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     vm001                          running
 9     vm003                          running
 10    vm004                          running
 14    vm002                          running

[root@localhost ~]#


@KVM删除虚拟机具体步骤
virsh list  查看要删除的虚拟机
virsh destroy <vm to del>   强制停止虚拟机
virsh undefine <vm to del>  删除虚拟机
updatedb  更新当前文件
locate <vm to del>  查找包含要删除虚拟机的所有内容
rm -rf xxx   删除相关虚拟机的一切内容
updatedb   更新当前文件
locate <vm to del>  确保要删除虚拟机的相关文件彻底删除

@虚拟机XML文件

<!-- kvm guest 定义开始 -->
<domain type='kvm'>
  <!-- guest的short name,由字母和数字组成,不能包含空格 -->
  <name>vm001</name>
  <!-- uuid,由命令行工具 uuidgen生成 -->
  <uuid>8ef72578-ca8a-4bd9-a6c4-ec4d5af7e770</uuid>
  <!-- 在不reboot guest的情况下,guset可以使用的最大内存,以KB为单位 -->
  <memory unit='KiB'>24576000</memory>
  <!-- uguest启动时内存,可以通过virsh setmem来调整内存,但不能大于最大可使用内存。 -->
  <currentMemory unit='KiB'>24576000</currentMemory>
  <!-- 分配的虚拟cpu -->
  <vcpu placement='static'>4</vcpu>
  <!-- 关于OS,架构: i686、x86_64; machine: 宿主机的操作系统
       boot:指定启动设备,可以重复多行,指定不同的值,作为一个启动设备列表。 -->
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <!-- 处理器特性 -->
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='host-model' check='partial'>
    <model fallback='allow'/>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <!-- 定义了在kvm环境中power off、reboot、crash时的默认动作分别为destroy和restart。
       其它允许的动作包括: preserve,rename-restart。 -->
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <!-- 设备定义开始  -->
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <!-- 用于kvm存储的文件。使用qemu-img命令创建该文件,kvm image的默认目录为:/var/lib/libvirt/images/-->
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/vm001.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </controller>
    <!-- 使用网桥类型,确保每个kvm guest的mac地址唯一 -->
    <interface type='bridge'>
      <mac address='00:50:56:00:00:0b'/>
      <source bridge='br0'/>
      <model type='e1000'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <interface type='bridge'>
      <mac address='52:54:00:7d:af:4c'/>
      <source bridge='br0'/>
      <model type='e1000'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      </target>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <channel type='spicevmc'>
      <target type='virtio' name='com.redhat.spice.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <input type='tablet' bus='usb'>
      <address type='usb' bus='0' port='1'/>
    </input>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='spice' autoport='yes'>
      <listen type='address'/>
      <image compression='off'/>
    </graphics>
    <sound model='ich6'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </sound>
    <video>
      <model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <redirdev bus='usb' type='spicevmc'>
      <address type='usb' bus='0' port='2'/>
    </redirdev>
    <redirdev bus='usb' type='spicevmc'>
      <address type='usb' bus='0' port='3'/>
    </redirdev>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
    </memballoon>
  </devices>
</domain>

创建启动虚拟机后可以找到以下文件

[root@localhost ~]# locate vm001
/etc/libvirt/qemu/vm001.xml
/var/vm001
/var/lib/libvirt/images/vm001.qcow2
/var/lib/libvirt/qemu/domain-9-vm001
/var/lib/libvirt/qemu/channel/target/domain-9-vm001
/var/lib/libvirt/qemu/domain-9-vm001/monitor.sock
/var/log/libvirt/qemu/vm001.log
[root@localhost ~]#

@关于网卡类型

参考:KVM虚拟机网络配置 Bridge方式,NAT方式  https://blog.csdn.net/hzhsan/article/details/44098537/
ovirt几种网卡(e1000, rtl8139,virtio)https://blog.csdn.net/gyunling/article/details/104748662/
CentOS 7下的KVM网卡配置为千兆网卡 https://blog.csdn.net/weixin_33961829/article/details/86265094

@virt-install

参考:virt-install命令---详解  https://www.cnblogs.com/saryli/p/11827903.html

@virt-clone

参考:CentOS7.1 KVM虚拟化之虚拟机克隆 https://blog.csdn.net/hnhuangyiyang/article/details/50885755
关于kvm虚拟机的克隆方法总结 https://www.cnblogs.com/5201351/p/4461000.html

参考资料:

virsh常用命令 https://blog.csdn.net/jiahaojie1984/article/details/23425695
KVM虚拟机的xml配置文件 https://blog.csdn.net/liukuan73/article/details/46049579

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章