2.kvm虚拟机的virsh日常管理和配置
孙富阳, 江湖人称没人称。多年互联网运维工作经验,曾负责过孙布斯大规模集群架构自动化运维管理工作。擅长Web集群架构与自动化运维,曾负责国内某大型博客网站运维工作。
1.列表list(--all)
[root@test ~]# virsh list ##查看运行中的虚拟机
Id Name State
----------------------------------------------------
[root@test ~]# virsh list --all##查看所有状态的虚拟机
Id Name State
----------------------------------------------------
- centos7 shut off
2.开关机,start和shutdown
[root@test ~]# virsh start centos7 ##开启centos7虚拟机
Domain centos7 started
[root@test ~]# virsh list --all
Id Name State
----------------------------------------------------
2 centos7 running
[root@test ~]# virsh shutdown centos7 ##关闭centos7虚拟机
Domain centos7 is being shutdown
[root@test ~]# virsh list --all
Id Name State
----------------------------------------------------
- centos7 shut off
[root@test ~]# virsh destroy centos7 ##强制关机(拔电源式关机)
Domain centos7 destroyed
##当正常关机无法关机时使用
[root@test ~]# virsh reboot centos7 ##重启虚拟机
Domain centos7 is being rebooted
3.导出虚拟机的配置文件dumpxml
[root@test ~]# virsh dumpxml centos7 > /opt/vm_centos7.xml
备份虚拟机的磁盘文件
[root@test ~]# cp /opt/centos2.raw /opt/centos2.raw.bk
4.删除虚拟机undefined
[root@test ~]# virsh undefine centos7 ##取消定义(需在关机后操作)
Domain centos7 has been undefined
[root@test ~]# virsh list –all(未关机时取消定义会发现虚拟机还活着)
Id Name State
----------------------------------------------------
4 centos7 running
[root@test ~]# virsh shutdown centos7 (关机后发现虚拟机没了)
Domain centos7 is being shutdown
[root@test ~]# virsh list --all
Id Name State
----------------------------------------------------
5.虚拟机的恢复define
[root@test ~]# virsh define /opt/vm_centos7.xml #使用xml文件恢复
Domain centos7 defined from /opt/vm_centos7.xml
[root@test ~]# virsh list --all
Id Name State
----------------------------------------------------
- centos7 shut off
6.修改配置edit(自带语法检查)
[root@test ~]# mv /opt/centos2.raw /opt/centos7.raw
[root@test ~]# virsh start centos7
error: Failed to start domain centos7
error: Cannot access storage file '/opt/centos2.raw': No such file or directory
[root@test ~]# virsh edit centos7
#相当于vim /etc/libvirt/qeum/centos7.xml,但是vim修改的文件需要重启libvirtd进程,edit编辑的只需要重启虚拟机即可生效
#重启指的是关机再开机,使用reboot不生效是因为进程一直存在
7.虚拟机重命名#domrename
[root@test ~]# virsh domrename centos7 web01#重命名
error: Requested operation is not valid: cannot rename active domain
###不能对运行中的虚拟机重命名
[root@test ~]# virsh shutdown centos7
Domain centos7 is being shutdown
[root@test ~]# virsh domrename centos7 web01
Domain successfully renamed
[root@test ~]# virsh list --all
Id Name State
----------------------------------------------------
- web01 shut off
8.挂起suspend和继续运行resume
##挂起
[root@test ~]# virsh suspend web01
Domain web01 suspended
[root@test ~]# virsh list --all
Id Name State
----------------------------------------------------
3 web01 paused
##继续运行
[root@test ~]# virsh resume web01
Domain web01 resumed
[root@test ~]# virsh list --all
Id Name State
----------------------------------------------------
3 web01 running
挂起后再次开机发现虚拟机时间已经不同步了,建议安装一个chronyd
9.查看虚拟机端口vncdisplay
[root@test ~]# virsh vncdisplay web01
:0
10.宿主机启动自动开启虚拟机(重启libvirtd自动开启虚拟机)autostart
[root@test ~]# virsh autostart web01
Domain web01 marked as autostarted
[root@test ~]# virsh list --all
Id Name State
----------------------------------------------------
1 web01 running
[root@test ~]# virsh shutdown web01
Domain web01 is being shutdown
[root@test ~]# virsh list --all
Id Name State
----------------------------------------------------
- web01 shut off
[root@test ~]# systemctl restart libvirtd
[root@test ~]# virsh list --all
Id Name State
----------------------------------------------------
1 web01 running
#开机自启原理
[root@test ~]# ll /etc/libvirt/qemu/autostart/
total 0
lrwxrwxrwx 1 root root 27 Jul 17 19:19 web01.xml -> /etc/libvirt/qemu/web01.xml
删除软连接虚拟机自启动自动失效
[root@test ~]# rm -f /etc/libvirt/qemu/autostart/*
[root@test ~]# virsh list --all
Id Name State
----------------------------------------------------
1 web01 running
[root@test ~]# virsh shutdown web01
Domain web01 is being shutdown
[root@test ~]# virsh list --all
Id Name State
----------------------------------------------------
- web01 shut off
[root@test ~]# systemctl restart libvirtd
[root@test ~]# virsh list --all
Id Name State
----------------------------------------------------
- web01 shut off
#同样的创建一个软连接相当于执行了
[root@test ~]# virsh autostart web01
除了查看软连接判断哪些虚拟机设置开机自启了,还可以通过下方命令查看
[root@test ~]# virsh list --autostart ##只能查看正在运行的虚拟机所以这里需要加--all
Id Name State
----------------------------------------------------
##当然取消开机自己启动除了删除软连接还可以使用如下命令
[root@test ~]# virsh autostart web01 --disable
Domain web01 unmarked as autostarted
11.console命令行控制台
#虚拟机上执行命令修改内核参数(注意是虚拟机不是宿主机)
[root@localhost ~]# grubby --update-kernel=ALL --args="console=ttyS0,115200n8"
[root@localhost ~]# reboot
宿主机执行
[root@test ~]# virsh console web01
执行命令后需要再按一次回车进入
要退出按crtl+d不行,得按crtl+]退出
12.使用虚拟机修改虚拟机密码set-user-password
##确保虚拟机的qemu-guest-agent.service进程已经启动
[root@localhost ~]# yum install qemu-guest-agent.x86_64
[root@localhost ~]# systemctl start qemu-guest-agent.service
[root@localhost ~]# systemctl enable qemu-guest-agent.service
##宿主机操作修改密码
[root@test ~]# virsh set-user-password --user root --password 123 web01
Password set successfully for root in web01
未经允许不得转载:孙某某的运维之路 » 2.kvm虚拟机的virsh日常管理和配置
评论已关闭