4.KVM虚拟机管理和配置
4.1KVM虚拟机管理命令
# virsh list 列出正在运⾏的虚拟机
[root@localhost opt]# virsh list
Id Name State
----------------------------------------------------
1 web02 running
2 web01 running
# virsh list --all 列出所有虚拟机(关闭和运⾏的虚拟机)
[root@localhost opt]# virsh list --all
Id Name State
----------------------------------------------------
1 web02 running
2 web01 running
# virsh start 虚拟机名称 启动虚拟机
[root@localhost opt]# virsh start web01
Domain web01 started
# virsh dominfo 虚拟机名称 查看虚拟机配置信息
[root@localhost opt]# virsh dominfo web02
Id: 1
Name: web02
UUID: e2835286-4a2b-4812-a970-b2fa0f85d5b3
OS Type: hvm
State: running
CPU(s): 1
CPU time: 52.8s
Max memory: 1048576 KiB
Used memory: 1048576 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: selinux
Security DOI: 0
Security label: system_u:system_r:svirt_t:s0:c546,c552 (enforcing)
# virsh domblklist 虚拟机名称 查看虚拟机磁盘信息
[root@localhost opt]# virsh domblklist web02
Target Source
------------------------------------------------
vda /opt/web01-vda.raw
hda /opt/CentOS-7-x86_64-DVD-1804.iso
# virsh domiflist 虚拟机名称 查看虚拟机⽹卡
[root@localhost opt]# virsh domiflist web02
Interface Type Source Model MAC
-------------------------------------------------------
vnet0 network default virtio 52:54:00:43:77:f4
# virsh shutdown 虚拟机名称 关闭虚拟机
[root@localhost opt]# virsh shutdown web02
Domain web02 is being shutdown
# virsh destroy 虚拟机名称 拔电源关机(很少⽤)
[root@localhost opt]# virsh destroy web01
Domain web01 destroyed
# virsh reboot 虚拟机名称 重启虚拟机
[root@localhost opt]# virsh reboot web02
error: Failed to reboot domain web02
error: Requested operation is not valid: domain is not running
# virsh dumpxml 虚拟机名称 以xml格式查看虚拟机
[root@localhost opt]# virsh dumpxml web02
<domain type='kvm'>
<name>web02</name>
<uuid>e2835286-4a2b-4812-a970-b2fa0f85d5b3</uuid>
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='static'>1</vcpu>
<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' check='partial'>
<model fallback='allow'>Broadwell-noTSX-IBRS</model>
<feature policy='require' name='md-clear'/>
<feature policy='require' name='spec-ctrl'/>
<feature policy='require' name='ssbd'/>
</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>destroy</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='raw'/>
<source file='/opt/web01-vda.raw'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='hda' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci2'>
<master startport='2'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci3'>
<master startport='4'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</controller>
<interface type='network'>
<mac address='52:54:00:43:77:f4'/>
<source network='default'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
<address type='virtio-serial' controller='0' bus='0' port='1'/>
</channel>
<input type='tablet' bus='usb'>
<address type='usb' bus='0' port='1'/>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>
<model type='cirrus' vram='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</memballoon>
</devices>
</domain>
# virsh undefine 虚拟机名称 删除虚拟机⽂件(不会删除磁盘⽂件)
[root@localhost opt]# virsh undefine web02
Domain web02 has been undefined
# virsh define ⽂件名 导⼊虚拟机
[root@localhost opt]# virsh define web02_bak.xml1
Domain web02 defined from web02_bak.xml1
# virsh edit 虚拟机名称 编译虚拟机器配置(⾃带语法检测)
[root@localhost opt]# virsh edit web02
<domain type='kvm'>
<name>web02</name>
<uuid>e2835286-4a2b-4812-a970-b2fa0f85d5b3</uuid>
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='static'>1</vcpu>
<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' check='partial'>
<model fallback='allow'>Broadwell-noTSX-IBRS</model>
<feature policy='require' name='md-clear'/>
<feature policy='require' name='spec-ctrl'/>
<feature policy='require' name='ssbd'/>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
# virsh console 虚拟机名称 连接虚拟机
[root@localhost ~]# virsh console web01
Connected to domain web01
Escape character is ^]
^]
CentOS Linux 7 (Core)
Kernel 3.10.0-862.el7.x86_64 on an x86_64
localhost login: root
密码:
Last login: Mon Feb 20 16:19:06 on :0
# virsh domrename 旧名称 新名称 虚拟机重命名
[root@localhost opt]# virsh domrename web02 web2
Domain successfully renamed
# virsh vncdisplay 虚拟机名称 查看虚拟机VNC端⼝
[root@localhost ~]# virsh vncdisplay web2
:0
# virsh autostart 虚拟机名称 虚拟机随机⾃启
[root@localhost ~]# virsh autostart web2
Domain web2 marked as autostarted
# virsh autostart --disable 虚拟机名称 取消随机⾃启
[root@localhost ~]# virsh autostart --disable web2
Domain web2 unmarked as autostarted
# virsh console 虚拟机名称 进⼊虚拟机控制台
[root@localhost ~]# virsh console web01
Connected to domain web01
Escape character is ^]
^]
CentOS Linux 7 (Core)
Kernel 3.10.0-862.el7.x86_64 on an x86_64
localhost login: root
密码:
Last login: Mon Feb 20 16:19:06 on :0