让KVM实例在Host OS启动时,KVM里面的虚拟机也跟着自动启动,不需要我们人工的参与。

在KVM服务器启动后或者libvirtd重新启动后,KVM虚拟机自动启动,运行以下步骤:

(1)开启VM的自动启动
root@kvm:~# virsh autostart kvm1
Domain kvm1 marked as autostarted
(2)获得实例的详细信息
root@kvm:~# virsh dominfo kvm1
Id: 31
Name: kvm1
UUID: 6ad84d8a-229d-d1f6-ecfc-d29a25fcfa03
OS Type: hvm
State: running
CPU(s): 2
CPU time: 10.9s
Max memory: 2097152 KiB
Used memory: 1048576 KiB
Persistent: yes
Autostart: enable
Managed save: no
Security model: none
Security DOI: 0
(3)停止运行的实例并确保它是处理shut off的状态
root@kvm:~# virsh destroy kvm1
Domain kvm1 destroyed

root@kvm:~# virsh list --all
 Id  Name  State
----------------------------------------------------
 -   kvm1  shut off
(4)停止libvirt的进程,并确保它没有运行。
root@kvm:~# /etc/init.d/libvirt-bin stop
libvirt-bin stop/waiting
root@kvm:~# pgrep -lfa libvirtd
(5)开启libvirt的进程
root@kvm:~# /etc/init.d/libvirt-bin start
libvirt-bin start/running, process 6639
(6)列出运行的实例
root@kvm:~# virsh list --all
 Id Name State
----------------------------------------------------
 2  kvm1 running
(7)关闭autostart选项
root@kvm:~# virsh autostart kvm1 --disable
Domain kvm1 unmarked as autostarted
(8)确保我们的改变是否生效
root@kvm:~# virsh dominfo kvm1 | grep -i autostart
Autostart: disable