文章目录[隐藏]
1.显示虚拟机列表
#显示所有虚拟机 [root@kvm /opt]# virsh list --all Id Name State ---------------------------------------------------- 7 db01 running - centos6 shut off #显示正在运行的虚拟机 [root@kvm /opt]# virsh list Id Name State ---------------------------------------------------- 7 db01 running
2.启动、关闭虚拟机
#启动虚拟机 [root@kvm /opt]# virsh start centos6 Domain centos6 started #关闭虚拟机 [root@kvm /opt]# virsh shutdown centos6 Domain centos6 is being shutdown #强制关闭虚拟机(相当于拔电源) [root@kvm /opt]# virsh destroy centos6 Domain centos6 destroyed
3.显示主机信息
[root@kvm /opt]# virsh dominfo centos6 Id: - Name: centos6 UUID: 9a58937a-e12d-4006-90aa-b9b13f1f3728 OS Type: hvm State: shut off CPU(s): 1 Max memory: 1048576 KiB Used memory: 1048576 KiB Persistent: yes Autostart: disable Managed save: no Security model: none Security DOI: 0
4.显示主机对应的端口
[root@kvm /opt]# virsh vncdisplay db01 :0 [root@kvm /opt]# virsh vncdisplay web01 :1 [root@kvm /opt]# virsh vncdisplay web02 :2 [root@kvm /opt]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN 15822/qemu-kvm #db01 tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN 17311/qemu-kvm #web01 tcp 0 0 0.0.0.0:5902 0.0.0.0:* LISTEN 17351/qemu-kvm #web02
5.调内存
#将内存进行调小 [root@kvm01-server /opt]# virsh setmem web01 524288 [root@kvm01-server /opt]# virsh dominfo web01 Id: 4 Name: web01 UUID: 71348068-6685-4134-9fca-15e8c686dcfc OS Type: hvm State: running CPU(s): 1 CPU time: 21.8s Max memory: 1048576 KiB Used memory: 524288 KiB Persistent: yes Autostart: disable Managed save: no Security model: none Security DOI: 0 #在关机状态下调整最大内存 [root@kvm01-server /opt]# virsh setmaxmem web01 2097152 [root@kvm01-server /opt]# virsh dominfo web01 Id: - Name: web01 UUID: 71348068-6685-4134-9fca-15e8c686dcfc OS Type: hvm State: shut off CPU(s): 1 Max memory: 2097152 KiB Used memory: 1048576 KiB Persistent: yes Autostart: disable Managed save: no Security model: none Security DOI: 0
6.导入、导出配置
#导出配置 [root@kvm /opt]# virsh dumpxml web01 >web01_bak.xml #导入配置 [root@kvm /opt]# virsh define web01_bak.xml Domain web01 defined from web01_bak.xml
7.重命名虚拟机名称
#重命名虚拟主机名 关机状态下 [root@kvm01-server /opt]# virsh shutdown web01 Domain web01 is being shutdown [root@kvm01-server /opt]# virsh domrename web01 lx Domain successfully renamed [root@kvm01-server /opt]# virsh list --all Id Name State ---------------------------------------------------- - lx shut off
8.开机自启动
#要想虚拟机能够开机自启动,必须你的libvirtd服务是开机启动,开机自启动就是基于一种软链接的方式 [root@kvm01-server /opt]# virsh dominfo web01 | grep start Autostart: disable [root@kvm01-server /opt]# systemctl enable libvirtd [root@kvm01-server /opt]# virsh autostart web01 Domain web01 marked as autostarted [root@kvm01-server /opt]# virsh dominfo web01 | grep start Autostart: enable [root@kvm01-server /opt]# ll /etc/libvirt/qemu total 8 drwxr-xr-x 2 root root 21 Jul 28 18:11 autostart drwx------ 3 root root 42 Jul 28 16:09 networks -rw------- 1 root root 4403 Jul 28 18:08 web01.xml [root@kvm01-server /opt]# ll /etc/libvirt/qemu/autostart/ total 0 lrwxrwxrwx 1 root root 25 Jul 28 18:11 web01.xml -> /etc/libvirt/qemu/web01.xml
9.挂起与恢复
#挂起 [root@kvm01-server /opt]# virsh suspend web01 Domain web01 suspended #恢复 [root@kvm01-server /opt]# virsh resume web01 Domain web01 resumed
10.虚拟机的console登录
[root@kvm01-server /opt]# ssh root@192.168.122.158 root@192.168.122.158 password: Last login: Tue Jul 28 18:09:14 2020 [root@web01 ~]# grubby --update-kernel=ALL --args="console=ttyS0,115200n8" [root@web01 ~]# reboot Connection to 192.168.122.158 closed by remote host. Connection to 192.168.122.158 closed. [root@kvm01-server /opt]# [root@kvm01-server /opt]# virsh console web01 Connected to domain web01 Escape character is ^] CentOS Linux 7 (Core) Kernel 3.10.0-957.el7.x86_64 on an x86_64 kvm login: root Password: Last login: Tue Jul 28 18:51:55 from gateway [root@web01 ~]#