@查看虚拟机
virsh list 查看运行的虚拟机
virsh list --all 查看所有定义了的虚拟机,包括没有运行的
说明:在所有需要名称作为参数的virsh命令中,可以用ID号代替虚拟机的名称,但ID号是不固定的,每次启动虚拟机会改变,并且只有运行中的虚拟机有ID。
ps -ef | grep -v grep | grep qemu 查看当前Qemu进程。
说明:每个虚拟机都是一个Qemu进程,当虚拟机遇到问题无法关闭时,可以通过kill的方式干掉它。
@virt-manager打开管理控制台
@virsh命令创建、启动、关机、销毁虚拟机
(定义) virsh define
(启动) virsh start
(停止) virsh shutdown
(下电) virsh destroy
(删除) virsh undefine
(临时起虚拟机) virsh create
(设置虚拟机跟随系统自启) virsh autostart
(关闭虚拟机自启) virsh autostart --disable
[例] 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
[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
[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
[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
说明: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
[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
[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
virsh undefine
updatedb 更新当前文件
locate
rm -rf xxx 删除相关虚拟机的一切内容
updatedb 更新当前文件
locate
@虚拟机XML文件
vm001
8ef72578-ca8a-4bd9-a6c4-ec4d5af7e770
24576000
24576000
4
hvm
destroy
restart
destroy
/usr/libexec/qemu-kvm
创建启动虚拟机后可以找到以下文件
[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