文章最后更新时间:2025年06月13日
列表:
# 查看正在运行的虚拟机
[root@KVM opt]# virsh list
Id 名称 状态
----------------------------------------------------
1 centos7 running
# 查看所有虚拟机
[root@KVM opt]# virsh list --all
Id 名称 状态
----------------------------------------------------
- centos7 关闭
开机
[root@KVM opt]# virsh start centos7
域 centos7 已开始
[root@KVM opt]# virsh list
Id 名称 状态
----------------------------------------------------
2 centos7 running
# 关机
[root@KVM opt]# virsh shutdown centos7
域 centos7 被关闭
[root@KVM opt]# virsh list
Id 名称 状态
----------------------------------------------------
拔电源关机
[root@KVM opt]# virsh destroy centos7
域 centos7 被删除
[root@KVM opt]# virsh list
Id 名称 状态
----------------------------------------------------
[root@KVM opt]# virsh list --all
Id 名称 状态
----------------------------------------------------
- centos7 关闭
重启
[root@KVM opt]# virsh reboot centos7
域 centos7 正在被重新启动
查看虚拟机配置
virsh dumpxml centos7
导出配置
[root@KVM opt]# virsh dumpxml centos7 > centos7.xml
[root@KVM opt]# ls
Centos7.raw CentOS-7-x86_64-Minimal-2009.iso centos7.xml rh
只要有配置文件与磁盘文件,就可以完成迁移
删除虚拟机
[root@KVM opt]# virsh undefine centos7
域 centos7 已经被取消定义
[root@KVM opt]# virsh list --all
Id 名称 状态
----------------------------------------------------
恢复虚拟机
[root@KVM opt]# virsh define centos7.xml
定义域 centos7(从 centos7.xml)
[root@KVM opt]# virsh list --all
Id 名称 状态
----------------------------------------------------
- centos7 关闭
修改配置文件,迁移挂载磁盘
[root@KVM qemu]# virsh shutdown centos7
域 centos7 被关闭
[root@KVM qemu]# virsh edit centos7
编辑了域 centos7 XML 配置。
[root@KVM qemu]# mkdir /data
[root@KVM qemu]# mv /opt/Centos7.raw /data/
[root@KVM qemu]# virsh start centos7
域 centos7 已开始
[root@KVM qemu]# virsh dumpxml centos7 | grep raw
<driver name='qemu' type='raw'/>
<source file='/data/Centos7.raw'/>
修改虚拟主机名字
[root@KVM qemu]# virsh list
Id 名称 状态
----------------------------------------------------
8 centos7.2 running
9 centos7 running
[root@KVM qemu]# virsh shutdown centos7
域 centos7 被关闭
[root@KVM qemu]# virsh domrename centos7 web_mysql
Domain successfully renamed
[root@KVM qemu]# virsh list --all
Id 名称 状态
----------------------------------------------------
8 centos7.2 running
- web_mysql 关闭
虚拟机挂起
[root@KVM qemu]# virsh list
Id 名称 状态
----------------------------------------------------
8 centos7.2 running
10 web_mysql running
[root@KVM qemu]# virsh suspend web_mysql
域 web_mysql 被挂起
[root@KVM qemu]# virsh list
Id 名称 状态
----------------------------------------------------
8 centos7.2 running
10 web_mysql 暂停
虚拟机恢复
[root@KVM qemu]# virsh resume web_mysql
域 web_mysql 被重新恢复
[root@KVM qemu]# virsh list
Id 名称 状态
----------------------------------------------------
8 centos7.2 running
10 web_mysql running
查询vnc端口
[root@KVM ~]# virsh vncdisplay web_mysql
:0
虚拟机断电之后自动重启
root@KVM ~]# ls /etc/libvirt/qemu
centos7.2.xml networks web_mysql.xml
# 开机自启
[root@KVM ~]# virsh autostart web_mysql
域 web_mysql标记为自动开始
[root@KVM ~]# ls /etc/libvirt/qemu
autostart centos7.2.xml networks web_mysql.xml
查看那些虚拟机是自动重启
[root@KVM ~]# ls /etc/libvirt/qemu/autostart/
web_mysql.xml
关闭虚拟机自启
[root@KVM ~]# virsh autostart --disable web_mysql
域 web_mysql取消标记为自动开始
[root@KVM ~]# ls /etc/libvirt/qemu/autostart/
配置console
# 进入虚拟机修改内核参数,然后重启
[root@localhost ~]#grubby --update-kernel=ALL --args="console=ttyS0,115200n8"
[root@localhost ~]#reboot
[root@KVM ~]# virsh console web_mysql
连接到域 web_mysql
换码符为 ^]
# 回车进入
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64
localhost login: root
密码:
Last login: Tue Nov 22 17:08:03 on ttyS0
[root@localhost ~]# lscpu | grep -i kvm
超管理器厂商: KVM
文章版权声明:除非注明,否则均为柳三千运维录原创文章,转载或复制请以超链接形式并注明出处。