kvm管理和配置

KVM虚拟机的管理主要是通过virsh命令对虚拟机进行管理。

 

<![if !supportLists]>1. <![endif]>配置文件目录

虚拟机的配置文件放在/etc/libvirt/qemu/目录下,命名规则: 主机名.xml

[root@localhost work]# ll /etc/libvirt/qemu/

总用量 24

-rw-------. 1 root root 3028 12月 25 2016 API-Online-137.xml

drwx------. 3 root root 4096 5月   5 18:58 networks

-rw-------. 1 root root 3043 12月 20 2016 Nginx-backend01-132.xml

-rw-------. 1 root root 3040 12月 20 2016 Nginx-qian-01.xml

-rw-------. 1 root root 3016 12月 25 2016 PHP-H5-136.xml

-rw-------. 1 root root 3042 7月  18 17:05 test01.xml

<![if !supportLists]>2. <![endif]>查看虚拟机状态

[root@localhost work]# virsh list --all

 Id    名称                         状态

----------------------------------------------------

 -     API-Online-137                  关闭

 -     Nginx-backend01-132             关闭

 -     Nginx-qian-01                   关闭

 -     PHP-H5-136                     关闭

 -     test01                          关闭

 

<![if !supportLists]>3. <![endif]>KVM虚拟机开机

[root@localhost work]# virsh start  test01 

 test01 已开始

 

[root@localhost work]# virsh list --all    

 Id    名称                         状态

----------------------------------------------------

 6     test01                         running

 -     API-Online-137                 关闭

 -     Nginx-backend01-132            关闭

 -     Nginx-qian-01                  关闭

 -     PHP-H5-136                     关闭

 

<![if !supportLists]>4. <![endif]>KVM虚拟机关机或断电

默认情况下virsh工具不能对linux虚拟机进行关机操作,linux操作系统需要开启与启动acpid服务。在安装KVM linux虚拟机必须配置此服务。

# chkconfig acpid on   
# service acpid restart

 

virsh关机:

virsh shutdown test01

virsh强制关闭电源:

[root@localhost work]# virsh destroy test01

 test01 被删除

 

<![if !supportLists]>5. <![endif]>通过配置文件启动虚拟机

virsh create /etc/libvirt/qemu/test01.xml 

上边的文件是虚拟机的配置文件

 

<![if !supportLists]>6. <![endif]>配置虚拟机开机自启动

[root@localhost work]# ll /etc/libvirt/qemu/

总用量 28

-rw-------. 1 root root 3028 12月 25 2016 API-Online-137.xml

drwxr-xr-x. 2 root root 4096 7月  18 17:36 autostart

drwx------. 3 root root 4096 5月   5 18:58 networks

-rw-------. 1 root root 3043 12月 20 2016 Nginx-backend01-132.xml

-rw-------. 1 root root 3040 12月 20 2016 Nginx-qian-01.xml

-rw-------. 1 root root 3016 12月 25 2016 PHP-H5-136.xml

-rw-------. 1 root root 3042 7月  18 17:05 test01.xml

[root@localhost work]# ll /etc/libvirt/qemu/autostart/

总用量 0

lrwxrwxrwx. 1 root root 28 7月  18 17:36 test01.xml -> /etc/libvirt/qemu/test01.xml

 

autostart录是kvm虚拟机开机自启动目录,可以看到该目录中有KVM配置文件链接。

 

<![if !supportLists]>7. <![endif]>导出KVM虚拟机配置文件

[root@localhost work]# virsh dumpxml test01 > /etc/libvirt/qemu/tmp01.xml

[root@localhost work]# ll /etc/libvirt/qemu/tmp01.xml

-rw-r--r--. 1 root root 2818 7月  18 17:39 /etc/libvirt/qemu/tmp01.xml

 

KVM虚拟机配置文件可以通过这种方式进行备份。

 

<![if !supportLists]>8. <![endif]>添加与删除虚拟机

删除kvm虚拟机:

[root@localhost work]# virsh undefine test01

 test01 已经被取消定义


说明:该命令只是删除wintest01的配置文件,并不删除虚拟磁盘文件。如下所示。

[root@localhost work]# ll /etc/libvirt/qemu/

总用量 28

-rw-------. 1 root root 3028 12月 25 2016 API-Online-137.xml

drwxr-xr-x. 2 root root 4096 7月  18 17:42 autostart

drwx------. 3 root root 4096 5月   5 18:58 networks

-rw-------. 1 root root 3043 12月 20 2016 Nginx-backend01-132.xml

-rw-------. 1 root root 3040 12月 20 2016 Nginx-qian-01.xml

-rw-------. 1 root root 3016 12月 25 2016 PHP-H5-136.xml

-rw-r--r--. 1 root root 2818 7月  18 17:39 tmp01.xml

 

重新定义虚拟机配置文件

通过导出备份的配置文件恢复原KVM虚拟机的定义,并重新定义虚拟机。

[root@localhost work]# mv /etc/libvirt/qemu/tmp01.xml  /etc/libvirt/qemu/test01.xml

[root@localhost work]# virsh define /etc/libvirt/qemu/test01.xml 

定义域 test01(从 /etc/libvirt/qemu/test01.xml

 

[root@localhost work]# virsh list --all

 Id    名称                         状态

----------------------------------------------------

 -     API-Online-137                 关闭

 -     Nginx-backend01-132            关闭

 -     Nginx-qian-01                  关闭

 -     PHP-H5-136                     关闭

 -     test01                         关闭

 

 

 

 

 

原文地址:https://www.cnblogs.com/yangruizeng/p/7510527.html