对KVM虚拟机的基本管理
管理kvm虚拟机有两种方法:一、图形化管理工具virt-manager,二、命令行工具virsh命令。这两种方法都是通过调用libvirt API来实现虚拟化管理。
使用virsh对KVM虚拟机的基本管理
1、查看虚拟机
virsh list显示运行的虚拟机。
[root@localhost images]# clear
[root@localhost images]# virsh list
Id Name State
----------------------------------------------------
7 rhel7-vm01 running
要查看所有虚拟机就要加参数 --all
[root@localhost images]# virsh list --all
Id Name State
----------------------------------------------------
7 rhel7-vm01 running
2、关闭虚拟机
virsh shutdown 虚拟机名称
[root@localhost images]# virsh shutdown rhel7-vm01
Domain rhel7-vm01 is being shutdown
3、启动虚拟机
virsh start 虚拟机名称
[root@localhost images]# virsh start rhel7-vm01
Domain rhel7-vm01 started
4、关闭虚拟机电源
virsh destroy 虚拟机名称,该命令是直接拔掉虚拟机电源进行关闭虚拟机。
5、挂起(暂停)虚拟机
virsh suspend 虚拟机名称
[root@localhost images]# virsh start rhel7-vm01
Domain rhel7-vm01 started
6、恢复虚拟机
virsh resume 虚拟机名称
[root@localhost images]# virsh resume rhel7-vm01
Domain rhel7-vm01 resumed
7、设置自动启动虚拟机
virsh autostart 虚拟机名称
[root@localhost images]# virsh autostart rhel7-vm01
Domain rhel7-vm01 marked as autostarted
8、禁止自启动虚拟机
virsh autostart 虚拟机 --disable
[root@localhost images]# virsh autostart rhel7-vm01 --disable
Domain rhel7-vm01 unmarked as autostarted
9、查看虚拟机信息
virsh dominfo 虚拟机名称
[root@localhost images]# virsh dominfo rhel7-vm01
Id: 8
Name: rhel7-vm01
UUID: c38aaa91-70ff-4a12-a77c-1cd482948f2e
OS Type: hvm
State: running
CPU(s): 1
CPU time: 51.7s
Max memory: 1048576 KiB
Used memory: 1048576 KiB
Persistent: yes
Autostart: enable
Managed save: no
Security model: selinux
Security DOI: 0
Security label: system_u:system_r:svirt_t:s0:c26,c812 (permissive)
10、删除虚拟机
virsh undefine 虚拟机名称
此时虚拟机是在虚拟机管理器里面找不到了。
11、删除虚拟机,并删除磁盘镜像文件
virsh undefine 虚拟机名称 --storage /var/lib/libvirt/images/虚拟机名称.qcow2
此时虚拟机是在虚拟机管理器里面找不到了,同时虚拟的磁盘镜像文件一并删除。
12、删除虚拟机,并删除所有磁盘文件。
virsh undefine 虚拟机名称 --remove-all-storage
13、显示虚拟机当前的配置文件。
virsh dumpxml 虚拟机名称
[root@localhost ~]# virsh dumpxml rhel7-vm01
<domain type='kvm' id='1'>
<name>rhel7-vm01</name>
<uuid>c38aaa91-70ff-4a12-a77c-1cd482948f2e</uuid>
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='static'>1</vcpu>
<resource>
<partition>/machine</partition>
</resource>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact'>
<model fallback='allow'>Broadwell-noTSX</model>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/rhel7-vm01.qcow2'/>
<backingStore/>
<target dev='vda' bus='virtio'/>
<alias name='virtio-disk0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<alias name='usb'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x7'/>
</controller>
<controller type='usb' index='0' mode