KVM虚拟机管理

1.查看主机列表

[root@kvm-server-01 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     centos-01                      running
 -     centos-02                      shut off

 #命令解释 
    virsh list    显示运行中的主机   
        --all       显示所有状态的主机,

2.启动和关闭主机

#启动命令
[root@kvm-server-01 ~]# virsh start centos-01

#关闭命令
[root@kvm-server-01 ~]# virsh shutdown centos-01

#强制关机
[root@kvm-server-01 ~]# virsh destroy centos-01

3.查看远程连接的主机端口

[root@kvm-server-01 ~]# virsh vncdisplay centos-01 
:0

# :0 对应的是 5900 端口
# :1 对应的是 5901 端口
以此类推

4.修改主机的配置

#  edit 具有语法检查功能
[root@kvm-server-01 ~]# virsh edit centos-01
Domain centos-01 XML configuration not changed.

# 通过编辑xml文件修改
[root@kvm-server-01 ~]# vim /etc/libvirt/qemu/centos-01.xml

6.显示虚拟机的基本状态

[root@kvm-server-01 ~]# virsh dominfo centos-01
Id:             6
Name:           centos-01
UUID:           8f4c8019-e1e8-491b-959f-d33bad7f1df7
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       30.3s
Max memory:     1048576 KiB
Used memory:    1048576 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: none
Security DOI:   0

7.调整已经创建的主机的内存数、CPU核心数

#调整主机内存大小,只能调小(可以运行中修改)
[root@kvm-server-01 ~]# virsh setmem centos-01 524288
[root@kvm-server-01 ~]# virsh dominfo centos-01 | grep memory
Max memory:     1048576 KiB
Used memory:    524288 KiB

#在关机状态下调整最大内存 
[root@kvm-server-01 ~]# virsh setmaxmem centos-01 2097152
[root@kvm-server-01 ~]# virsh dominfo centos-01 | grep "memory"
Max memory:     2097152 KiB
Used memory:    1048576 KiB

#从配置文件汇总修改配置信息(内存、CPU核数)
[root@kvm-server-01 ~]# virsh edit centos-01
  <memory unit='KiB'>1048576</memory>
    <vcpu placement='static'>1</vcpu>

8.导入、导出主机配置(做备份)

#导出配置,可关机导出,也可开机导出
[root@kvm-server-01 ~]# virsh dumpxml centos-01 > centos-01.bak
[root@kvm-server-01 ~]# ll centos-01.bak 
-rw-r--r-- 1 root root 5298 Jul 28 22:21 centos-01.bak

#删除主机
[root@kvm-server-01 ~]# virsh undefine centos-01
Domain centos-01 has been undefined
[root@kvm-server-01 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 8     centos-02                      running

#导入主机配置
[root@kvm-server-01 ~]# virsh define centos-01.bak
[root@kvm-server-01 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 8     centos-02                      running
 -     centos-01                      shut off

9.删除虚拟主机

[root@kvm-server-01 ~]# virsh undefine centos-01
Domain centos-01 has been undefined

10.重命名虚拟主机名

[root@kvm-server-01 ~]# virsh domrename centos-01 centos7.6
[root@kvm-server-01 ~]# mv /opt/centos-01.raw /opt/centos7.6.raw
[root@kvm-server-01 ~]# virsh edit centos
      <source file='/opt/centos7.6.raw'/>

11.虚拟主机开机自启、挂起

#使虚拟主机能够开机自启动,前提让libvirtd服务是开机启动
[root@kvm-server-01 ~]# systemctl enable libvirtd.service

# 虚拟主机开机自启
[root@kvm-server-01 ~]# virsh autostart centos7.6
# 查看主机信息
[root@kvm-server-01 ~]# virsh dominfo centos7.6 | grep Autostart
Autostart:      enable

# 取消开机自启
[root@kvm-server-01 ~]# virsh autostart --disable centos7.6
# 查看主机信息
[root@kvm-server-01 ~]# virsh dominfo centos7.6 | grep Autostart
Autostart:      disable

12.虚拟主机挂起、恢复

#挂起虚拟主机
[root@kvm-server-01 ~]# virsh suspend centos7.6

#恢复虚拟主机
[root@kvm-server-01 ~]# virsh resume centos7.6 

13.使用console连接虚拟主机

# 使用console前,先通过ssh连接或者vnc viewer连接进虚拟主机,执行以下命令
grubby --update-kernel=ALL --args="console=ttyS0,115200n8"

# 使用console登录虚拟主机 centos7.6
[root@kvm-server-01 ~]# virsh console centos7.6 
Connected to domain centos7.6
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-957.el7.x86_64 on an x86_64

localhost login: root
Password: 
Last login: Tue Jul 28 10:56:12 on ttyS0
[root@localhost ~]# 
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注